(t *testing.T)
| 33 | } |
| 34 | |
| 35 | func TestStream(t *testing.T) { |
| 36 | t.Parallel() |
| 37 | |
| 38 | t.Run("simple", func(t *testing.T) { |
| 39 | t.Parallel() |
| 40 | s := New() |
| 41 | var res []int |
| 42 | for i := 0; i < 5; i++ { |
| 43 | i := i |
| 44 | s.Go(func() Callback { |
| 45 | i *= 2 |
| 46 | return func() { |
| 47 | res = append(res, i) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | s.Wait() |
| 52 | require.Equal(t, []int{0, 2, 4, 6, 8}, res) |
| 53 | }) |
| 54 | |
| 55 | t.Run("max goroutines", func(t *testing.T) { |
| 56 | t.Parallel() |
| 57 | s := New().WithMaxGoroutines(5) |
| 58 | var currentTaskCount atomic.Int64 |
| 59 | var currentCallbackCount atomic.Int64 |
| 60 | for i := 0; i < 50; i++ { |
| 61 | s.Go(func() Callback { |
| 62 | curr := currentTaskCount.Add(1) |
| 63 | if curr > 5 { |
| 64 | t.Fatal("too many concurrent tasks being executed") |
| 65 | } |
| 66 | defer currentTaskCount.Add(-1) |
| 67 | |
| 68 | time.Sleep(time.Millisecond) |
| 69 | |
| 70 | return func() { |
| 71 | curr := currentCallbackCount.Add(1) |
| 72 | if curr > 1 { |
| 73 | t.Fatal("too many concurrent callbacks being executed") |
| 74 | } |
| 75 | |
| 76 | time.Sleep(time.Millisecond) |
| 77 | |
| 78 | defer currentCallbackCount.Add(-1) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | s.Wait() |
| 83 | }) |
| 84 | |
| 85 | t.Run("panic in task is propagated", func(t *testing.T) { |
| 86 | t.Parallel() |
| 87 | s := New().WithMaxGoroutines(5) |
| 88 | s.Go(func() Callback { |
| 89 | panic("something really bad happened in the task") |
| 90 | }) |
| 91 | require.Panics(t, s.Wait) |
| 92 | }) |
nothing calls this directly
no test coverage detected
searching dependent graphs…