|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ghosind/go-assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestParallel(t *testing.T) { |
| 13 | + a := assert.New(t) |
| 14 | + |
| 15 | + index, err := Parallel(0) |
| 16 | + a.NilNow(err) |
| 17 | + a.EqualNow(index, -1) |
| 18 | + |
| 19 | + a.PanicNow(func() { |
| 20 | + Parallel(-1) |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +func TestParallelWithoutConcurrencyLimit(t *testing.T) { |
| 25 | + a := assert.New(t) |
| 26 | + |
| 27 | + funcs := make([]AsyncFn, 0, 5) |
| 28 | + for i := 0; i < 5; i++ { |
| 29 | + funcs = append(funcs, func(ctx context.Context) error { |
| 30 | + time.Sleep(100 * time.Millisecond) |
| 31 | + return nil |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + start := time.Now() |
| 36 | + index, err := Parallel(0, funcs...) |
| 37 | + dur := time.Since(start) |
| 38 | + a.NilNow(err) |
| 39 | + a.EqualNow(index, -1) |
| 40 | + a.TrueNow(dur-100*time.Millisecond < 5*time.Millisecond) // allow 5ms deviation |
| 41 | +} |
| 42 | + |
| 43 | +func TestParallelWithConcurrencyLimit(t *testing.T) { |
| 44 | + a := assert.New(t) |
| 45 | + |
| 46 | + funcs := make([]AsyncFn, 0, 5) |
| 47 | + for i := 0; i < 5; i++ { |
| 48 | + funcs = append(funcs, func(ctx context.Context) error { |
| 49 | + time.Sleep(100 * time.Millisecond) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + start := time.Now() |
| 55 | + index, err := Parallel(2, funcs...) |
| 56 | + dur := time.Since(start) |
| 57 | + a.NilNow(err) |
| 58 | + a.EqualNow(index, -1) |
| 59 | + a.TrueNow(dur-300*time.Millisecond < 10*time.Millisecond) // allow 10ms deviation |
| 60 | +} |
| 61 | + |
| 62 | +func TestParallelWithFailedTask(t *testing.T) { |
| 63 | + a := assert.New(t) |
| 64 | + |
| 65 | + expectedErr := errors.New("expected error") |
| 66 | + |
| 67 | + funcs := make([]AsyncFn, 0, 5) |
| 68 | + for i := 0; i < 5; i++ { |
| 69 | + n := i |
| 70 | + funcs = append(funcs, func(ctx context.Context) error { |
| 71 | + time.Sleep(100 * time.Millisecond) |
| 72 | + if n == 2 { |
| 73 | + return expectedErr |
| 74 | + } |
| 75 | + return nil |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + index, err := Parallel(2, funcs...) |
| 80 | + a.EqualNow(err, expectedErr) |
| 81 | + a.EqualNow(index, 2) |
| 82 | +} |
| 83 | + |
| 84 | +func TestParallelWithContext(t *testing.T) { |
| 85 | + a := assert.New(t) |
| 86 | + |
| 87 | + funcs := make([]AsyncFn, 0, 5) |
| 88 | + res := make([]bool, 5) |
| 89 | + for i := 0; i < 5; i++ { |
| 90 | + n := i |
| 91 | + funcs = append(funcs, func(ctx context.Context) error { |
| 92 | + time.Sleep(100 * time.Millisecond) |
| 93 | + res[n] = true |
| 94 | + return nil |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + index, err := ParallelWithContext(context.Background(), 2, funcs...) |
| 99 | + a.NilNow(err) |
| 100 | + a.EqualNow(index, -1) |
| 101 | + |
| 102 | + finishedNum := 0 |
| 103 | + for _, v := range res { |
| 104 | + if v { |
| 105 | + finishedNum++ |
| 106 | + } |
| 107 | + } |
| 108 | + a.EqualNow(finishedNum, 5) |
| 109 | +} |
| 110 | + |
| 111 | +func TestParallelWithTimedOutContext(t *testing.T) { |
| 112 | + a := assert.New(t) |
| 113 | + |
| 114 | + funcs := make([]AsyncFn, 0, 5) |
| 115 | + res := make([]bool, 5) |
| 116 | + for i := 0; i < 5; i++ { |
| 117 | + n := i |
| 118 | + funcs = append(funcs, func(ctx context.Context) error { |
| 119 | + time.Sleep(100 * time.Millisecond) |
| 120 | + res[n] = true |
| 121 | + return nil |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + ctx, canFunc := context.WithTimeout(context.Background(), 150*time.Millisecond) |
| 126 | + defer canFunc() |
| 127 | + |
| 128 | + index, err := ParallelWithContext(ctx, 2, funcs...) |
| 129 | + a.TrueNow(errors.Is(err, ErrContextCanceled)) |
| 130 | + a.EqualNow(index, -1) |
| 131 | + |
| 132 | + finishedNum := 0 |
| 133 | + for _, v := range res { |
| 134 | + if v { |
| 135 | + finishedNum++ |
| 136 | + } |
| 137 | + } |
| 138 | + a.EqualNow(finishedNum, 2) |
| 139 | +} |
0 commit comments