-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomb_test.go
131 lines (114 loc) · 2.04 KB
/
tomb_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package tomb
import (
"testing"
"time"
)
func TestTomb_HappyFlow(t1 *testing.T) {
var t Tomb
var b bool
var c bool
err := t.Go(func() {
b = true
<-t.Dying()
})
if err != nil {
t1.Fatal(err)
}
err = t.Go(func() {
c = true
<-t.Dying()
})
if err != nil {
t1.Fatal(err)
}
t.Kill()
err = t.Wait(time.Second)
if err != nil {
t1.Fatal(err)
}
if !b {
t1.Fatalf("goroutine b was not run")
}
if !c {
t1.Fatalf("goroutine c was not run")
}
}
func TestTomb_NoGoroutineStartedBeforeKill(t1 *testing.T) {
var t Tomb
t.Kill()
err := t.Wait(time.Second)
if err != nil {
t1.Fatal(err)
}
}
func TestTomb_StartGoroutineWhileDead(t1 *testing.T) {
var t Tomb
t.Kill()
err := t.Wait(time.Second)
if err != nil {
t1.Fatal(err)
}
err = t.Go(func() {})
if err == nil {
t1.Fatal("expected cannot start goroutine error but got no error")
}
if err != CannotStartDeadError {
t1.Fatal("expected cannot start goroutine error but got: " + err.Error())
}
}
func TestTomb_StartGoroutineWhileDying(t1 *testing.T) {
var t Tomb
waitChan := make(chan int)
err := t.Go(func() {
<-waitChan
})
if err != nil {
t1.Fatal(err)
}
t.Kill()
err = t.Go(func() {})
if err == nil {
t1.Fatal("expected cannot start goroutine error but got no error")
}
if err != CannotStartDeadError {
t1.Fatal("expected cannot start goroutine error but got: " + err.Error())
}
close(waitChan)
err = t.Wait(time.Second)
if err != nil {
t1.Fatal(err)
}
}
func TestTomb_StuckGoroutine(t1 *testing.T) {
var t Tomb
waitChan := make(chan int)
defer close(waitChan)
err := t.Go(func() {
<-waitChan
})
if err != nil {
t1.Fatal(err)
}
t.Kill()
err = t.Wait(time.Second)
if err == nil {
t1.Fatal("expected timeout but got no error")
}
if err != WaitTimeoutError {
t1.Fatal("expected timeout error but got: " + err.Error())
}
}
func TestTomb_PanicInGoFunc(t1 *testing.T) {
var t Tomb
err := t.Go(func() {
panic("some error")
})
if err != nil {
t1.Fatal(err)
}
t.Kill()
err = t.Wait(time.Second)
if err != nil {
t1.Fatal(err)
}
}