1
+ // Tideland Go Wait - Unit Tests
2
+ //
3
+ // Copyright (C) 2019-2025 Frank Mueller / Tideland / Oldenburg / Germany
4
+ //
5
+ // All rights reserved. Use of this source code is governed
6
+ // by the new BSD license.
7
+
8
+ package wait_test // import "tideland.dev/go/wait"
9
+
10
+ //--------------------
11
+ // IMPORTS
12
+ //--------------------
13
+
14
+ import (
15
+ "context"
16
+ "testing"
17
+ "time"
18
+
19
+ "tideland.dev/go/asserts/verify"
20
+
21
+ "tideland.dev/go/wait"
22
+ )
23
+
24
+ //--------------------
25
+ // TESTS
26
+ //--------------------
27
+
28
+ // TestPollWithJitter tests the polling of conditions in a maximum
29
+ // number of intervals.
30
+ func TestPollWithJitter (t * testing.T ) {
31
+ timestamps := []time.Time {}
32
+ err := wait .Poll (
33
+ context .Background (),
34
+ wait .MakeJitteringTicker (
35
+ 50 * time .Millisecond ,
36
+ 10 * time .Millisecond ,
37
+ 1250 * time .Millisecond ,
38
+ ),
39
+ func () (bool , error ) {
40
+ timestamps = append (timestamps , time .Now ())
41
+ if len (timestamps ) == 10 {
42
+ return true , nil
43
+ }
44
+ return false , nil
45
+ },
46
+ )
47
+ verify .NoError (t , err )
48
+ verify .Length (t , timestamps , 10 )
49
+
50
+ t .Logf ("Timestamps for first test: %v" , timestamps )
51
+ for i := range 9 {
52
+ diff := timestamps [i + 1 ].Sub (timestamps [i ])
53
+ t .Logf ("Diff %d: %v" , i , diff )
54
+ // According to implementation, jitter is within [offset, offset+interval]
55
+ verify .InRange (t , 10 * time .Millisecond , 60 * time .Millisecond , diff )
56
+ }
57
+
58
+ timestamps = []time.Time {}
59
+ err = wait .WithJitter (
60
+ context .Background (),
61
+ 50 * time .Millisecond ,
62
+ 10 * time .Millisecond ,
63
+ 1250 * time .Millisecond , func () (bool , error ) {
64
+ timestamps = append (timestamps , time .Now ())
65
+ if len (timestamps ) == 10 {
66
+ return true , nil
67
+ }
68
+ return false , nil
69
+ })
70
+ verify .NoError (t , err )
71
+ verify .Length (t , timestamps , 10 )
72
+
73
+ t .Logf ("Timestamps for second test: %v" , timestamps )
74
+ for i := 1 ; i < 10 ; i ++ {
75
+ diff := timestamps [i ].Sub (timestamps [i - 1 ])
76
+ t .Logf ("Diff %d: %v" , i , diff )
77
+ // According to implementation, jitter is within [offset, offset+interval]
78
+ verify .InRange (t , 10 * time .Millisecond , 60 * time .Millisecond , diff )
79
+ }
80
+
81
+ timestamps = []time.Time {}
82
+ err = wait .Poll (
83
+ context .Background (),
84
+ wait .MakeJitteringTicker (
85
+ 50 * time .Millisecond ,
86
+ 10 * time .Millisecond ,
87
+ 1250 * time .Millisecond ),
88
+ func () (bool , error ) {
89
+ timestamps = append (timestamps , time .Now ())
90
+ return false , nil
91
+ },
92
+ )
93
+ verify .ErrorContains (t , "exceeded" , err )
94
+ verify .InRange (t , len (timestamps ), 10 , 25 )
95
+
96
+ timestamps = []time.Time {}
97
+ err = wait .Poll (
98
+ context .Background (),
99
+ wait .MakeJitteringTicker (
100
+ 50 * time .Millisecond ,
101
+ 10 * time .Millisecond ,
102
+ 1000 * time .Millisecond ),
103
+ func () (bool , error ) {
104
+ timestamps = append (timestamps , time .Now ())
105
+ return false , nil
106
+ },
107
+ )
108
+ verify .ErrorContains (t , "exceeded" , err )
109
+ // This ticker has a non-zero offset, so it should run at least once before timing out
110
+ verify .True (t , len (timestamps ) > 0 )
111
+
112
+ timestamps = []time.Time {}
113
+ ctx , cancel := context .WithTimeout (context .Background (), 350 * time .Millisecond )
114
+ defer cancel ()
115
+ err = wait .Poll (
116
+ ctx ,
117
+ wait .MakeJitteringTicker (
118
+ 50 * time .Millisecond ,
119
+ 10 * time .Millisecond ,
120
+ 1000 * time .Millisecond ),
121
+ func () (bool , error ) {
122
+ timestamps = append (timestamps , time .Now ())
123
+ return false , nil
124
+ },
125
+ )
126
+ verify .ErrorContains (t , "exceeded" , err )
127
+ // Context cancellation should still allow some ticks to happen
128
+ verify .True (t , len (timestamps ) > 0 )
129
+ }
130
+
131
+ // EOF
0 commit comments