-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor_test.go
69 lines (58 loc) · 1.23 KB
/
executor_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
package sync
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/anchore/go-sync/internal/atomic"
"github.com/anchore/go-sync/internal/stats"
)
func Test_Executors(t *testing.T) {
for i := 0; i < 100; i++ {
Test_Executor(t)
}
}
func Test_Executor(t *testing.T) {
const count = 1000
tests := []struct {
name string
maxConcurrency int
}{
{
name: "unbounded concurrency",
maxConcurrency: 0,
},
{
name: "single execution",
maxConcurrency: 1,
},
{
name: "dual execution",
maxConcurrency: 2,
},
{
name: "ten-x execution",
maxConcurrency: 10,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := NewExecutor(test.maxConcurrency)
executed := atomic.Int32{}
concurrency := stats.Tracked[int]{}
for i := 0; i < count; i++ {
e.Execute(func() {
defer concurrency.Incr()()
executed.Add(1)
time.Sleep(10 * time.Nanosecond)
})
}
e.Wait()
require.Equal(t, count, int(executed.Load()))
if test.maxConcurrency > 0 {
require.LessOrEqual(t, concurrency.Max(), test.maxConcurrency)
} else {
require.GreaterOrEqual(t, concurrency.Max(), 1)
}
})
}
}