-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathsocket_spec.rb
564 lines (447 loc) · 17.3 KB
/
socket_spec.rb
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
require File.join(File.dirname(__FILE__), %w[spec_helper])
module ZMQ
describe Socket do
include APIHelper
socket_types =
[ZMQ::REQ, ZMQ::REP, ZMQ::DEALER, ZMQ::ROUTER, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR, ZMQ::XPUB, ZMQ::XSUB]
context "when initializing" do
before(:all) { @ctx = Context.new }
after(:all) { @ctx.terminate }
it "should raise an error for a nil context" do
expect { Socket.new(FFI::Pointer.new(0), ZMQ::REQ) }.to raise_exception(ZMQ::ContextError)
end
it "works with a Context#pointer as the context_ptr" do
expect do
s = Socket.new(@ctx.pointer, ZMQ::REQ)
s.close
end.not_to raise_exception
end
it "works with a Context instance as the context_ptr" do
expect do
s = Socket.new(@ctx, ZMQ::SUB)
s.close
end.not_to raise_exception
end
socket_types.each do |socket_type|
it "should not raise an error for a [#{ZMQ::SocketTypeNameMap[socket_type]}] socket type" do
sock = nil
expect { sock = Socket.new(@ctx.pointer, socket_type) }.not_to raise_error
sock.close
end
end # each socket_type
it "should set the :socket accessor to the raw socket allocated by libzmq" do
socket = double('socket')
allow(socket).to receive(:null?).and_return(false)
expect(LibZMQ).to receive(:zmq_socket).and_return(socket)
sock = Socket.new(@ctx.pointer, ZMQ::REQ)
expect(sock.socket).to eq(socket)
end
it "should define a finalizer on this object" do
expect(ObjectSpace).to receive(:define_finalizer).at_least(1)
sock = Socket.new(@ctx.pointer, ZMQ::REQ)
sock.close
end
unless jruby?
it "should track pid in finalizer so subsequent fork will not segfault" do
sock = Socket.new(@ctx.pointer, ZMQ::REQ)
pid = fork { }
Process.wait(pid)
sock.close
end
end
end # context initializing
context "calling close" do
before(:all) { @ctx = Context.new }
after(:all) { @ctx.terminate }
it "should call LibZMQ.close only once" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
raw_socket = sock.socket
expect(LibZMQ).to receive(:close).with(raw_socket)
sock.close
sock.close
LibZMQ.close raw_socket # *really close it otherwise the context will block indefinitely
end
end # context calling close
context "identity=" do
before(:all) { @ctx = Context.new }
after(:all) { @ctx.terminate }
it "fails to set identity for identities in excess of 255 bytes" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
sock.identity = ('a' * 256)
expect(sock.identity).to eq('')
sock.close
end
it "fails to set identity for identities of length 0" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
sock.identity = ''
expect(sock.identity).to eq('')
sock.close
end
it "sets the identity for identities of 1 byte" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
sock.identity = 'a'
expect(sock.identity).to eq('a')
sock.close
end
it "set the identity identities of 255 bytes" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
sock.identity = ('a' * 255)
expect(sock.identity).to eq('a' * 255)
sock.close
end
it "should convert numeric identities to strings" do
sock = Socket.new @ctx.pointer, ZMQ::REQ
sock.identity = 7
expect(sock.identity).to eq('7')
sock.close
end
end # context identity=
socket_types.each do |socket_type|
context "#setsockopt for a #{ZMQ::SocketTypeNameMap[socket_type]} socket" do
before(:all) { @ctx = Context.new }
after(:all) { @ctx.terminate }
let(:socket) do
Socket.new @ctx.pointer, socket_type
end
after(:each) do
socket.close
end
context "using option ZMQ::IDENTITY" do
it "should set the identity given any string under 255 characters" do
length = 4
(1..255).each do |length|
identity = 'a' * length
socket.setsockopt ZMQ::IDENTITY, identity
array = []
rc = socket.getsockopt(ZMQ::IDENTITY, array)
expect(rc).to eq(0)
expect(array[0]).to eq(identity)
end
end
it "returns -1 given a string 256 characters or longer" do
identity = 'a' * 256
array = []
rc = socket.setsockopt(ZMQ::IDENTITY, identity)
expect(rc).to eq(-1)
end
end # context using option ZMQ::IDENTITY
context "using option ZMQ::IPV4ONLY" do
it "should enable use of IPV6 sockets when set to 0" do
value = 0
socket.setsockopt ZMQ::IPV4ONLY, value
array = []
rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "should default to a value of 1" do
value = 1
array = []
rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "returns -1 given a negative value" do
value = -1
rc = socket.setsockopt ZMQ::IPV4ONLY, value
expect(rc).to eq(-1)
end
it "returns -1 given a value > 1" do
value = 2
rc = socket.setsockopt ZMQ::IPV4ONLY, value
expect(rc).to eq(-1)
end
end # context using option ZMQ::IPV4ONLY
context "using option ZMQ::LAST_ENDPOINT" do
it "should return last enpoint" do
random_port = bind_to_random_tcp_port(socket, max_tries = 500)
array = []
rc = socket.getsockopt(ZMQ::LAST_ENDPOINT, array)
expect(ZMQ::Util.resultcode_ok?(rc)).to eq(true)
endpoint_regex = %r{\Atcp://(.*):(\d+)\0\z}
expect(array[0]).to match(endpoint_regex)
expect(Integer(array[0][endpoint_regex, 2])).to eq(random_port)
end
end
context "using option ZMQ::SUBSCRIBE" do
if ZMQ::SUB == socket_type
it "returns 0 for a SUB socket" do
rc = socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string")
expect(rc).to eq(0)
end
else
it "returns -1 for non-SUB sockets" do
rc = socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string")
expect(rc).to eq(-1)
end
end
end # context using option ZMQ::SUBSCRIBE
context "using option ZMQ::UNSUBSCRIBE" do
if ZMQ::SUB == socket_type
it "returns 0 given a topic string that was previously subscribed" do
socket.setsockopt ZMQ::SUBSCRIBE, "topic.string"
rc = socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string")
expect(rc).to eq(0)
end
else
it "returns -1 for non-SUB sockets" do
rc = socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string")
expect(rc).to eq(-1)
end
end
end # context using option ZMQ::UNSUBSCRIBE
context "using option ZMQ::AFFINITY" do
it "should set the affinity value given a positive value" do
affinity = 3
socket.setsockopt ZMQ::AFFINITY, affinity
array = []
rc = socket.getsockopt(ZMQ::AFFINITY, array)
expect(rc).to eq(0)
expect(array[0]).to eq(affinity)
end
end # context using option ZMQ::AFFINITY
context "using option ZMQ::RATE" do
it "should set the multicast send rate given a positive value" do
rate = 200
socket.setsockopt ZMQ::RATE, rate
array = []
rc = socket.getsockopt(ZMQ::RATE, array)
expect(rc).to eq(0)
expect(array[0]).to eq(rate)
end
it "returns -1 given a negative value" do
rate = -200
rc = socket.setsockopt ZMQ::RATE, rate
expect(rc).to eq(-1)
end
end # context using option ZMQ::RATE
context "using option ZMQ::RECOVERY_IVL" do
it "should set the multicast recovery buffer measured in seconds given a positive value" do
rate = 200
socket.setsockopt ZMQ::RECOVERY_IVL, rate
array = []
rc = socket.getsockopt(ZMQ::RECOVERY_IVL, array)
expect(rc).to eq(0)
expect(array[0]).to eq(rate)
end
it "returns -1 given a negative value" do
rate = -200
rc = socket.setsockopt ZMQ::RECOVERY_IVL, rate
expect(rc).to eq(-1)
end
end # context using option ZMQ::RECOVERY_IVL
context "using option ZMQ::SNDBUF" do
it "should set the OS send buffer given a positive value" do
size = 100
socket.setsockopt ZMQ::SNDBUF, size
array = []
rc = socket.getsockopt(ZMQ::SNDBUF, array)
expect(rc).to eq(0)
expect(array[0]).to eq(size)
end
end # context using option ZMQ::SNDBUF
context "using option ZMQ::RCVBUF" do
it "should set the OS receive buffer given a positive value" do
size = 100
socket.setsockopt ZMQ::RCVBUF, size
array = []
rc = socket.getsockopt(ZMQ::RCVBUF, array)
expect(rc).to eq(0)
expect(array[0]).to eq(size)
end
end # context using option ZMQ::RCVBUF
context "using option ZMQ::LINGER" do
it "should set the socket message linger option measured in milliseconds given a positive value" do
value = 200
socket.setsockopt ZMQ::LINGER, value
array = []
rc = socket.getsockopt(ZMQ::LINGER, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "should set the socket message linger option to 0 for dropping packets" do
value = 0
socket.setsockopt ZMQ::LINGER, value
array = []
rc = socket.getsockopt(ZMQ::LINGER, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "should default to a value of 0" do
value = [SUB, XSUB].include?(socket_type) ? 0 : -1
array = []
rc = socket.getsockopt(ZMQ::LINGER, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
end # context using option ZMQ::LINGER
context "using option ZMQ::RECONNECT_IVL" do
it "should set the time interval for reconnecting disconnected sockets measured in milliseconds given a positive value" do
value = 200
socket.setsockopt ZMQ::RECONNECT_IVL, value
array = []
rc = socket.getsockopt(ZMQ::RECONNECT_IVL, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "should default to a value of 100" do
value = 100
array = []
rc = socket.getsockopt(ZMQ::RECONNECT_IVL, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
end # context using option ZMQ::RECONNECT_IVL
context "using option ZMQ::BACKLOG" do
it "should set the maximum number of pending socket connections given a positive value" do
value = 200
rc = socket.setsockopt ZMQ::BACKLOG, value
expect(rc).to eq(0)
array = []
rc = socket.getsockopt(ZMQ::BACKLOG, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
it "should default to a value of 100" do
value = 100
array = []
rc = socket.getsockopt(ZMQ::BACKLOG, array)
expect(rc).to eq(0)
expect(array[0]).to eq(value)
end
end # context using option ZMQ::BACKLOG
end # context #setsockopt
context "#getsockopt for a #{ZMQ::SocketTypeNameMap[socket_type]} socket" do
before(:all) { @ctx = Context.new }
after(:all) { @ctx.terminate }
let(:socket) do
Socket.new @ctx.pointer, socket_type
end
after(:each) do
socket.close
end
if RUBY_PLATFORM =~ /linux|darwin/
# this spec doesn't work on Windows; hints welcome
context "using option ZMQ::FD" do
it "should return an FD as a positive integer" do
array = []
rc = socket.getsockopt(ZMQ::FD, array)
expect(rc).to eq(0)
expect(array[0]).to be > 0
end
it "returns a valid FD that is accepted by the system poll() function" do
# Use FFI to wrap the C library function +poll+ so that we can execute it
# on the 0mq file descriptor. If it returns 0, then it succeeded and the FD
# is valid!
module LibSocket
extend FFI::Library
# figures out the correct libc for each platform including Windows
library = ffi_lib(FFI::Library::LIBC).first
find_type(:nfds_t) rescue typedef(:uint32, :nfds_t)
attach_function :poll, [:pointer, :nfds_t, :int], :int
class PollFD < FFI::Struct
layout :fd, :int,
:events, :short,
:revents, :short
end
end # module LibSocket
array = []
rc = socket.getsockopt(ZMQ::FD, array)
expect(rc).to eq(0)
fd = array[0]
# setup the BSD poll_fd struct
pollfd = LibSocket::PollFD.new
pollfd[:fd] = fd
pollfd[:events] = 0
pollfd[:revents] = 0
rc = LibSocket.poll(pollfd, 1, 0)
expect(rc).to eq(0)
end
end
end # posix platform
context "using option ZMQ::EVENTS" do
it "should return a mask of events as an Integer" do
array = []
rc = socket.getsockopt(ZMQ::EVENTS, array)
expect(rc).to eq(0)
expect(array[0]).to be_a(Integer)
end
end
context "using option ZMQ::TYPE" do
it "should return the socket type" do
array = []
rc = socket.getsockopt(ZMQ::TYPE, array)
expect(rc).to eq(0)
expect(array[0]).to eq(socket_type)
end
end
end # context #getsockopt
end # each socket_type
describe "Mapping socket EVENTS to POLLIN and POLLOUT" do
include APIHelper
shared_examples_for "pubsub sockets where" do
it "SUB socket that received a message always has POLLIN set" do
events = []
rc = @sub.getsockopt(ZMQ::EVENTS, events)
expect(rc).to eq(0)
expect(events[0]).to eq ZMQ::POLLIN
end
it "PUB socket always has POLLOUT set" do
events = []
rc = @pub.getsockopt(ZMQ::EVENTS, events)
expect(rc).to eq(0)
expect(events[0]).to eq ZMQ::POLLOUT
end
it "PUB socket never has POLLIN set" do
events = []
rc = @pub.getsockopt(ZMQ::EVENTS, events)
expect(rc).to eq(0)
expect(events[0]).not_to eq ZMQ::POLLIN
end
it "SUB socket never has POLLOUT set" do
events = []
rc = @sub.getsockopt(ZMQ::EVENTS, events)
expect(rc).to eq(0)
expect(events[0]).not_to eq ZMQ::POLLOUT
end
end # shared example for pubsub
context "when SUB binds and PUB connects" do
before(:each) do
@ctx = Context.new
poller_setup
endpoint = "inproc://socket_test"
@sub = @ctx.socket ZMQ::SUB
rc = @sub.setsockopt ZMQ::SUBSCRIBE, ''
expect(rc).to eq(0)
@pub = @ctx.socket ZMQ::PUB
@sub.bind(endpoint)
connect_to_inproc(@pub, endpoint)
@pub.send_string('test')
end
#it_behaves_like "pubsub sockets where" # see Jira LIBZMQ-270
end # context SUB binds PUB connects
context "when SUB connects and PUB binds" do
before(:each) do
@ctx = Context.new
poller_setup
endpoint = "inproc://socket_test"
@sub = @ctx.socket ZMQ::SUB
rc = @sub.setsockopt ZMQ::SUBSCRIBE, ''
@pub = @ctx.socket ZMQ::PUB
@pub.bind(endpoint)
connect_to_inproc(@sub, endpoint)
poll_it_for_read(@sub) do
rc = @pub.send_string('test')
end
end
it_behaves_like "pubsub sockets where"
end # context SUB binds PUB connects
after(:each) do
@sub.close
@pub.close
# must call close on *every* socket before calling terminate otherwise it blocks indefinitely
@ctx.terminate
end
end # describe 'events mapping to pollin and pollout'
end # describe Socket
end # module ZMQ