-
Notifications
You must be signed in to change notification settings - Fork 321
/
iter_test.go
182 lines (154 loc) · 3.76 KB
/
iter_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package iter_test
import (
"fmt"
"strconv"
"sync/atomic"
"testing"
"github.com/sourcegraph/conc/iter"
"github.com/stretchr/testify/require"
)
func ExampleIterator() {
input := []int{1, 2, 3, 4}
iterator := iter.Iterator[int]{
MaxGoroutines: len(input) / 2,
}
iterator.ForEach(input, func(v *int) {
if *v%2 != 0 {
*v = -1
}
})
fmt.Println(input)
// Output:
// [-1 2 -1 4]
}
func TestIterator(t *testing.T) {
t.Parallel()
t.Run("safe for reuse", func(t *testing.T) {
t.Parallel()
iterator := iter.Iterator[int]{MaxGoroutines: 999}
// iter.Concurrency > numInput case that updates iter.Concurrency
iterator.ForEachIdx([]int{1, 2, 3}, func(i int, t *int) {})
require.Equal(t, iterator.MaxGoroutines, 999)
})
t.Run("allows more than defaultMaxGoroutines() concurrent tasks", func(t *testing.T) {
t.Parallel()
wantConcurrency := 2 * iter.DefaultMaxGoroutines()
maxConcurrencyHit := make(chan struct{})
tasks := make([]int, wantConcurrency)
iterator := iter.Iterator[int]{MaxGoroutines: wantConcurrency}
var concurrentTasks atomic.Int64
iterator.ForEach(tasks, func(t *int) {
n := concurrentTasks.Add(1)
defer concurrentTasks.Add(-1)
if int(n) == wantConcurrency {
// All our tasks are running concurrently.
// Signal to the rest of the tasks to stop.
close(maxConcurrencyHit)
} else {
// Wait until we hit max concurrency before exiting.
// This ensures that all tasks have been started
// in parallel, despite being a larger input set than
// defaultMaxGoroutines().
<-maxConcurrencyHit
}
})
})
}
func TestForEachIdx(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
iter.ForEachIdx(ints, func(i int, val *int) {
panic("this should never be called")
})
}
require.NotPanics(t, f)
})
t.Run("panic is propagated", func(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
iter.ForEachIdx(ints, func(i int, val *int) {
panic("super bad thing happened")
})
}
require.Panics(t, f)
})
t.Run("mutating inputs is fine", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
iter.ForEachIdx(ints, func(i int, val *int) {
*val += 1
})
require.Equal(t, []int{2, 3, 4, 5, 6}, ints)
})
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
iter.ForEachIdx(ints, func(i int, val *int) {
*val = i
})
expected := make([]int, 10000)
for i := 0; i < 10000; i++ {
expected[i] = i
}
require.Equal(t, expected, ints)
})
}
func TestForEach(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{}
iter.ForEach(ints, func(val *int) {
panic("this should never be called")
})
}
require.NotPanics(t, f)
})
t.Run("panic is propagated", func(t *testing.T) {
t.Parallel()
f := func() {
ints := []int{1}
iter.ForEach(ints, func(val *int) {
panic("super bad thing happened")
})
}
require.Panics(t, f)
})
t.Run("mutating inputs is fine", func(t *testing.T) {
t.Parallel()
ints := []int{1, 2, 3, 4, 5}
iter.ForEach(ints, func(val *int) {
*val += 1
})
require.Equal(t, []int{2, 3, 4, 5, 6}, ints)
})
t.Run("huge inputs", func(t *testing.T) {
t.Parallel()
ints := make([]int, 10000)
iter.ForEach(ints, func(val *int) {
*val = 1
})
expected := make([]int, 10000)
for i := 0; i < 10000; i++ {
expected[i] = 1
}
require.Equal(t, expected, ints)
})
}
func BenchmarkForEach(b *testing.B) {
for _, count := range []int{0, 1, 8, 100, 1000, 10000, 100000} {
b.Run(strconv.Itoa(count), func(b *testing.B) {
ints := make([]int, count)
for i := 0; i < b.N; i++ {
iter.ForEach(ints, func(i *int) {
*i = 0
})
}
})
}
}