-
Notifications
You must be signed in to change notification settings - Fork 15
/
mux_test.go
179 lines (152 loc) · 2.66 KB
/
mux_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dingo
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
//
// test case
//
func TestMuxDifferentType(t *testing.T) {
var (
err error
ass = assert.New(t)
)
defer func() {
ass.Nil(err)
}()
m := newMux()
remain, err := m.More(3)
ass.Equal(0, remain)
defer m.Close()
// prepare for string channel
cStr := make(chan string, 1)
iStr, err := m.Register(cStr, 0)
if err != nil {
return
}
defer func() {
_, err2 := m.Unregister(iStr)
ass.Nil(err2)
}()
ass.Equal(0, iStr)
// prepare for integer channel
cInt := make(chan int, 1)
iInt, err := m.Register(cInt, 0)
if err != nil {
return
}
defer func() {
_, err2 := m.Unregister(iInt)
ass.Nil(err2)
}()
ass.NotEqual(0, iInt)
// add a handler
oStr := make(chan string, 1)
oInt := make(chan int, 1)
m.Handle(func(val interface{}, idx int) {
switch idx {
case iStr:
oStr <- val.(string)
case iInt:
oInt <- val.(int)
default:
ass.Fail(fmt.Sprintf("unknown index: %v", idx))
}
})
// send a string
cStr <- "test string"
{
v, ok := <-oStr
ass.True(ok)
if ok {
ass.Equal("test string", v)
}
}
// send an integer
cInt <- 55
{
v, ok := <-oInt
ass.True(ok)
if ok {
ass.Equal(55, v)
}
}
close(cStr)
close(cInt)
close(oStr)
close(oInt)
}
func TestMuxChannelClose(t *testing.T) {
var (
err error
ass = assert.New(t)
)
defer func() {
ass.Nil(err)
}()
m := newMux()
remain, err := m.More(1)
ass.Equal(0, remain)
defer m.Close()
ch := make(chan string, 2)
ch <- "test string 1"
ch <- "test string 2"
// close before registering
close(ch)
id, err := m.Register(ch, 0)
seq := 0
m.Handle(func(val interface{}, idx int) {
ass.Equal(id, idx)
switch seq {
case 0:
ass.Equal("test string 1", val.(string))
case 1:
ass.Equal("test string 2", val.(string))
default:
ass.Fail(fmt.Sprintf("unknown index: %v", seq))
}
seq++
})
}
func TestMuxOutputClose(t *testing.T) {
ass := assert.New(t)
m := newMux()
remain, err := m.More(3)
ass.Equal(0, remain)
ass.Nil(err)
ch := make(chan int, 1)
id, err := m.Register(ch, 0)
ass.Nil(err)
o := make(chan int, 1)
m.Handle(func(val interface{}, idx int) {
ass.Equal(id, idx)
o <- int(val.(int))
})
ch <- 66
// close now
m.Close()
v, ok := <-o
ass.True(ok)
if ok {
ass.Equal(66, v)
}
close(ch)
close(o)
}
func TestMuxIdGeneration(t *testing.T) {
ass := assert.New(t)
m := newMux()
remain, err := m.More(3)
ass.Equal(0, remain)
ass.Nil(err)
ch1 := make(chan int, 1)
id, err := m.Register(ch1, 1)
ass.Equal(id, 1)
ass.Nil(err)
// should generate a new id
ch2 := make(chan int, 1)
id, err = m.Register(ch2, 1)
ass.True(id != 1)
ass.Nil(err)
}