-
Notifications
You must be signed in to change notification settings - Fork 2
/
pipe_test.go
132 lines (110 loc) · 2.47 KB
/
pipe_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
package eventbus
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func pipeHandlerOne(val int) {
}
func pipeHandlerTwo(val int) {
}
func Test_NewPipe(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
assert.NotNil(t, p.stopCh)
assert.NotNil(t, p.handlers)
p.Close()
}
func Test_NewBufferedPipe(t *testing.T) {
p := NewBufferedPipe[int](100)
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
assert.Equal(t, 100, cap(p.channel))
assert.NotNil(t, p.stopCh)
assert.NotNil(t, p.handlers)
p.Close()
pipeZero := NewBufferedPipe[int](0)
assert.NotNil(t, pipeZero)
assert.NotNil(t, pipeZero.channel)
assert.Equal(t, 1, cap(pipeZero.channel))
assert.NotNil(t, pipeZero.stopCh)
assert.NotNil(t, pipeZero.handlers)
pipeZero.Close()
}
func Test_PipeSubscribe(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
err := p.Subscribe(pipeHandlerOne)
assert.Nil(t, err)
p.Close()
err = p.Subscribe(pipeHandlerTwo)
assert.Equal(t, ErrChannelClosed, err)
}
func Test_PipeUnsubscribe(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
err := p.Subscribe(pipeHandlerOne)
assert.Nil(t, err)
err = p.Unsubscribe(pipeHandlerOne)
assert.Nil(t, err)
err = p.Subscribe(pipeHandlerOne)
assert.Nil(t, err)
p.Close()
err = p.Unsubscribe(pipeHandlerOne)
assert.Equal(t, ErrChannelClosed, err)
}
func Test_PipePublish(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
err := p.Subscribe(pipeHandlerOne)
time.Sleep(time.Millisecond)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
err := p.Publish(i)
assert.Nil(t, err)
}
wg.Done()
}()
wg.Wait()
p.Close()
err = p.Publish(1)
assert.Equal(t, ErrChannelClosed, err)
}
func Test_PipePublishSync(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
err := p.Subscribe(pipeHandlerOne)
time.Sleep(time.Millisecond)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
err := p.PublishSync(i)
assert.Nil(t, err)
}
wg.Done()
}()
wg.Wait()
p.Close()
err = p.PublishSync(1)
assert.Equal(t, ErrChannelClosed, err)
}
func Test_PipeClose(t *testing.T) {
p := NewPipe[int]()
assert.NotNil(t, p)
assert.NotNil(t, p.channel)
err := p.Subscribe(pipeHandlerOne)
assert.Nil(t, err)
p.Close()
err = p.Unsubscribe(pipeHandlerOne)
assert.Equal(t, ErrChannelClosed, err)
p.Close()
}