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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
package pool
import (
"context"
"errors"
"fmt"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleContextPool_WithCancelOnError() {
p := New().
WithMaxGoroutines(4).
WithContext(context.Background()).
WithCancelOnError()
for i := 0; i < 3; i++ {
i := i
p.Go(func(ctx context.Context) error {
if i == 2 {
return errors.New("I will cancel all other tasks!")
}
<-ctx.Done()
return nil
})
}
err := p.Wait()
fmt.Println(err)
// Output:
// I will cancel all other tasks!
}
func TestContextPool(t *testing.T) {
t.Parallel()
err1 := errors.New("err1")
err2 := errors.New("err2")
bgctx := context.Background()
t.Run("panics on configuration after init", func(t *testing.T) {
t.Run("before wait", func(t *testing.T) {
t.Parallel()
g := New().WithContext(context.Background())
g.Go(func(context.Context) error { return nil })
require.Panics(t, func() { g.WithMaxGoroutines(10) })
})
t.Run("after wait", func(t *testing.T) {
t.Parallel()
g := New().WithContext(context.Background())
g.Go(func(context.Context) error { return nil })
_ = g.Wait()
require.Panics(t, func() { g.WithMaxGoroutines(10) })
})
})
t.Run("behaves the same as ErrorGroup", func(t *testing.T) {
t.Parallel()
t.Run("wait returns no error if no errors", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx)
p.Go(func(context.Context) error { return nil })
require.NoError(t, p.Wait())
})
t.Run("wait errors if func returns error", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx)
p.Go(func(context.Context) error { return err1 })
require.ErrorIs(t, p.Wait(), err1)
})
t.Run("wait error is all returned errors", func(t *testing.T) {
t.Parallel()
p := New().WithErrors().WithContext(bgctx)
p.Go(func(context.Context) error { return err1 })
p.Go(func(context.Context) error { return nil })
p.Go(func(context.Context) error { return err2 })
err := p.Wait()
require.ErrorIs(t, err, err1)
require.ErrorIs(t, err, err2)
})
})
t.Run("context error propagates", func(t *testing.T) {
t.Parallel()
t.Run("canceled", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(bgctx)
p := New().WithContext(ctx)
p.Go(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
cancel()
require.ErrorIs(t, p.Wait(), context.Canceled)
})
t.Run("timed out", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(bgctx, time.Millisecond)
defer cancel()
p := New().WithContext(ctx)
p.Go(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
require.ErrorIs(t, p.Wait(), context.DeadlineExceeded)
})
})
t.Run("WithCancelOnError", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx).WithCancelOnError()
p.Go(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
p.Go(func(ctx context.Context) error {
return err1
})
err := p.Wait()
require.ErrorIs(t, err, context.Canceled)
require.ErrorIs(t, err, err1)
})
t.Run("no WithCancelOnError", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx)
p.Go(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(10 * time.Millisecond):
return nil
}
})
p.Go(func(ctx context.Context) error {
return err1
})
err := p.Wait()
require.ErrorIs(t, err, err1)
require.NotErrorIs(t, err, context.Canceled)
})
t.Run("WithFirstError", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx).WithFirstError()
sync := make(chan struct{})
p.Go(func(ctx context.Context) error {
defer close(sync)
return err1
})
p.Go(func(ctx context.Context) error {
// This test has a race condition. After the first goroutine
// completes, this goroutine is woken up because sync is closed.
// However, this goroutine might be woken up before the error from
// the first goroutine is registered. To prevent that, we sleep for
// another 10 milliseconds, giving the other goroutine time to return
// and register its error before this goroutine returns its error.
<-sync
time.Sleep(10 * time.Millisecond)
return err2
})
err := p.Wait()
require.ErrorIs(t, err, err1)
require.NotErrorIs(t, err, err2)
})
t.Run("WithFirstError and WithCancelOnError", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx).WithFirstError().WithCancelOnError()
p.Go(func(ctx context.Context) error {
return err1
})
p.Go(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
})
err := p.Wait()
require.ErrorIs(t, err, err1)
require.NotErrorIs(t, err, context.Canceled)
})
t.Run("WithCancelOnError and panic", func(t *testing.T) {
t.Parallel()
p := New().WithContext(bgctx).WithCancelOnError()
var cancelledTasks atomic.Int64
p.Go(func(ctx context.Context) error {
<-ctx.Done()
cancelledTasks.Add(1)
return ctx.Err()
})
p.Go(func(ctx context.Context) error {
<-ctx.Done()
cancelledTasks.Add(1)
return ctx.Err()
})
p.Go(func(ctx context.Context) error {
panic("abort!")
})
assert.Panics(t, func() { _ = p.Wait() })
assert.EqualValues(t, 2, cancelledTasks.Load())
})
t.Run("limit", func(t *testing.T) {
t.Parallel()
for _, maxConcurrent := range []int{1, 10, 100} {
t.Run(strconv.Itoa(maxConcurrent), func(t *testing.T) {
maxConcurrent := maxConcurrent // copy
t.Parallel()
p := New().WithContext(bgctx).WithMaxGoroutines(maxConcurrent)
var currentConcurrent atomic.Int64
for i := 0; i < 100; i++ {
p.Go(func(context.Context) error {
cur := currentConcurrent.Add(1)
if cur > int64(maxConcurrent) {
return fmt.Errorf("expected no more than %d concurrent goroutine", maxConcurrent)
}
time.Sleep(time.Millisecond)
currentConcurrent.Add(-1)
return nil
})
}
require.NoError(t, p.Wait())
require.Equal(t, int64(0), currentConcurrent.Load())
})
}
})
}
|