(t *testing.T)
| 38 | } |
| 39 | |
| 40 | func TestTaskBackoffReset(t *testing.T) { |
| 41 | if enabled := os.Getenv("TEST_TIMING"); enabled == "" { |
| 42 | t.Skip("Timing sensitive tests are disabled") |
| 43 | } |
| 44 | |
| 45 | a := assertions.New(t) |
| 46 | ctx := test.Context() |
| 47 | |
| 48 | c, err := New(test.GetLogger(t), &Config{}) |
| 49 | a.So(err, should.BeNil) |
| 50 | |
| 51 | var ( |
| 52 | calls uint |
| 53 | lastCallTime time.Time |
| 54 | wg sync.WaitGroup |
| 55 | ) |
| 56 | wg.Add(4) |
| 57 | c.RegisterTask(&task.Config{ |
| 58 | Context: ctx, |
| 59 | ID: "failing_but_recovering", |
| 60 | Func: func(_ context.Context) error { |
| 61 | defer wg.Done() |
| 62 | calls++ |
| 63 | backoff := time.Since(lastCallTime) |
| 64 | switch calls { |
| 65 | case 1: |
| 66 | // Nothing to expect in the initial call |
| 67 | case 2: |
| 68 | // Expect the first backoff interval |
| 69 | a.So(backoff, should.BeBetweenOrEqual, timeMult(test.Delay, 0.9), timeMult(test.Delay, 1.1)) |
| 70 | case 3: |
| 71 | // Same jitter requirement, but now act as if we are 'working' |
| 72 | a.So(backoff, should.BeBetweenOrEqual, timeMult(2*test.Delay, 0.9), timeMult(2*test.Delay, 1.1)) |
| 73 | time.Sleep(3 * test.Delay) |
| 74 | case 4: |
| 75 | // Expect the backoff to have been reset |
| 76 | a.So(backoff, should.BeBetweenOrEqual, timeMult(test.Delay, 0.9), timeMult(test.Delay, 1.1)) |
| 77 | } |
| 78 | lastCallTime = time.Now() |
| 79 | return nil |
| 80 | }, |
| 81 | Restart: task.RestartAlways, |
| 82 | Backoff: TestTaskBackoffConfig, |
| 83 | }) |
| 84 | |
| 85 | // Wait for all invocations. |
| 86 | test.Must[any](nil, c.Start()) |
| 87 | defer c.Close() |
| 88 | wg.Wait() |
| 89 | } |
| 90 | |
| 91 | func timeMult(t time.Duration, c float64) time.Duration { |
| 92 | ft := float64(t) |
nothing calls this directly
no test coverage detected