(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestResultContextPool(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | |
| 20 | err1 := errors.New("err1") |
| 21 | err2 := errors.New("err2") |
| 22 | |
| 23 | t.Run("panics on configuration after init", func(t *testing.T) { |
| 24 | t.Run("before wait", func(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | g := NewWithResults[int]().WithContext(context.Background()) |
| 27 | g.Go(func(context.Context) (int, error) { return 0, nil }) |
| 28 | require.Panics(t, func() { g.WithMaxGoroutines(10) }) |
| 29 | }) |
| 30 | |
| 31 | t.Run("after wait", func(t *testing.T) { |
| 32 | t.Parallel() |
| 33 | g := NewWithResults[int]().WithContext(context.Background()) |
| 34 | g.Go(func(context.Context) (int, error) { return 0, nil }) |
| 35 | _, _ = g.Wait() |
| 36 | require.Panics(t, func() { g.WithMaxGoroutines(10) }) |
| 37 | }) |
| 38 | }) |
| 39 | |
| 40 | t.Run("behaves the same as ErrorGroup", func(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | bgctx := context.Background() |
| 43 | t.Run("wait returns no error if no errors", func(t *testing.T) { |
| 44 | t.Parallel() |
| 45 | g := NewWithResults[int]().WithContext(bgctx) |
| 46 | g.Go(func(context.Context) (int, error) { return 0, nil }) |
| 47 | res, err := g.Wait() |
| 48 | require.Len(t, res, 1) |
| 49 | require.NoError(t, err) |
| 50 | }) |
| 51 | |
| 52 | t.Run("wait error if func returns error", func(t *testing.T) { |
| 53 | t.Parallel() |
| 54 | g := NewWithResults[int]().WithContext(bgctx) |
| 55 | g.Go(func(context.Context) (int, error) { return 0, err1 }) |
| 56 | res, err := g.Wait() |
| 57 | require.Len(t, res, 0) |
| 58 | require.ErrorIs(t, err, err1) |
| 59 | }) |
| 60 | |
| 61 | t.Run("wait error is all returned errors", func(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | g := NewWithResults[int]().WithErrors().WithContext(bgctx) |
| 64 | g.Go(func(context.Context) (int, error) { return 0, err1 }) |
| 65 | g.Go(func(context.Context) (int, error) { return 0, nil }) |
| 66 | g.Go(func(context.Context) (int, error) { return 0, err2 }) |
| 67 | res, err := g.Wait() |
| 68 | require.Len(t, res, 1) |
| 69 | require.ErrorIs(t, err, err1) |
| 70 | require.ErrorIs(t, err, err2) |
| 71 | }) |
| 72 | }) |
| 73 | |
| 74 | t.Run("context cancel propagates", func(t *testing.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…