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
/
io_test.go
274 lines (212 loc) · 4.13 KB
/
io_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package zmq4chan_test
import (
"fmt"
"strconv"
"testing"
zmq "github.com/pebbe/zmq4"
zmqchan "."
)
var io = zmqchan.NewIO()
func TestIO(t *testing.T) {
const (
addr1 = "tcp://127.0.0.1:12345"
addr2 = "tcp://127.0.0.1:12346"
)
ctx, err := zmq.NewContext()
if err != nil {
t.Fatal(err)
}
defer ctx.Term()
go func() {
s, err := ctx.NewSocket(zmq.REQ)
if err != nil {
t.Fatal(err)
}
defer s.Close()
if err := s.Connect(addr1); err != nil {
t.Fatal(err)
}
if err := s.Connect(addr2); err != nil {
t.Fatal(err)
}
for {
t.Logf("0 sending")
if _, err := s.SendMessage("hello", "world"); err != nil {
t.Logf("0 %s", err)
return
}
t.Logf("0 receiving")
msg, err := s.RecvMessage(0)
if err != nil {
t.Logf("0 %s", err)
return
}
t.Logf("0 %s", msg)
}
}()
ctx1, err := zmq.NewContext()
if err != nil {
t.Fatal(err)
}
defer ctx1.Term()
ctx2, err := zmq.NewContext()
if err != nil {
t.Fatal(err)
}
defer ctx2.Term()
s1, err := ctx1.NewSocket(zmq.REP)
if err != nil {
t.Fatal(err)
}
defer io.Remove(s1)
s2, err := ctx2.NewSocket(zmq.REP)
if err != nil {
t.Fatal(err)
}
defer io.Remove(s2)
if err := s1.Bind(addr1); err != nil {
t.Fatal(err)
}
if err := s2.Bind(addr2); err != nil {
t.Fatal(err)
}
send1 := make(chan zmqchan.Data)
recv1 := make(chan zmqchan.Data)
if err := io.Add(s1, send1, recv1); err != nil {
t.Fatal(err)
}
send2 := make(chan zmqchan.Data)
recv2 := make(chan zmqchan.Data)
if err := io.Add(s2, send2, recv2); err != nil {
t.Fatal(err)
}
var (
send1buf zmqchan.Data
send2buf zmqchan.Data
recv1count = 0
recv2count = 0
)
for recv1 != nil || send1 != nil || recv2 != nil || send2 != nil {
var (
recv1chan <-chan zmqchan.Data
recv2chan <-chan zmqchan.Data
send1chan chan<- zmqchan.Data
send2chan chan<- zmqchan.Data
)
if send1buf.Bytes == nil {
recv1chan = recv1
} else {
send1chan = send1
}
if send2buf.Bytes == nil {
recv2chan = recv2
} else {
send2chan = send2
}
select {
case data, ok := <-recv1chan:
if !ok {
t.Fatal("1 closed")
}
t.Logf("1 '%s'", data)
if !data.More {
if recv1count++; recv1count == 10 {
t.Logf("1 no more")
recv1 = nil
}
send1buf.Bytes = []byte("okie")
}
case send1chan <- send1buf:
send1buf.Bytes = nil
if recv1 == nil {
close(send1)
send1 = nil
}
case b, ok := <-recv2chan:
if !ok {
t.Fatal("2 closed")
}
t.Logf("2 '%s'", b)
if !b.More {
if recv2count++; recv2count == 10 {
t.Logf("2 no more")
recv2 = nil
}
send2buf.Bytes = []byte("dokie")
}
case send2chan <- send2buf:
send2buf.Bytes = nil
if recv2 == nil {
close(send2)
send2 = nil
}
}
}
}
func TestLotsOfIO(t *testing.T) {
const (
numSockets = 503
)
defer zmq.Term()
done := make(chan struct{})
for i := 0; i < numSockets; i++ {
addr := fmt.Sprintf("inproc://%d", i)
numMessages := i * 11
if i&1 == 1 {
numMessages -= 1317
}
go func(addr string, numMessages int) {
defer func() {
done <- struct{}{}
}()
s, err := zmq.NewSocket(zmq.PULL)
if err != nil {
t.Fatal(err)
}
defer io.Remove(s)
if err := s.Bind(addr); err != nil {
t.Fatal(err)
}
c := make(chan zmqchan.Data)
if err := io.Add(s, nil, c); err != nil {
t.Fatal(err)
}
for n := 0; n < numMessages; n++ {
m := <-c
ms, err := strconv.Atoi(m.String())
if err != nil {
t.Fatal(err)
}
if ms != n {
t.Fatalf("%d: %d != %d", i, ms, n)
}
}
}(addr, numMessages)
go func(addr string, numMessages int) {
defer func() {
done <- struct{}{}
}()
s, err := zmq.NewSocket(zmq.PUSH)
if err != nil {
t.Fatal(err)
}
defer io.Remove(s)
if err := s.Connect(addr); err != nil {
t.Fatal(err)
}
c := make(chan zmqchan.Data)
defer close(c)
if err := io.Add(s, c, nil); err != nil {
t.Fatal(err)
}
for n := 0; n < numMessages; n++ {
c <- zmqchan.Data{
Bytes: []byte(strconv.Itoa(n)),
}
}
}(addr, numMessages)
}
for i := 0; i < numSockets; i++ {
<-done
}
}