This repository has been archived by the owner on May 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
94 lines (74 loc) · 1.56 KB
/
utils_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
package zmq4chan_test
import (
"bytes"
"testing"
zmqchan "."
)
var (
messages = [][][]byte{
[][]byte{
[]byte("hello"),
[]byte("world"),
},
[][]byte{
[]byte("123"),
[]byte("4567"),
[]byte("89"),
},
}
parts = []zmqchan.Data{
zmqchan.Data{Bytes: []byte("hello"), More: true},
zmqchan.Data{Bytes: []byte("world"), More: false},
zmqchan.Data{Bytes: []byte("123"), More: true},
zmqchan.Data{Bytes: []byte("4567"), More: true},
zmqchan.Data{Bytes: []byte("89"), More: false},
}
)
func TestSendMessageBytes(t *testing.T) {
input := make(chan [][]byte)
output := zmqchan.SendMessageBytes(input)
go func() {
defer close(input)
for _, message := range messages {
input <- message
}
}()
i := 0
for part := range output {
if bytes.Compare(part.Bytes, parts[i].Bytes) != 0 {
t.Errorf("i=%d Bytes mismatch", i)
}
if part.More != parts[i].More {
t.Errorf("i=%d More mismatch", i)
}
i++
}
if i != len(parts) {
t.Error("incorrect number of parts received")
}
}
func TestRecvMessageBytes(t *testing.T) {
output := make(chan [][]byte)
input := zmqchan.RecvMessageBytes(output)
go func() {
defer close(input)
for _, part := range parts {
input <- part
}
}()
i := 0
for message := range output {
if len(message) != len(messages[i]) {
t.Fatalf("i=%d length mismatch", i)
}
for j, part := range message {
if bytes.Compare(part, messages[i][j]) != 0 {
t.Errorf("i=%d part=%d mismatch", i, j)
}
}
i++
}
if i != len(messages) {
t.Error("incorrect number of messages received")
}
}