-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathresult_pool_test.go
More file actions
151 lines (134 loc) · 3.21 KB
/
result_pool_test.go
File metadata and controls
151 lines (134 loc) · 3.21 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package pool_test
import (
"fmt"
"iter"
"math/rand"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/sourcegraph/conc/pool"
"github.com/stretchr/testify/require"
)
func ExampleResultPool() {
p := pool.NewWithResults[int]()
for i := 0; i < 10; i++ {
i := i
p.Go(func() int {
return i * 2
})
}
res := p.Wait()
fmt.Println(res)
// Output:
// [0 2 4 6 8 10 12 14 16 18]
}
func ExampleResultPool_GoForEach() {
seq := func() iter.Seq[func() int] {
return func(yield func(func() int) bool) {
for i := 0; i < 10; i++ {
if !yield(func() int {
return i * 2
}) {
return
}
}
}
}
p := pool.NewWithResults[int]()
p.GoForEach(seq())
res := p.Wait()
fmt.Println(res)
// Output:
// [0 2 4 6 8 10 12 14 16 18]
}
func TestResultGroup(t *testing.T) {
t.Parallel()
t.Run("panics on configuration after init", func(t *testing.T) {
t.Run("before wait", func(t *testing.T) {
t.Parallel()
g := pool.NewWithResults[int]()
g.Go(func() int { return 0 })
require.Panics(t, func() { g.WithMaxGoroutines(10) })
})
t.Run("after wait", func(t *testing.T) {
t.Parallel()
g := pool.NewWithResults[int]()
g.Go(func() int { return 0 })
_ = g.Wait()
require.Panics(t, func() { g.WithMaxGoroutines(10) })
})
})
t.Run("basic", func(t *testing.T) {
t.Parallel()
g := pool.NewWithResults[int]()
expected := []int{}
for i := 0; i < 100; i++ {
i := i
expected = append(expected, i)
g.Go(func() int {
return i
})
}
res := g.Wait()
require.Equal(t, expected, res)
})
t.Run("deterministic order", func(t *testing.T) {
t.Parallel()
p := pool.NewWithResults[int]()
results := make([]int, 100)
for i := 0; i < 100; i++ {
results[i] = i
}
for _, result := range results {
result := result
p.Go(func() int {
// Add a random sleep to make it exceedingly unlikely that the
// results are returned in the order they are submitted.
time.Sleep(time.Duration(rand.Int()%100) * time.Millisecond)
return result
})
}
got := p.Wait()
require.Equal(t, results, got)
})
t.Run("limit", func(t *testing.T) {
t.Parallel()
for _, maxGoroutines := range []int{1, 10, 100} {
t.Run(strconv.Itoa(maxGoroutines), func(t *testing.T) {
g := pool.NewWithResults[int]().WithMaxGoroutines(maxGoroutines)
var currentConcurrent atomic.Int64
var errCount atomic.Int64
taskCount := maxGoroutines * 10
expected := make([]int, taskCount)
for i := 0; i < taskCount; i++ {
i := i
expected[i] = i
g.Go(func() int {
cur := currentConcurrent.Add(1)
if cur > int64(maxGoroutines) {
errCount.Add(1)
}
time.Sleep(time.Millisecond)
currentConcurrent.Add(-1)
return i
})
}
res := g.Wait()
require.Equal(t, expected, res)
require.Equal(t, int64(0), errCount.Load())
require.Equal(t, int64(0), currentConcurrent.Load())
})
}
})
t.Run("reuse", func(t *testing.T) {
// Test for https://github.com/sourcegraph/conc/issues/128
p := pool.NewWithResults[int]()
p.Go(func() int { return 1 })
results1 := p.Wait()
require.Equal(t, []int{1}, results1)
p.Go(func() int { return 2 })
results2 := p.Wait()
require.Equal(t, []int{2}, results2)
})
}