MCPcopy Index your code
hub / github.com/sourcegraph/conc / TestPool

Function TestPool

pool/pool_test.go:29–125  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

27}
28
29func TestPool(t *testing.T) {
30 t.Parallel()
31
32 t.Run("basic", func(t *testing.T) {
33 t.Parallel()
34
35 g := New()
36 var completed atomic.Int64
37 for i := 0; i < 100; i++ {
38 g.Go(func() {
39 time.Sleep(1 * time.Millisecond)
40 completed.Add(1)
41 })
42 }
43 g.Wait()
44 require.Equal(t, completed.Load(), int64(100))
45 })
46
47 t.Run("panics on configuration after init", func(t *testing.T) {
48 t.Run("before wait", func(t *testing.T) {
49 t.Parallel()
50 g := New()
51 g.Go(func() {})
52 require.Panics(t, func() { g.WithMaxGoroutines(10) })
53 })
54
55 t.Run("after wait", func(t *testing.T) {
56 t.Parallel()
57 g := New()
58 g.Go(func() {})
59 g.Wait()
60 require.Panics(t, func() { g.WithMaxGoroutines(10) })
61 })
62 })
63
64 t.Run("limit", func(t *testing.T) {
65 t.Parallel()
66 for _, maxConcurrent := range []int{1, 10, 100} {
67 t.Run(strconv.Itoa(maxConcurrent), func(t *testing.T) {
68 g := New().WithMaxGoroutines(maxConcurrent)
69
70 var currentConcurrent atomic.Int64
71 var errCount atomic.Int64
72 taskCount := maxConcurrent * 10
73 for i := 0; i < taskCount; i++ {
74 g.Go(func() {
75 cur := currentConcurrent.Add(1)
76 if cur > int64(maxConcurrent) {
77 errCount.Add(1)
78 }
79 time.Sleep(time.Millisecond)
80 currentConcurrent.Add(-1)
81 })
82 }
83 g.Wait()
84 require.Equal(t, int64(0), errCount.Load())
85 require.Equal(t, int64(0), currentConcurrent.Load())
86 })

Callers

nothing calls this directly

Calls 5

NewFunction · 0.70
GoMethod · 0.45
WaitMethod · 0.45
WithMaxGoroutinesMethod · 0.45
MaxGoroutinesMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…