-
Notifications
You must be signed in to change notification settings - Fork 290
/
main_test.go
59 lines (45 loc) · 1.11 KB
/
main_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
package main
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
func TestSubscriber(t *testing.T) {
ctx := context.Background()
h := newHub()
sub01 := newSubscriber("sub01")
sub02 := newSubscriber("sub02")
sub03 := newSubscriber("sub03")
h.subscribe(ctx, sub01)
h.subscribe(ctx, sub02)
h.subscribe(ctx, sub03)
assert.Equal(t, 3, h.subscribers())
h.unsubscribe(ctx, sub01)
h.unsubscribe(ctx, sub02)
h.unsubscribe(ctx, sub03)
assert.Equal(t, 0, h.subscribers())
}
func TestCancelSubscriber(t *testing.T) {
ctx := context.Background()
h := newHub()
sub01 := newSubscriber("sub01")
sub02 := newSubscriber("sub02")
sub03 := newSubscriber("sub03")
h.subscribe(ctx, sub01)
h.subscribe(ctx, sub02)
ctx03, cancel := context.WithCancel(ctx)
h.subscribe(ctx03, sub03)
assert.Equal(t, 3, h.subscribers())
// cancel subscriber 03
cancel()
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 2, h.subscribers())
h.unsubscribe(ctx, sub01)
h.unsubscribe(ctx, sub02)
assert.Equal(t, 0, h.subscribers())
}