forked from travis-ci/worker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcanceller_test.go
45 lines (37 loc) · 1012 Bytes
/
canceller_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
package worker
import "testing"
func TestCancellationBroadcaster(t *testing.T) {
cb := NewCancellationBroadcaster()
ch1_1 := cb.Subscribe(1)
ch1_2 := cb.Subscribe(1)
ch1_3 := cb.Subscribe(1)
ch2 := cb.Subscribe(2)
cb.Unsubscribe(1, ch1_2)
cb.Broadcast(1)
cb.Broadcast(1)
assertClosed(t, "ch1_1", ch1_1)
assertWaiting(t, "ch1_2", ch1_2)
assertClosed(t, "ch1_3", ch1_3)
assertWaiting(t, "ch2", ch2)
}
func assertClosed(t *testing.T, name string, ch <-chan struct{}) {
select {
case _, ok := (<-ch):
if ok {
t.Errorf("expected %s to be closed, but it received a value", name)
}
default:
t.Errorf("expected %s to be closed, but it wasn't", name)
}
}
func assertWaiting(t *testing.T, name string, ch <-chan struct{}) {
select {
case _, ok := (<-ch):
if ok {
t.Errorf("expected %s to be not be closed and not have a value, but it received a value", name)
} else {
t.Errorf("expected %s to be not be closed and not have a value, but it was closed", name)
}
default:
}
}