forked from segmentio/analytics-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
executor_test.go
56 lines (45 loc) · 1.08 KB
/
executor_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
package analytics
import (
"sync"
"testing"
"time"
)
func TestExecutorClose(t *testing.T) {
// Simply make sure that nothing raises a panic nor blocks.
ex := newExecutor(1)
ex.close()
}
func TestExecutorSimple(t *testing.T) {
wg := &sync.WaitGroup{}
ex := newExecutor(1)
defer ex.close()
wg.Add(1)
if !ex.do(wg.Done) {
t.Error("failed pushing a task to an executor with a capacity of 1")
return
}
// Make sure wg.Done gets called, this shouldn't block indefinitely.
wg.Wait()
}
func TestExecutorMulti(t *testing.T) {
wg := &sync.WaitGroup{}
ex := newExecutor(3)
defer ex.close()
// Schedule a couple of tasks to fill the executor.
for i := 0; i != 3; i++ {
wg.Add(1)
if !ex.do(func() {
time.Sleep(10 * time.Millisecond)
wg.Done()
}) {
t.Error("failed pushing a task to an executor with a capacity of 1")
return
}
}
// Make sure the executor refuses more tasks.
if ex.do(func() {}) {
t.Error("the executor should have been full and refused to run more tasks")
}
// Make sure wg.Done gets called, this shouldn't block indefinitely.
wg.Wait()
}