-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor_test.go
81 lines (70 loc) · 1.48 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
70
71
72
73
74
75
76
77
78
79
80
81
package sync
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"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: "sequential",
maxConcurrency: 0,
},
{
name: "unbounded concurrency",
maxConcurrency: -1,
},
{
name: "single execution",
maxConcurrency: 1,
},
{
name: "dual execution",
maxConcurrency: 2,
},
{
name: "ten-x execution",
maxConcurrency: 10,
},
}
for _, test := range tests {
for _, errGroup := range []bool{false, true} {
t.Run(test.name, func(t *testing.T) {
e := NewExecutor(test.maxConcurrency)
if !errGroup && test.maxConcurrency > 1 {
e = &queuedExecutor{
maxConcurrency: test.maxConcurrency,
}
}
executed := atomic.Int32{}
concurrency := stats.Tracked[int]{}
for i := 0; i < count; i++ {
e.Go(func() {
defer concurrency.Incr()()
executed.Add(1)
time.Sleep(10 * time.Nanosecond)
})
}
e.Wait(context.Background())
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)
}
})
}
}
}