forked from jaihind213/go-demux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux_test.go
153 lines (131 loc) · 3.91 KB
/
demux_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
package go_demux
import (
"fmt"
"testing"
)
func TestChannelDemux_UsingMurmur(t *testing.T) {
const numChnls = 3
////prepare test
outputChannels := make(map[OutputLineId]chan Signal, numChnls)
for i := 0; i < numChnls; i++ {
outputChannels[OutputLineId(i)] = make(chan Signal, 5)
}
//set up
dmux := ChannelDemux{
OutputChannels: outputChannels,
Selector: NewMurmurKeyBasedDemuxFunc(numChnls),
}
////test cases
type test struct {
input []byte
lineExpected int
errExpected bool
}
tests := []test{
{[]byte("1647fec8408-39c40000010a443b"), 0, false},
{[]byte("1647fec8408-39c40000010a443b"), 0, false},
{[]byte("1647ee3d0a6-58030000010f610d"), 0, false},
{[]byte("1647ea7b83a-5ec60000010e2cbf"), 1, false},
}
//prepare expected results from test input above.
xpectedCountPerOutputLine := make(map[OutputLineId]int) // key is output line id , value is count
for _, tc := range tests {
if tc.errExpected {
continue
}
if cnt, ok := xpectedCountPerOutputLine[OutputLineId(tc.lineExpected)]; !ok {
xpectedCountPerOutputLine[OutputLineId(tc.lineExpected)] = 1
} else {
xpectedCountPerOutputLine[OutputLineId(tc.lineExpected)] = cnt + 1
}
}
////do test
for _, tc := range tests {
got, err := dmux.Demux(tc.input)
if !tc.errExpected && err != nil {
t.Errorf("did not expect error for tc %v bot got %v", tc.input, err)
}
if err != nil {
fmt.Println(err)
}
if got != tc.lineExpected {
t.Errorf("expected output region chanl %v, got %v", tc.lineExpected, got)
}
}
//check num of signals received per channel
for outputLineId, ch := range outputChannels {
if xpected, ok := xpectedCountPerOutputLine[outputLineId]; !ok {
//but if
if len(outputChannels[outputLineId]) > 0 {
t.Errorf("expected 0 elements for outputline %v but got %v", outputLineId, len(outputChannels[outputLineId]))
}
} else {
if xpected != len(ch) {
t.Errorf("expected %v elements in output line for outputline %v, got %v", xpected, outputLineId, len(ch))
}
}
}
}
func TestChannelDemux_WithNoChannls(t *testing.T) {
dmux := ChannelDemux{
OutputChannels: nil,
Selector: NewRandomDemuxFunc(2),
}
id, err := dmux.Demux("abc")
if err == nil {
t.Errorf("expected error (but got none) while demuxing with 0 output lines but got none")
}
if id >= 0 {
t.Errorf("no output lines were set, but demux chose a valid output line with id %v", id)
}
}
func TestChannelDemux_WithInvalidChannel(t *testing.T) {
outputChannels := make(map[OutputLineId]chan Signal, 1)
outputChannels[0] = make(chan Signal, 1)
dmux := ChannelDemux{
OutputChannels: outputChannels,
Selector: func(inputSignal Signal) (outputLineId int, err error) {
return -2, nil //return some id not in output channels
},
}
id, err := dmux.Demux("abc")
if err == nil {
t.Errorf("expected error (but got none) while demuxing , as configured selector returned a channel id does not exist")
}
if id >= 0 {
t.Errorf("configured selector always returns an invalid output line id, but we got a valid one : %v", id)
}
}
func TestChannelDemux_WithNoSelector(t *testing.T) {
outputChannels := make(map[OutputLineId]chan Signal, 1)
outputChannels[0] = make(chan Signal, 1)
dmux := ChannelDemux{
OutputChannels: outputChannels,
Selector: nil,
}
id, err := dmux.Demux("abc")
if err == nil {
t.Errorf("expected error as selector function was not set, but gone.")
}
if id >= 0 {
t.Errorf("selector not set but we got a valid output line id : %v", id)
}
}
type blackHole struct {
}
func (b blackHole) Add(signal interface{}) {
fmt.Println("blackhole got", signal)
}
func ExampleGenericDemux() {
numLines := 2
outputLines := make(map[OutputLineId]Adder, numLines)
for i := 0; i < numLines; i++ {
outputLines[OutputLineId(i)] = blackHole{}
}
dmux := GenericDemux{
OutputLines: outputLines,
Selector: NewRandomDemuxFunc(numLines),
}
_, _ = dmux.Demux("abc")
// Output: blackhole got abc
}