-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstopper_test.go
114 lines (99 loc) · 2.22 KB
/
stopper_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
package stop
import (
"testing"
"time"
)
func TestNewChannelStopper(t *testing.T) {
s := NewChannelStopper()
if s == nil {
t.Fatal("s is nil")
}
if s.stop == nil {
t.Errorf("s.stop = nil")
}
if s.stopped == nil {
t.Errorf("s.stopped = nil")
}
if s.isStopped {
t.Error("s.isStopped = true, expected false")
}
if s.isStopping {
t.Error("s.isStopping = true, expected false")
}
}
func TestChannelStopperIsStopped(t *testing.T) {
s := NewChannelStopper()
if s.IsStopped() {
t.Error("s.IsStopped() = true, expected false")
}
s.isStopped = true
if !s.IsStopped() {
t.Error("s.IsStopped() = false, expected true")
}
}
func TestChannelStopperIsStopping(t *testing.T) {
s := NewChannelStopper()
if s.IsStopping() {
t.Error("s.IsStopping() = true, expected false")
}
s.isStopping = true
if !s.IsStopping() {
t.Error("s.IsStopping() = false, expected true")
}
}
func TestChannelStopperStop(t *testing.T) {
s := NewChannelStopper()
if s.isStopping {
t.Error("s.isStopping = true, expected false")
}
s.Stop()
if !s.isStopping {
t.Error("s.isStopping = false, expected true")
}
select {
case <-s.stop:
case <-time.After(1 * time.Second):
t.Error("Stop() did not close stop channel")
}
}
func TestChannelStopperStopChannel(t *testing.T) {
s := NewChannelStopper()
if s.StopChannel() != s.stop {
t.Errorf("s.StopChannel() = %#v, expected %#v", s.StopChannel(), s.stop)
}
}
func TestChannelStopperStopped(t *testing.T) {
s := NewChannelStopper()
if s.isStopped {
t.Errorf("s.isStopped is true, expected false")
}
s.Stopped()
if !s.isStopped {
t.Errorf("s.isStopped is false, expected true")
}
select {
case <-s.stopped:
case <-time.After(1 * time.Second):
t.Error("Stopped() did not close stopped channel")
}
}
func TestChannelStopperStoppedChannel(t *testing.T) {
s := NewChannelStopper()
if s.StoppedChannel() != s.stopped {
t.Errorf("s.StoppedChannel() = %#v, expected %#v", s.StoppedChannel(), s.stopped)
}
}
func TestChannelStopperWaitForStopped(t *testing.T) {
s := NewChannelStopper()
ch := make(chan bool)
go func() {
s.WaitForStopped()
close(ch)
}()
s.Stopped()
select {
case <-ch:
case <-time.After(1 * time.Second):
t.Error("WaitForStopped() didn't return")
}
}