@@ -15,54 +15,54 @@ func TestLocker(t *testing.T) {
15
15
t .Parallel ()
16
16
17
17
tests := []struct {
18
- name string
19
- sleepTime time.Duration
20
- ctxTimeout time.Duration
21
- parallelism int
22
- expectedSuccesses int
23
- expectedErrorCount int
24
- expectedErrors []string
18
+ name string
19
+ sleepTime time.Duration
20
+ ctxTimeout time.Duration
21
+ parallelism int
22
+ expectedSuccessesRange [ 2 ] int
23
+ expectedErrorRange [ 2 ] int
24
+ expectedErrors []string
25
25
}{
26
26
{
27
- name : "basic" ,
28
- sleepTime : 1 * time .Millisecond ,
29
- ctxTimeout : 10 * time .Millisecond ,
30
- parallelism : 5 ,
31
- expectedSuccesses : 5 ,
27
+ name : "basic" ,
28
+ sleepTime : 1 * time .Millisecond ,
29
+ ctxTimeout : 10 * time .Millisecond ,
30
+ parallelism : 5 ,
31
+ expectedSuccessesRange : [ 2 ] int { 5 , 5 } ,
32
32
},
33
33
{
34
- name : "some finishers" ,
35
- sleepTime : 4 * time .Millisecond ,
36
- ctxTimeout : 10 * time .Millisecond ,
37
- parallelism : 5 ,
38
- expectedSuccesses : 3 ,
39
- expectedErrorCount : 2 ,
34
+ name : "some finishers" ,
35
+ sleepTime : 4 * time .Millisecond ,
36
+ ctxTimeout : 10 * time .Millisecond ,
37
+ parallelism : 5 ,
38
+ expectedSuccessesRange : [ 2 ] int { 2 , 3 } ,
39
+ expectedErrorRange : [ 2 ] int { 2 , 3 } ,
40
40
},
41
41
{
42
- name : "sleep longer than context" ,
43
- sleepTime : 150 * time .Millisecond ,
44
- ctxTimeout : 10 * time .Millisecond ,
45
- parallelism : 5 ,
46
- expectedSuccesses : 1 ,
47
- expectedErrorCount : 4 ,
48
- expectedErrors : []string {"context canceled: context deadline exceeded" },
42
+ name : "sleep longer than context" ,
43
+ sleepTime : 150 * time .Millisecond ,
44
+ ctxTimeout : 10 * time .Millisecond ,
45
+ parallelism : 5 ,
46
+ expectedSuccessesRange : [ 2 ] int { 1 , 1 } ,
47
+ expectedErrorRange : [ 2 ] int { 4 , 4 } ,
48
+ expectedErrors : []string {"context canceled: context deadline exceeded" },
49
49
},
50
50
{
51
- name : "no ctx fall back to default timeout" ,
52
- sleepTime : 150 * time .Millisecond ,
53
- parallelism : 5 ,
54
- expectedSuccesses : 1 ,
55
- expectedErrorCount : 4 ,
56
- expectedErrors : []string {"timeout after 100ms" },
51
+ name : "no ctx fall back to default timeout" ,
52
+ sleepTime : 150 * time .Millisecond ,
53
+ parallelism : 5 ,
54
+ expectedSuccessesRange : [ 2 ] int { 1 , 1 } ,
55
+ expectedErrorRange : [ 2 ] int { 4 , 4 } ,
56
+ expectedErrors : []string {"timeout after 100ms" },
57
57
},
58
58
{
59
- name : "ctx longer than maxwait" ,
60
- sleepTime : 250 * time .Millisecond ,
61
- ctxTimeout : 10 * time .Second ,
62
- parallelism : 5 ,
63
- expectedSuccesses : 1 ,
64
- expectedErrorCount : 4 ,
65
- expectedErrors : []string {"timeout after maximum of 200ms" },
59
+ name : "ctx longer than maxwait" ,
60
+ sleepTime : 250 * time .Millisecond ,
61
+ ctxTimeout : 10 * time .Second ,
62
+ parallelism : 5 ,
63
+ expectedSuccessesRange : [ 2 ] int { 1 , 1 } ,
64
+ expectedErrorRange : [ 2 ] int { 4 , 4 } ,
65
+ expectedErrors : []string {"timeout after maximum of 200ms" },
66
66
},
67
67
}
68
68
@@ -91,8 +91,8 @@ func TestLocker(t *testing.T) {
91
91
92
92
wait .Wait ()
93
93
94
- assert . Equal (t , tt . expectedSuccesses , doer . Successes )
95
- assert . Equal (t , tt . expectedErrorCount , len (doer .Errors ))
94
+ assertBetween (t , doer . Successes , tt . expectedSuccessesRange , "success count" )
95
+ assertBetween (t , len (doer .Errors ), tt . expectedErrorRange , "error count" )
96
96
97
97
for _ , errMsg := range tt .expectedErrors {
98
98
assert .Contains (t , doer .Errors , errMsg )
@@ -176,3 +176,10 @@ func (doer *thingDoer) Once(ctx context.Context, d time.Duration) error {
176
176
177
177
return nil
178
178
}
179
+
180
+ // assertBetween is a wrapper over assert.GreateOrEqual and assert.LessOrEqual. We use it to provide small ranges for
181
+ // expected test results. This is because GitHub Actions is prone to weird timing issues and slowdowns
182
+ func assertBetween (t * testing.T , actual int , r [2 ]int , msg string ) {
183
+ assert .GreaterOrEqual (t , actual , r [0 ], msg )
184
+ assert .LessOrEqual (t , actual , r [1 ], msg )
185
+ }
0 commit comments