-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlimit_test.go
More file actions
113 lines (100 loc) · 2.38 KB
/
Copy pathlimit_test.go
File metadata and controls
113 lines (100 loc) · 2.38 KB
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
package simultaneous_test
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/singlestore-labs/simultaneous"
)
const (
threadCount = 1000
max = 10
sleep = time.Microsecond * 10
)
func TestLimitSilent(t *testing.T) {
testLimit(t, false)
}
func TestLimitVerbose(t *testing.T) {
testLimit(t, true)
}
func testLimit(t *testing.T, withStuck bool) {
t.Parallel()
var running int32
var wg sync.WaitGroup
limit := simultaneous.New[any](max)
var stuckCalled atomic.Int32
var unstuckCalled atomic.Int32
someUnstuck := make(chan struct{})
if withStuck {
limit = limit.SetForeverMessaging(time.Millisecond,
func(context.Context) {
if stuckCalled.Add(1) == 1 {
close(someUnstuck)
}
},
func(context.Context) {
unstuckCalled.Add(1)
},
)
}
var fail atomic.Int32
var success atomic.Int32
for i := 0; i < threadCount; i++ {
i := i
wg.Add(1)
go func() {
defer wg.Done()
var done simultaneous.Limited[any]
switch i % 5 {
case 0:
done = limit.Forever(context.Background())
case 1:
var err error
done, err = limit.Timeout(context.Background(), 0)
if err != nil {
fail.Add(1)
return
} else {
success.Add(1)
}
case 2:
var err error
done, err = limit.Timeout(context.Background(), time.Second*2)
if !assert.NoError(t, err) {
return
}
case 3:
var err error
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
done, err = limit.Timeout(ctx, time.Hour)
if !assert.NoError(t, err) {
return
}
case 4:
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
cancel()
limit.Forever(ctx).Done()
return
}
assert.LessOrEqual(t, atomic.AddInt32(&running, 1), int32(max))
time.Sleep(sleep)
assert.GreaterOrEqual(t, atomic.AddInt32(&running, -1), int32(0))
if withStuck {
<-someUnstuck
}
done.Done()
}()
}
wg.Wait()
if withStuck {
t.Logf("stuck called %d unstuck called %d", stuckCalled.Load(), unstuckCalled.Load())
assert.NotZero(t, stuckCalled.Load(), "stuck reported")
assert.Equal(t, stuckCalled.Load(), unstuckCalled.Load(), "stuck == unstuck")
}
t.Logf("timeout 0 failed %d succeeded %d", fail.Load(), success.Load())
assert.NotZero(t, fail.Load(), "fail")
assert.NotZero(t, success.Load(), "succeed")
}