MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestTaskBackoffReset

Function TestTaskBackoffReset

pkg/component/tasks_internal_test.go:40–89  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

38}
39
40func 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
91func timeMult(t time.Duration, c float64) time.Duration {
92 ft := float64(t)

Callers

nothing calls this directly

Calls 12

ContextFunction · 0.92
GetLoggerFunction · 0.92
timeMultFunction · 0.85
RegisterTaskMethod · 0.80
DoneMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
AddMethod · 0.65
NowMethod · 0.65
CloseMethod · 0.65
WaitMethod · 0.65
StartMethod · 0.45

Tested by

no test coverage detected