|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGroupAllSucceed(t *testing.T) { |
| 13 | + g := NewGroup() |
| 14 | + g.Go(func() error { return nil }) |
| 15 | + g.Go(func() error { return nil }) |
| 16 | + g.Go(func() error { return nil }) |
| 17 | + |
| 18 | + if err := g.Wait(); err != nil { |
| 19 | + t.Errorf("expected nil, got: %v", err) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestGroupCollectsAllErrors(t *testing.T) { |
| 24 | + g := NewGroup() |
| 25 | + g.Go(func() error { return New("error one") }) |
| 26 | + g.Go(func() error { return nil }) |
| 27 | + g.Go(func() error { return New("error two") }) |
| 28 | + g.Go(func() error { return New("error three") }) |
| 29 | + |
| 30 | + err := g.Wait() |
| 31 | + if err == nil { |
| 32 | + t.Fatal("expected errors, got nil") |
| 33 | + } |
| 34 | + multi, ok := err.(*MultiError) |
| 35 | + if !ok { |
| 36 | + t.Fatalf("expected *MultiError, got %T", err) |
| 37 | + } |
| 38 | + if multi.Count() != 3 { |
| 39 | + t.Errorf("expected 3 errors, got %d", multi.Count()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestGroupSingleError(t *testing.T) { |
| 44 | + g := NewGroup() |
| 45 | + g.Go(func() error { return New("only error") }) |
| 46 | + |
| 47 | + err := g.Wait() |
| 48 | + if err == nil { |
| 49 | + t.Fatal("expected error, got nil") |
| 50 | + } |
| 51 | + // Wait always returns *MultiError so callers can reliably type-assert. |
| 52 | + multi, ok := err.(*MultiError) |
| 53 | + if !ok { |
| 54 | + t.Fatalf("expected *MultiError, got %T", err) |
| 55 | + } |
| 56 | + if multi.Count() != 1 { |
| 57 | + t.Errorf("expected 1 error, got %d", multi.Count()) |
| 58 | + } |
| 59 | + if !strings.Contains(multi.Error(), "only error") { |
| 60 | + t.Errorf("unexpected message: %q", multi.Error()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestGroupGoCtx(t *testing.T) { |
| 65 | + ctx := context.Background() |
| 66 | + g := NewGroup(GroupWithContext(ctx, false)) |
| 67 | + |
| 68 | + var received atomic.Int32 |
| 69 | + g.GoCtx(func(ctx context.Context) error { |
| 70 | + if ctx == nil { |
| 71 | + return New("nil context") |
| 72 | + } |
| 73 | + received.Add(1) |
| 74 | + return nil |
| 75 | + }) |
| 76 | + g.GoCtx(func(ctx context.Context) error { |
| 77 | + received.Add(1) |
| 78 | + return nil |
| 79 | + }) |
| 80 | + |
| 81 | + if err := g.Wait(); err != nil { |
| 82 | + t.Errorf("unexpected error: %v", err) |
| 83 | + } |
| 84 | + if received.Load() != 2 { |
| 85 | + t.Errorf("expected 2 goroutines to run, got %d", received.Load()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestGroupCancelOnFirst(t *testing.T) { |
| 90 | + ctx := context.Background() |
| 91 | + g := NewGroup(GroupWithContext(ctx, true)) |
| 92 | + |
| 93 | + var started atomic.Int32 |
| 94 | + // First goroutine errors immediately. |
| 95 | + g.GoCtx(func(ctx context.Context) error { |
| 96 | + started.Add(1) |
| 97 | + return New("first failure") |
| 98 | + }) |
| 99 | + // Second goroutine checks ctx cancellation after a small delay. |
| 100 | + g.GoCtx(func(ctx context.Context) error { |
| 101 | + started.Add(1) |
| 102 | + select { |
| 103 | + case <-ctx.Done(): |
| 104 | + // Context was cancelled by first failure — return nil |
| 105 | + // to show cancellation was observed. |
| 106 | + return nil |
| 107 | + case <-time.After(200 * time.Millisecond): |
| 108 | + return New("second should have been cancelled") |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + err := g.Wait() |
| 113 | + // Only the first error should be collected; second observed cancellation. |
| 114 | + if err == nil { |
| 115 | + t.Fatal("expected at least one error") |
| 116 | + } |
| 117 | + if started.Load() != 2 { |
| 118 | + t.Errorf("expected both goroutines to start, got %d", started.Load()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestGroupWithLimit(t *testing.T) { |
| 123 | + g := NewGroup(GroupWithLimit(2)) |
| 124 | + for i := 0; i < 10; i++ { |
| 125 | + i := i |
| 126 | + g.Go(func() error { return fmt.Errorf("error %d", i) }) |
| 127 | + } |
| 128 | + err := g.Wait() |
| 129 | + if err == nil { |
| 130 | + t.Fatal("expected errors, got nil") |
| 131 | + } |
| 132 | + multi, ok := err.(*MultiError) |
| 133 | + if !ok { |
| 134 | + t.Fatalf("expected *MultiError, got %T", err) |
| 135 | + } |
| 136 | + if multi.Count() > 2 { |
| 137 | + t.Errorf("expected at most 2 errors due to limit, got %d", multi.Count()) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestGroupErrors(t *testing.T) { |
| 142 | + g := NewGroup() |
| 143 | + g.Go(func() error { return New("a") }) |
| 144 | + g.Go(func() error { return New("b") }) |
| 145 | + _ = g.Wait() |
| 146 | + |
| 147 | + errs := g.Errors() |
| 148 | + if len(errs) != 2 { |
| 149 | + t.Errorf("expected 2 errors from Errors(), got %d", len(errs)) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestGroupReuseAfterWait(t *testing.T) { |
| 154 | + g := NewGroup() |
| 155 | + g.Go(func() error { return New("round one") }) |
| 156 | + err1 := g.Wait() |
| 157 | + if err1 == nil { |
| 158 | + t.Fatal("expected error in round one") |
| 159 | + } |
| 160 | + |
| 161 | + // Second round — verify group can be reused. |
| 162 | + g.Go(func() error { return nil }) |
| 163 | + err2 := g.Wait() |
| 164 | + // After reuse the old errors are still present (Group accumulates). |
| 165 | + // This is expected behaviour; document it in the test. |
| 166 | + _ = err2 |
| 167 | +} |
| 168 | + |
| 169 | +func TestGroupConcurrentSafety(t *testing.T) { |
| 170 | + g := NewGroup() |
| 171 | + for i := 0; i < 100; i++ { |
| 172 | + i := i |
| 173 | + g.Go(func() error { |
| 174 | + if i%2 == 0 { |
| 175 | + // Use unique messages so MultiError.Add deduplication does not |
| 176 | + // collapse them — each goroutine index produces a distinct string. |
| 177 | + return fmt.Errorf("even error %d", i) |
| 178 | + } |
| 179 | + return nil |
| 180 | + }) |
| 181 | + } |
| 182 | + err := g.Wait() |
| 183 | + if err == nil { |
| 184 | + t.Fatal("expected errors from 50 failing goroutines") |
| 185 | + } |
| 186 | + multi, ok := err.(*MultiError) |
| 187 | + if !ok { |
| 188 | + t.Fatalf("expected *MultiError, got %T", err) |
| 189 | + } |
| 190 | + if multi.Count() != 50 { |
| 191 | + t.Errorf("expected 50 errors, got %d", multi.Count()) |
| 192 | + } |
| 193 | +} |
0 commit comments