-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_test.go
More file actions
130 lines (99 loc) · 2.35 KB
/
Copy pathexecutor_test.go
File metadata and controls
130 lines (99 loc) · 2.35 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func TestExecuteOrder(t *testing.T) {
tasks := []Task{
func(ctx context.Context) (Result, error) {
time.Sleep(300 * time.Millisecond)
return 1, nil
},
func(ctx context.Context) (Result, error) {
time.Sleep(100 * time.Millisecond)
return 2, nil
},
func(ctx context.Context) (Result, error) {
time.Sleep(200 * time.Millisecond)
return 3, nil
},
}
results, err := Execute(context.Background(), 3, tasks)
require.NoError(t, err)
require.Equal(t, []Result{1, 2, 3}, results)
}
func TestExecuteErrorCancels(t *testing.T) {
cancelled := atomic.Int32{}
tasks := []Task{
func(ctx context.Context) (Result, error) {
time.Sleep(100 * time.Millisecond)
return nil, errors.New("boom")
},
func(ctx context.Context) (Result, error) {
select {
case <-ctx.Done():
cancelled.Add(1)
return nil, ctx.Err()
case <-time.After(5 * time.Second):
return "finished", nil
}
},
func(ctx context.Context) (Result, error) {
select {
case <-ctx.Done():
cancelled.Add(1)
return nil, ctx.Err()
case <-time.After(5 * time.Second):
return "finished", nil
}
},
}
_, err := Execute(context.Background(), 3, tasks)
require.Error(t, err)
require.EqualError(t, err, "boom")
require.Equal(t, int32(2), cancelled.Load())
}
func TestExecuteWorkerLimit(t *testing.T) {
var running atomic.Int32
var maxRunning atomic.Int32
tasks := make([]Task, 50)
for i := range tasks {
tasks[i] = func(ctx context.Context) (Result, error) {
current := running.Add(1)
for {
old := maxRunning.Load()
if current <= old {
break
}
if maxRunning.CompareAndSwap(old, current) {
break
}
}
time.Sleep(100 * time.Millisecond)
running.Add(-1)
return nil, nil
}
}
_, err := Execute(context.Background(), 5, tasks)
require.NoError(t, err)
require.Equal(t, int32(5), maxRunning.Load())
}
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestExecuteNoLeaks(t *testing.T) {
tasks := make([]Task, 100)
for i := range tasks {
tasks[i] = func(ctx context.Context) (Result, error) {
time.Sleep(10 * time.Millisecond)
return nil, nil
}
}
_, err := Execute(context.Background(), 10, tasks)
require.NoError(t, err)
}