-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
145 lines (108 loc) · 3.35 KB
/
group_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package pubsub
import (
"context"
"testing"
"time"
)
func TestGroup(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
group := NewGroup(time.Millisecond)
defer group.Close(time.Second)
done := group.Publish("topic", struct{}{})
waitForEventToBeSent(t, done)
})
}
func TestGroupClose(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
group := NewGroup(time.Millisecond)
group.Close(time.Second)
done := group.Publish("topic", struct{}{})
waitForEventToBeSent(t, done)
})
t.Run("close after subscribe", func(t *testing.T) {
var called bool
group := NewGroup(time.Millisecond)
group.Subscribe("topic", func(topic string, data interface{}) {
called = true
})
err := group.Start(context.TODO())
if expected, actual := true, err == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
group.Close(time.Second)
done := group.Publish("topic", struct{}{})
waitForEventToBeSent(t, done)
if expected, actual := false, called; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
})
}
func TestGroupSubscribe(t *testing.T) {
t.Parallel()
t.Run("one subscriber", func(t *testing.T) {
var called bool
group := NewGroup(time.Millisecond)
defer group.Close(time.Second)
group.Subscribe("topic", func(topic string, data interface{}) {
if expected, actual := "topic", topic; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
if expected, actual := true, data == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
called = true
})
err := group.Start(context.TODO())
if expected, actual := true, err == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
done := group.Publish("topic", nil)
waitForEventToBeSent(t, done)
if expected, actual := true, called; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
})
t.Run("close subscriber", func(t *testing.T) {
var called bool
group := NewGroup(time.Millisecond)
sub := group.Subscribe("topic", func(topic string, data interface{}) {
called = true
})
err := group.Start(context.TODO())
if expected, actual := true, err == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
sub.Close()
done := group.Publish("topic", nil)
waitForEventToBeSent(t, done)
if expected, actual := false, called; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
})
}
func TestGroupUnsubscribe(t *testing.T) {
t.Parallel()
t.Run("unsubscribe", func(t *testing.T) {
var called bool
group := NewGroup(time.Millisecond)
defer group.Close(time.Second)
sub := group.Subscribe("topic", func(topic string, data interface{}) {
called = true
})
err := group.Start(context.TODO())
if expected, actual := true, err == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
err = sub.Unsubscribe()
if expected, actual := true, err == nil; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
done := group.Publish("topic", struct{}{})
waitForEventToBeSent(t, done)
if expected, actual := false, called; expected != actual {
t.Errorf("expected: %v, actual: %v", expected, actual)
}
})
}