-
Notifications
You must be signed in to change notification settings - Fork 79
/
locker_test.go
185 lines (155 loc) · 4.68 KB
/
locker_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package osquery
import (
"context"
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLocker(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sleepTime time.Duration
ctxTimeout time.Duration
parallelism int
expectedSuccessesRange [2]int
expectedErrorRange [2]int
expectedErrors []string
}{
{
name: "basic",
sleepTime: 1 * time.Millisecond,
ctxTimeout: 10 * time.Millisecond,
parallelism: 5,
expectedSuccessesRange: [2]int{5, 5},
},
{
name: "some finishers",
sleepTime: 4 * time.Millisecond,
ctxTimeout: 10 * time.Millisecond,
parallelism: 5,
expectedSuccessesRange: [2]int{2, 3},
expectedErrorRange: [2]int{2, 3},
},
{
name: "sleep longer than context",
sleepTime: 150 * time.Millisecond,
ctxTimeout: 10 * time.Millisecond,
parallelism: 5,
expectedSuccessesRange: [2]int{1, 1},
expectedErrorRange: [2]int{4, 4},
expectedErrors: []string{"context canceled: context deadline exceeded"},
},
{
name: "no ctx fall back to default timeout",
sleepTime: 150 * time.Millisecond,
parallelism: 5,
expectedSuccessesRange: [2]int{1, 1},
expectedErrorRange: [2]int{4, 4},
expectedErrors: []string{"timeout after 100ms"},
},
{
name: "ctx longer than maxwait",
sleepTime: 250 * time.Millisecond,
ctxTimeout: 10 * time.Second,
parallelism: 5,
expectedSuccessesRange: [2]int{1, 1},
expectedErrorRange: [2]int{4, 4},
expectedErrors: []string{"timeout after maximum of 200ms"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
doer := NewThingDoer()
wait := sync.WaitGroup{}
for i := 0; i < tt.parallelism; i++ {
wait.Add(1)
go func() {
defer wait.Done()
ctx := context.TODO()
if tt.ctxTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, tt.ctxTimeout)
defer cancel()
}
_ = doer.Once(ctx, tt.sleepTime)
}()
}
wait.Wait()
assertBetween(t, doer.Successes, tt.expectedSuccessesRange, "success count")
assertBetween(t, len(doer.Errors), tt.expectedErrorRange, "error count")
for _, errMsg := range tt.expectedErrors {
assert.Contains(t, doer.Errors, errMsg)
}
})
}
}
func TestNeedlessUnlock(t *testing.T) {
t.Parallel()
locker := NewLocker(100*time.Millisecond, 200*time.Millisecond)
assert.Panics(t, func() { locker.Unlock() })
}
func TestDoubleUnlock(t *testing.T) {
t.Parallel()
locker := NewLocker(100*time.Millisecond, 200*time.Millisecond)
require.NoError(t, locker.Lock(context.TODO()))
assert.NotPanics(t, func() { locker.Unlock() })
assert.Panics(t, func() { locker.Unlock() })
}
func TestLockerChaos(t *testing.T) {
t.Parallel()
doer := NewThingDoer()
wait := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wait.Add(1)
go func() {
defer wait.Done()
ctx := context.TODO()
if rand.Intn(100) > 20 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(rand.Intn(500))*time.Millisecond)
defer cancel()
}
_ = doer.Once(ctx, time.Duration(rand.Intn(100))*time.Millisecond)
}()
}
wait.Wait()
assert.GreaterOrEqual(t, doer.Successes, 1, "successes")
assert.GreaterOrEqual(t, len(doer.Errors), 1, "failures")
}
type thingDoer struct {
locker *locker
Successes int
Errors []string
errMu sync.Mutex
}
func NewThingDoer() *thingDoer {
return &thingDoer{
locker: NewLocker(100*time.Millisecond, 200*time.Millisecond),
Errors: make([]string, 0),
}
}
func (doer *thingDoer) Once(ctx context.Context, d time.Duration) error {
if err := doer.locker.Lock(ctx); err != nil {
doer.errMu.Lock()
defer doer.errMu.Unlock()
doer.Errors = append(doer.Errors, err.Error())
return err
}
defer doer.locker.Unlock()
// Note that we don't need to protect the success path with a mutext, that is the point of locker
time.Sleep(d)
doer.Successes += 1
return nil
}
// assertBetween is a wrapper over assert.GreateOrEqual and assert.LessOrEqual. We use it to provide small ranges for
// expected test results. This is because GitHub Actions is prone to weird timing issues and slowdowns
func assertBetween(t *testing.T, actual int, r [2]int, msg string) {
assert.GreaterOrEqual(t, actual, r[0], msg)
assert.LessOrEqual(t, actual, r[1], msg)
}