blob: 545ed16033a685b5a649c9923079593e6437562d (
plain)
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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package scrape
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_throttledCaller(t *testing.T) {
var current int64
var max int64
var total int64
var mux sync.Mutex
limit := 5
n := 10000
tc := newThrottledCaller(limit)
for i := 0; i < n; i++ {
job := func() {
atomic.AddInt64(&total, 1)
atomic.AddInt64(¤t, 1)
time.Sleep(100 * time.Microsecond)
mux.Lock()
defer mux.Unlock()
if atomic.LoadInt64(¤t) > max {
max = atomic.LoadInt64(¤t)
}
atomic.AddInt64(¤t, -1)
}
tc.call(job)
}
tc.wait()
assert.Equal(t, int64(n), total)
assert.Equal(t, max, int64(limit))
}
|