12 func TestTicker(t *testing.T) {
16 // This function is successful on "successOn" calls.
19 log.Printf("function is called %d. time\n", i)
27 return errors.New("error")
30 b := NewExponentialBackOff()
31 ticker := NewTicker(b)
32 elapsed := b.GetElapsedTime()
33 if elapsed > time.Second {
34 t.Errorf("elapsed time too large: %v", elapsed)
38 for _ = range ticker.C {
39 if err = f(); err != nil {
47 t.Errorf("unexpected error: %s", err.Error())
50 t.Errorf("invalid number of retries: %d", i)
54 func TestTickerContext(t *testing.T) {
58 ctx, cancel := context.WithCancel(context.Background())
61 // This function cancels context on "cancelOn" calls.
64 log.Printf("function is called %d. time\n", i)
66 // cancelling the context in the operation function is not a typical
67 // use-case, however it allows to get predictable test results.
73 return fmt.Errorf("error (%d)", i)
76 b := WithContext(NewConstantBackOff(time.Millisecond), ctx)
77 ticker := NewTicker(b)
80 for _ = range ticker.C {
81 if err = f(); err != nil {
89 t.Errorf("error is unexpectedly nil")
91 if err.Error() != "error (3)" {
92 t.Errorf("unexpected error: %s", err.Error())
95 t.Errorf("invalid number of retries: %d", i)