Unit test for exponential backoff in Golang

354 Views Asked by At

I'm currently writing a function that would retry with exponential backoff up to 5 times. How do I write the unit test for testing the wait time is correct or not? Here's my function:

func (OdinClient) GetPrivateKeySigner(materialSet string) (ssh.Signer, error) {
    # If it fails on first try, then retry 5 times, or return the value
    signer, err := ssh.NewSignerFromKey(privateKeyMaterial.PrivateKey)
    if err != nil {
        for i := 0; i < 5; i++ {
            time.Sleep(time.Duration(math.Pow(2, float64(i)) * 100) * time.Millisecond)
            signer, err := ssh.NewSignerFromKey(privateKeyMaterial.PrivateKey)
            if err == nil {
                return signer, nil
            }
        }
        # If still fails after 5 retries, return error message. 
        return nil, novaerr.New("utils/getPrivateKey", fmt.Sprintf("failed"), err)
    } else {
        return signer, nil
    }
}
0

There are 0 best solutions below