-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub_test.go
65 lines (49 loc) · 1.08 KB
/
pubsub_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
package gopubsub
import (
"sync"
"testing"
"time"
)
func TestBasicOps(t *testing.T) {
ps := New(4)
ps.Close()
ps.Publish("something")
var allGotSomething sync.WaitGroup
var allGotAnother sync.WaitGroup
test := func() {
c := ps.Subscribe(true)
<-c // getting "something" here
allGotSomething.Done()
<-c // getting "another" here
allGotAnother.Done()
}
for i := 0; i < 5; i++ {
allGotSomething.Add(1)
allGotAnother.Add(1)
go test()
}
allGotSomething.Wait()
ps.Publish("another")
allGotAnother.Wait()
}
func TestIncreasedSubscribers(t *testing.T) {
ps := New(2)
defer ps.Close()
count := 5
for i := 0; i < count; i++ {
ps.Subscribe(false)
}
if len(ps.subscribers) != count {
t.Errorf("Expected to have %d subscribers, have %d", count, len(ps.subscribers))
}
}
func TestUnsubscribe(t *testing.T) {
ps := New(2)
defer ps.Close()
c := ps.Subscribe(false)
ps.Unsubscribe(c)
time.Sleep(10 * time.Millisecond) // wait for the op to complete
if len(ps.subscribers) > 0 {
t.Errorf("Expected to have 0 subscribers, have %d", len(ps.subscribers))
}
}