| 118 | } |
| 119 | |
| 120 | func TestTimeoutNoGoroutineLeak(t *testing.T) { |
| 121 | initialWorkers := 10 |
| 122 | extraWorkers := 1000 |
| 123 | |
| 124 | pool := workerpool.New(initialWorkers, 2) |
| 125 | |
| 126 | before := runtime.NumGoroutine() |
| 127 | |
| 128 | pool.Expand(extraWorkers, time.Millisecond*100, nil) |
| 129 | |
| 130 | go func() { // A |
| 131 | for i := 0; i < extraWorkers; i++ { |
| 132 | assert.True(t, pool.Queue(func() { |
| 133 | time.Sleep(time.Millisecond * 10) |
| 134 | }, 0)) |
| 135 | } |
| 136 | }() |
| 137 | |
| 138 | <-time.After(time.Millisecond * 500) |
| 139 | go func() { |
| 140 | for i := 0; i < initialWorkers*2; i++ { |
| 141 | assert.True(t, pool.Queue(func() { |
| 142 | time.Sleep(time.Millisecond * 10) |
| 143 | }, 0)) |
| 144 | } |
| 145 | }() |
| 146 | |
| 147 | <-time.After(time.Millisecond * 500) |
| 148 | after := runtime.NumGoroutine() |
| 149 | if (after - before) > (initialWorkers * 2) { |
| 150 | t.Fatal() |
| 151 | } |
| 152 | |
| 153 | assert.NoError(t, waitFunc(pool.Stop, _timeout)) |
| 154 | } |
| 155 | |
| 156 | func ExampleWorkerPool() { |
| 157 | pool := workerpool.New(-1, 0) |