-
Notifications
You must be signed in to change notification settings - Fork 7
/
cwaitgroup_test.go
57 lines (52 loc) · 1.1 KB
/
cwaitgroup_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
package nsync
import (
"sync"
"testing"
"time"
)
func TestCWaitGroup(t *testing.T) {
cwg := NewControlWaitGroup(2)
cwg.Do(func() { time.Sleep(time.Second) })
cwg.Do(func() { time.Sleep(time.Second) })
if cwg.Working() != 2 {
t.Error("Two working thread should be running.")
}
cwg.Wait()
if cwg.Working() != 0 {
t.Error("No workers should be running")
}
cwg.Do(func() { time.Sleep(time.Second) })
cwg.Do(func() { time.Sleep(time.Second) })
go cwg.Do(func() { time.Sleep(time.Second) })
time.Sleep(time.Millisecond * 200)
if cwg.Waiting() != 1 {
t.Error("1 waiting worker should be there")
}
if cwg.Working() != 2 {
t.Error("Two working thread should be running.")
}
cwg.Wait()
if cwg.Working() != 0 {
t.Error("No workers should be running")
}
}
func TestAbort(t *testing.T) {
var mu sync.Mutex
var a int
f := func() {
mu.Lock()
a++
mu.Unlock()
time.Sleep(time.Second)
}
cwg := NewControlWaitGroup(2)
cwg.Do(f)
cwg.Do(f)
go cwg.Do(f)
time.Sleep(time.Millisecond * 200)
cwg.Abort()
cwg.Wait()
if a != 2 {
t.Errorf("Only two jobs should be completed. Actual: %d", a)
}
}