(t *testing.T)
| 29 | } |
| 30 | |
| 31 | func TestResultGroup(t *testing.T) { |
| 32 | t.Parallel() |
| 33 | |
| 34 | t.Run("panics on configuration after init", func(t *testing.T) { |
| 35 | t.Run("before wait", func(t *testing.T) { |
| 36 | t.Parallel() |
| 37 | g := NewWithResults[int]() |
| 38 | g.Go(func() int { return 0 }) |
| 39 | require.Panics(t, func() { g.WithMaxGoroutines(10) }) |
| 40 | }) |
| 41 | |
| 42 | t.Run("after wait", func(t *testing.T) { |
| 43 | t.Parallel() |
| 44 | g := NewWithResults[int]() |
| 45 | g.Go(func() int { return 0 }) |
| 46 | require.Panics(t, func() { g.WithMaxGoroutines(10) }) |
| 47 | }) |
| 48 | }) |
| 49 | |
| 50 | t.Run("basic", func(t *testing.T) { |
| 51 | t.Parallel() |
| 52 | g := NewWithResults[int]() |
| 53 | expected := []int{} |
| 54 | for i := 0; i < 100; i++ { |
| 55 | i := i |
| 56 | expected = append(expected, i) |
| 57 | g.Go(func() int { |
| 58 | return i |
| 59 | }) |
| 60 | } |
| 61 | res := g.Wait() |
| 62 | sort.Ints(res) |
| 63 | require.Equal(t, expected, res) |
| 64 | }) |
| 65 | |
| 66 | t.Run("limit", func(t *testing.T) { |
| 67 | t.Parallel() |
| 68 | for _, maxGoroutines := range []int{1, 10, 100} { |
| 69 | t.Run(strconv.Itoa(maxGoroutines), func(t *testing.T) { |
| 70 | g := NewWithResults[int]().WithMaxGoroutines(maxGoroutines) |
| 71 | |
| 72 | var currentConcurrent atomic.Int64 |
| 73 | var errCount atomic.Int64 |
| 74 | taskCount := maxGoroutines * 10 |
| 75 | expected := make([]int, taskCount) |
| 76 | for i := 0; i < taskCount; i++ { |
| 77 | i := i |
| 78 | expected[i] = i |
| 79 | g.Go(func() int { |
| 80 | cur := currentConcurrent.Add(1) |
| 81 | if cur > int64(maxGoroutines) { |
| 82 | errCount.Add(1) |
| 83 | } |
| 84 | time.Sleep(time.Millisecond) |
| 85 | currentConcurrent.Add(-1) |
| 86 | return i |
| 87 | }) |
| 88 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…