-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle_test.go
203 lines (186 loc) · 4.44 KB
/
throttle_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Tideland Go Wait - Unit Tests
//
// Copyright (C) 2019-2023 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package wait_test // import "tideland.dev/go/wait"
//--------------------
// IMPORTS
//--------------------
import (
"context"
"sync"
"testing"
"time"
"tideland.dev/go/audit/asserts"
"tideland.dev/go/wait"
)
//--------------------
// TESTS
//--------------------
// TestThrottle verifies the throttling of parallel processed events.
func TestThrottle(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
tests := []struct {
name string
limit wait.Limit
burst int
tasks int
timeout time.Duration
err string
}{
{
name: "throttle allows no tasks",
limit: 0,
burst: 0,
tasks: 10,
err: "Wait(n=1) exceeds limiter's burst 0",
},
{
name: "throttle has infinite limit and no burst",
limit: wait.InfLimit,
burst: 0,
tasks: 10,
timeout: time.Second,
},
{
name: "throttle allows two tasks per second",
limit: 2,
burst: 1,
tasks: 10,
},
{
name: "throttle allows five tasks per second",
limit: 5,
burst: 1,
tasks: 10,
},
}
// Run the different tests.
for _, test := range tests {
assert.Logf("test: %s", test.name)
throttle := wait.NewThrottle(test.limit, test.burst)
ctx := context.Background()
if test.timeout > 0 {
// Add a timeout to the context.
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, test.timeout)
defer cancel()
}
var wg sync.WaitGroup
wg.Add(test.tasks)
cc := &concurrencyCounter{}
start := time.Now()
task := func() error {
cc.incr()
defer cc.decr()
time.Sleep(25 * time.Millisecond)
return nil
}
for i := 0; i < test.tasks; i++ {
// Process the task in a goroutine.
go func() {
err := throttle.Process(ctx, task)
wg.Done()
if test.err == "" {
assert.NoError(err)
} else {
assert.ErrorContains(err, test.err)
}
}()
}
wg.Wait()
elapsed := time.Since(start)
assert.Logf("elapsed: %v", elapsed)
// Check the results.
if test.burst > 0 {
assert.Equal(cc.max(), test.burst, "maximum number of parallel goroutines defined by burst")
}
switch {
case test.limit == 0 && test.burst == 0:
assert.Equal(cc.max(), 0)
case test.limit > 0 && test.burst == 1:
expected := (time.Duration(test.tasks) / time.Duration(test.limit)) * time.Second
tenth := expected / 10
assert.Range(elapsed, expected-tenth, expected+tenth)
}
}
}
// TestThrottleBurst verifies the influence of the burst on throttling.
func TestThrottleBurst(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)
results := [3][3]struct {
burst int
tasks int
elapsed time.Duration
}{}
// Run nested tests.
for i, burst := range []int{1, 5, 100} {
for j, tasks := range []int{10, 50, 10000} {
assert.Logf("burst: %d, tasks: %d", burst, tasks)
throttle := wait.NewThrottle(wait.InfLimit, burst)
ctx := context.Background()
var wg sync.WaitGroup
wg.Add(tasks)
cc := &concurrencyCounter{}
start := time.Now()
task := func() error {
cc.incr()
defer cc.decr()
time.Sleep(25 * time.Millisecond)
return nil
}
for k := 0; k < tasks; k++ {
// Pause a bit every 250 tasks.
if k%250 == 0 {
time.Sleep(10 * time.Millisecond)
}
// Process the task in a goroutine.
go func() {
throttle.Process(ctx, task)
wg.Done()
}()
}
wg.Wait()
elapsed := time.Since(start)
assert.Logf("elapsed: %v", elapsed)
results[i][j].burst = burst
results[i][j].tasks = tasks
results[i][j].elapsed = elapsed
}
}
}
//--------------------
// HELPER
//--------------------
// concurrencyCounter is a helper to count the maximum number of
// parallel running goroutines.
type concurrencyCounter struct {
mu sync.Mutex
current int
maximum int
}
// increase increases the current number of goroutines and
// updates the maximum.
func (cc *concurrencyCounter) incr() {
cc.mu.Lock()
defer cc.mu.Unlock()
cc.current++
if cc.current > cc.maximum {
cc.maximum = cc.current
}
}
// decrease decreases the current number of goroutines.
func (cc *concurrencyCounter) decr() {
cc.mu.Lock()
defer cc.mu.Unlock()
cc.current--
}
// maximum returns the maximum number of parallel running goroutines.
func (cc *concurrencyCounter) max() int {
cc.mu.Lock()
defer cc.mu.Unlock()
return cc.maximum
}
// EOF