| 22 | ) |
| 23 | |
| 24 | func TestRunGroup(t *testing.T) { |
| 25 | tests := []struct { |
| 26 | name string |
| 27 | fns []func(context.Context) error |
| 28 | expectedErr error |
| 29 | }{ |
| 30 | { |
| 31 | name: "All functions succeed", |
| 32 | fns: []func(context.Context) error{ |
| 33 | func(_ context.Context) error { return nil }, |
| 34 | func(_ context.Context) error { return nil }, |
| 35 | }, |
| 36 | expectedErr: nil, |
| 37 | }, |
| 38 | { |
| 39 | name: "One function fails immediately", |
| 40 | fns: []func(context.Context) error{ |
| 41 | func(_ context.Context) error { return errors.New("fail") }, |
| 42 | func(ctx context.Context) error { |
| 43 | // Simulate work |
| 44 | select { |
| 45 | case <-ctx.Done(): |
| 46 | return ctx.Err() |
| 47 | case <-time.After(100 * time.Millisecond): |
| 48 | return nil |
| 49 | } |
| 50 | }, |
| 51 | }, |
| 52 | expectedErr: errors.New("fail"), |
| 53 | }, |
| 54 | { |
| 55 | name: "Multiple failures return first error", |
| 56 | fns: []func(context.Context) error{ |
| 57 | func(_ context.Context) error { return errors.New("error 1") }, |
| 58 | func(_ context.Context) error { |
| 59 | time.Sleep(10 * time.Millisecond) // Ensure this happens slightly later |
| 60 | |
| 61 | return errors.New("error 2") |
| 62 | }, |
| 63 | }, |
| 64 | expectedErr: errors.New("error 1"), |
| 65 | }, |
| 66 | { |
| 67 | name: "Cancellation propagates", |
| 68 | fns: []func(context.Context) error{ |
| 69 | func(_ context.Context) error { |
| 70 | return errors.New("trigger cancel") |
| 71 | }, |
| 72 | func(ctx context.Context) error { |
| 73 | select { |
| 74 | case <-ctx.Done(): |
| 75 | // Correct behavior: context was cancelled |
| 76 | return nil |
| 77 | case <-time.After(1 * time.Second): |
| 78 | return errors.New("timeout: context was not cancelled") |
| 79 | } |
| 80 | }, |
| 81 | }, |