-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore_test.go
138 lines (116 loc) · 2.94 KB
/
semaphore_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
package shopifysemaphore
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func newSemaphore(cap int, opts ...func(*Semaphore)) *Semaphore {
return NewSemaphore(cap, NewBalance(900, 1000, 100), opts...)
}
// TestAquire should run N Goroutines. Allowing them
// to aquire and release their spot. We are expecting no error to happen
// and for the count (cnt) to match the number of Goroutines N.
func TestAquire(t *testing.T) {
var err error
var wg sync.WaitGroup
var cnt int // Count of Gorotunes which ran.
cap := 1 // Capacity.
n := 2 // Number of Goroutines to spin up.
ctx := context.Background()
sema := newSemaphore(cap)
for i := 0; i < n; i += 1 {
wg.Add(1)
go func() {
err = sema.Aquire(ctx)
if err != nil {
wg.Done()
return
}
cnt += 1
wg.Done()
sema.Release(1000)
}()
}
wg.Wait()
if cnt != n {
t.Errorf("cnt = %d; want %d", cnt, n)
}
if err != nil {
t.Errorf("Aquire(%q) = %v; want nil", ctx, err)
}
}
// TestReleaseCausesPauseAndResume should create a situation where
// Release triggers a pause to happen due to hitting a threshold.
func TestReleaseCausesPauseAndResume(t *testing.T) {
var err error
var wg sync.WaitGroup
var cnt int // Count of Gorotunes which ran.
cap := 1 // Capacity.
n := 2 // Number of Goroutines to spin up.
res := false // If resume happened.
exdur := 1 * time.Second // Expected duration of pause.
var expts int32 = 900 // Expected points at pause.
ctx := context.Background()
sema := newSemaphore(cap, WithPauseFunc(func(pts int32, dur time.Duration) {
if pts != expts || dur != exdur {
t.Errorf("PauseFunc(%d, %q); want PauseFunc(%d, %q)", pts, dur, expts, exdur)
}
}), WithResumeFunc(func() {
res = true
}))
for i := 0; i < n; i += 1 {
wg.Add(1)
go func() {
err = sema.Aquire(ctx)
if err != nil {
wg.Done()
return
}
cnt += 1
wg.Done()
sema.Release(expts)
}()
}
wg.Wait()
if cnt != 2 {
t.Errorf("cnt = %d; want %d", cnt, n)
}
if err != nil {
t.Errorf("err = %v; want nil", err)
}
if res == false {
t.Errorf("res = %v; want true", res)
}
}
// TestAquireCtxErr should detect a context error on attempting
// to aquire a spot.
func TestAquireCtxErr(t *testing.T) {
var err error
var wg sync.WaitGroup
cap := 1 // Capacity.
n := 2 // Number of Goroutines to spin up.
ctxo := 50 * time.Millisecond // Context timeout duration.
ctx, cancel := context.WithTimeout(context.Background(), ctxo)
defer cancel()
sema := newSemaphore(cap)
for i := 0; i < n; i += 1 {
wg.Add(1)
go func() {
err = sema.Aquire(ctx)
if err != nil {
wg.Done()
return
}
// Make work "long", longer than the context timeout.
time.Sleep(ctxo * 4)
wg.Done()
sema.Release(1000)
}()
}
wg.Wait()
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("err = %v; want %v", err, context.DeadlineExceeded)
}
}