-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg_test.go
210 lines (191 loc) · 5.69 KB
/
msg_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2020 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zyre
import (
"bytes"
"context"
"testing"
zmq "github.com/go-zeromq/zmq4"
)
func TestNumber(t *testing.T) {
var buffer bytes.Buffer
putNumber1(&buffer, Version)
version := getNumber1(&buffer)
if version != Version {
t.Errorf("want %d, got %d", Version, version)
}
var givenSeq uint16 = 234
putNumber2(&buffer, givenSeq)
seq := getNumber2(&buffer)
if seq != givenSeq {
t.Errorf("want %d, got %d", givenSeq, seq)
}
}
func TestString(t *testing.T) {
var buffer bytes.Buffer
givenStr := "hello"
putString(&buffer, givenStr)
str := getString(&buffer)
if str != givenStr {
t.Errorf("want %s, got %s", givenStr, str)
}
givenStr = "bye"
putLongString(&buffer, givenStr)
str = getLongString(&buffer)
if str != givenStr {
t.Errorf("want %s, got %s", givenStr, str)
}
}
func TestZreMsg(t *testing.T) {
tests := []struct {
name string
id uint8
}{
{name: "Hello", id: HelloID},
{name: "Whisper", id: WhisperID},
{name: "Shout", id: ShoutID},
{name: "Join", id: JoinID},
{name: "Leave", id: LeaveID},
{name: "Ping", id: PingID},
{name: "PingOK", id: PingOkID},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
endpoint := "inproc://selftest-" + tt.name
routingID := "Test"
dealer := zmq.NewDealer(ctx, zmq.WithID(zmq.SocketIdentity(routingID)))
defer dealer.Close()
checkError(dealer.Listen(endpoint), t)
router := zmq.NewRouter(ctx, zmq.WithID(zmq.SocketIdentity("router")))
defer router.Close()
checkError(router.Dial(endpoint), t)
// Create a ZreMsg message and send it through the wire
expectedMsg := NewZreMsg(tt.id)
expectedMsg.routingID = []byte(routingID)
expectedMsg.sequence = 123
expectedMsg.Endpoint = "Endpoint"
expectedMsg.Groups = []string{"Group1: A", "Group2: B"}
expectedMsg.Status = 123
expectedMsg.Name = "Name"
expectedMsg.Headers = map[string]string{"Header1": "1", "Header2": "2"}
expectedMsg.Group = "Group"
expectedMsg.Content = []byte("Some Content")
// dealer sends expectedMsg to router
checkError(Send(dealer, expectedMsg), t)
msg, err := Recv(router)
checkError(err, t)
checkMsg(msg, *expectedMsg, t)
// router sends back received msg to dealer
checkError(Send(router, msg), t)
msg, err = Recv(dealer)
checkError(err, t)
expectedMsg.routingID = []byte{}
checkMsg(msg, *expectedMsg, t)
})
}
}
func TestRecvError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
endpoint := "inproc://selftest-error"
routingID := "Test"
dealer := zmq.NewDealer(ctx, zmq.WithID(zmq.SocketIdentity(routingID)))
defer dealer.Close()
checkError(dealer.Listen(endpoint), t)
router := zmq.NewRouter(ctx, zmq.WithID(zmq.SocketIdentity("router")))
defer router.Close()
checkError(router.Dial(endpoint), t)
// dealer sends Msg with no signature
dealer.Send(zmq.NewMsgString(""))
_, err := Recv(router)
if err == nil {
t.Error("error expected")
}
// dealer sends Msg with incorrect version
var buffer bytes.Buffer
putNumber2(&buffer, Signature)
putNumber1(&buffer, HelloID)
putNumber1(&buffer, 1)
dealer.Send(zmq.NewMsgFrom(buffer.Bytes()))
_, err = Recv(router)
if err == nil {
t.Error("error expected")
}
// socket Recv returns an error
cancel()
_, err = Recv(router)
if err == nil {
t.Error("error expected")
}
}
func checkMsg(msg *ZreMsg, expectedMsg ZreMsg, t *testing.T) {
switch msg.id {
case HelloID:
expectedMsg.id = HelloID
expectedMsg.Group = ""
expectedMsg.Content = []byte{}
checkZreMsg(msg, &expectedMsg, t)
case PingID, PingOkID:
expected := ZreMsg{routingID: expectedMsg.routingID, id: msg.id,
sequence: expectedMsg.sequence}
checkZreMsg(msg, &expected, t)
case JoinID, LeaveID:
expected := ZreMsg{routingID: expectedMsg.routingID, id: msg.id,
sequence: expectedMsg.sequence, Group: expectedMsg.Group,
Status: expectedMsg.Status}
checkZreMsg(msg, &expected, t)
case ShoutID:
expected := ZreMsg{routingID: expectedMsg.routingID, id: msg.id,
sequence: expectedMsg.sequence, Group: expectedMsg.Group,
Content: expectedMsg.Content}
checkZreMsg(msg, &expected, t)
case WhisperID:
expected := ZreMsg{routingID: expectedMsg.routingID, id: msg.id,
sequence: expectedMsg.sequence, Content: expectedMsg.Content}
checkZreMsg(msg, &expected, t)
}
}
func checkError(err error, t *testing.T) {
if err != nil {
t.Error(err)
}
}
func checkZreMsg(msg *ZreMsg, expected *ZreMsg, t *testing.T) {
if string(msg.routingID) != string(expected.routingID) {
t.Errorf("want %s, got %s", expected.routingID, msg.routingID)
}
if msg.id != expected.id {
t.Errorf("want %d, got %d", expected.id, msg.id)
}
if msg.sequence != expected.sequence {
t.Errorf("want %d, got %d", expected.sequence, msg.sequence)
}
if msg.Endpoint != expected.Endpoint {
t.Errorf("want %s, got %s", expected.Endpoint, msg.Endpoint)
}
for idx, str := range expected.Groups {
if msg.Groups[idx] != str {
t.Errorf("want %s, got %s", str, msg.Groups[idx])
}
}
if msg.Status != expected.Status {
t.Errorf("want %d, got %d", expected.Status, msg.Status)
}
if msg.Name != expected.Name {
t.Errorf("want %s, got %s", expected.Name, msg.Name)
}
if msg.Group != expected.Group {
t.Errorf("want %s, got %s", expected.Group, msg.Group)
}
if string(msg.Content) != string(expected.Content) {
t.Errorf("want %s, got %s", expected.Content, msg.Content)
}
for key, val := range expected.Headers {
if msg.Headers[key] != val {
t.Errorf("want %s, got %s", val, msg.Headers[key])
}
}
}