forked from alitto/pond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pond_test.go
75 lines (56 loc) · 1.74 KB
/
pond_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pond
import (
"sync/atomic"
"testing"
"time"
)
func assertEqual(t *testing.T, expected interface{}, actual interface{}) {
if expected != actual {
t.Helper()
t.Errorf("Expected %T(%v) but was %T(%v)", expected, expected, actual, actual)
}
}
func TestNew(t *testing.T) {
pool := New(17, 10, MinWorkers(2), IdleTimeout(1*time.Second))
assertEqual(t, 17, pool.maxWorkers)
assertEqual(t, 10, pool.maxCapacity)
assertEqual(t, 2, pool.minWorkers)
assertEqual(t, 1*time.Second, pool.idleTimeout)
}
func TestNewWithInconsistentOptions(t *testing.T) {
pool := New(-10, -5, MinWorkers(20), IdleTimeout(-1*time.Second))
assertEqual(t, 1, pool.maxWorkers)
assertEqual(t, 0, pool.maxCapacity)
assertEqual(t, 1, pool.minWorkers)
assertEqual(t, defaultIdleTimeout, pool.idleTimeout)
}
func TestPurgeAfterPoolStopped(t *testing.T) {
pool := New(1, 1)
var doneCount int32
pool.SubmitAndWait(func() {
atomic.AddInt32(&doneCount, 1)
})
assertEqual(t, int32(1), atomic.LoadInt32(&doneCount))
assertEqual(t, 1, pool.RunningWorkers())
// Simulate purger goroutine attempting to stop a worker after tasks channel is closed
atomic.StoreInt32(&pool.stopped, 1)
pool.maybeStopIdleWorker()
}
// See: https://github.com/alitto/pond/issues/33
func TestPurgeDuringSubmit(t *testing.T) {
pool := New(1, 1)
var doneCount int32
// Submit a task to ensure at least 1 worker is started
pool.SubmitAndWait(func() {
atomic.AddInt32(&doneCount, 1)
})
assertEqual(t, 1, pool.IdleWorkers())
// Stop an idle worker right before submitting another task
pool.maybeStopIdleWorker()
pool.Submit(func() {
atomic.AddInt32(&doneCount, 1)
})
pool.StopAndWait()
assertEqual(t, int32(2), atomic.LoadInt32(&doneCount))
assertEqual(t, 0, pool.RunningWorkers())
}