ffi-rzmq 1.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +1 -1
  3. data/AUTHORS.txt +5 -1
  4. data/History.txt +45 -0
  5. data/README.rdoc +58 -28
  6. data/Rakefile +15 -0
  7. data/examples/README.rdoc +1 -3
  8. data/examples/{v3api/latency_measurement.rb → latency_measurement.rb} +1 -2
  9. data/examples/{v3api/local_lat.rb → local_lat.rb} +1 -1
  10. data/examples/{v3api/local_lat_poll.rb → local_lat_poll.rb} +4 -4
  11. data/examples/{v3api/local_throughput.rb → local_throughput.rb} +1 -1
  12. data/examples/{v3api/pub.rb → pub.rb} +1 -1
  13. data/examples/{v2api/publish_subscribe.rb → publish_subscribe.rb} +2 -2
  14. data/examples/{v2api/remote_lat.rb → remote_lat.rb} +1 -1
  15. data/examples/{v3api/remote_throughput.rb → remote_throughput.rb} +4 -3
  16. data/examples/repreq_over_curve.rb +60 -0
  17. data/examples/{v2api/reqrep_poll.rb → reqrep_poll.rb} +3 -4
  18. data/examples/{v2api/request_response.rb → request_response.rb} +1 -2
  19. data/examples/{v3api/sub.rb → sub.rb} +1 -2
  20. data/examples/{v3api/throughput_measurement.rb → throughput_measurement.rb} +1 -1
  21. data/examples/{v3api/xreqxrep_poll.rb → xreqxrep_poll.rb} +6 -7
  22. data/ffi-rzmq.gemspec +4 -4
  23. data/lib/ffi-rzmq.rb +2 -3
  24. data/lib/ffi-rzmq/context.rb +25 -53
  25. data/lib/ffi-rzmq/device.rb +8 -4
  26. data/lib/ffi-rzmq/exceptions.rb +3 -0
  27. data/lib/ffi-rzmq/message.rb +24 -30
  28. data/lib/ffi-rzmq/poll.rb +5 -16
  29. data/lib/ffi-rzmq/socket.rb +132 -282
  30. data/lib/ffi-rzmq/util.rb +28 -36
  31. data/lib/ffi-rzmq/version.rb +1 -1
  32. data/spec/context_spec.rb +18 -23
  33. data/spec/device_spec.rb +13 -12
  34. data/spec/message_spec.rb +13 -13
  35. data/spec/multipart_spec.rb +11 -11
  36. data/spec/nonblocking_recv_spec.rb +22 -22
  37. data/spec/poll_spec.rb +49 -49
  38. data/spec/pushpull_spec.rb +12 -11
  39. data/spec/reqrep_spec.rb +11 -11
  40. data/spec/socket_spec.rb +109 -196
  41. data/spec/spec_helper.rb +3 -11
  42. data/spec/util_spec.rb +29 -0
  43. metadata +80 -104
  44. data/examples/v2api/latency_measurement.rb +0 -139
  45. data/examples/v2api/local_lat.rb +0 -58
  46. data/examples/v2api/local_lat_poll.rb +0 -66
  47. data/examples/v2api/local_throughput.rb +0 -58
  48. data/examples/v2api/pub.rb +0 -46
  49. data/examples/v2api/remote_throughput.rb +0 -39
  50. data/examples/v2api/sub.rb +0 -74
  51. data/examples/v2api/throughput_measurement.rb +0 -138
  52. data/examples/v2api/xreqxrep_poll.rb +0 -93
  53. data/examples/v3api/publish_subscribe.rb +0 -82
  54. data/examples/v3api/remote_lat.rb +0 -71
  55. data/examples/v3api/reqrep_poll.rb +0 -62
  56. data/examples/v3api/request_response.rb +0 -40
  57. data/lib/ffi-rzmq/constants.rb +0 -187
  58. data/lib/ffi-rzmq/libc.rb +0 -19
  59. data/lib/ffi-rzmq/libzmq.rb +0 -283
@@ -12,20 +12,20 @@ module ZMQ
12
12
 
13
13
  it "returns -1 when there are no messages to read" do
14
14
  array = []
15
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
16
- Util.resultcode_ok?(rc).should be_false
15
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
16
+ expect(Util.resultcode_ok?(rc)).to eq(false)
17
17
  end
18
18
 
19
19
  it "gets EAGAIN when there are no messages to read" do
20
20
  array = []
21
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
22
- ZMQ::Util.errno.should == ZMQ::EAGAIN
21
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
22
+ expect(ZMQ::Util.errno).to eq(ZMQ::EAGAIN)
23
23
  end
24
24
 
25
25
  it "returns the given array unmodified when there are no messages to read" do
26
26
  array = []
27
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
28
- array.size.should be_zero
27
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
28
+ expect(array.size).to eq(0)
29
29
  end
30
30
 
31
31
  end
@@ -35,26 +35,26 @@ module ZMQ
35
35
  it "read the single message and returns a successful result code" do
36
36
  poll_it_for_read(@receiver) do
37
37
  rc = @sender.send_string('test')
38
- Util.resultcode_ok?(rc).should be_true
38
+ expect(Util.resultcode_ok?(rc)).to eq(true)
39
39
  end
40
40
 
41
41
  array = []
42
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
43
- Util.resultcode_ok?(rc).should be_true
44
- array.size.should == 1
42
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
43
+ expect(Util.resultcode_ok?(rc)).to eq(true)
44
+ expect(array.size).to eq(1)
45
45
  end
46
46
 
47
47
  it "read all message parts transmitted and returns a successful result code" do
48
48
  poll_it_for_read(@receiver) do
49
49
  strings = Array.new(10, 'test')
50
50
  rc = @sender.send_strings(strings)
51
- Util.resultcode_ok?(rc).should be_true
51
+ expect(Util.resultcode_ok?(rc)).to eq(true)
52
52
  end
53
53
 
54
54
  array = []
55
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
56
- Util.resultcode_ok?(rc).should be_true
57
- array.size.should == 10
55
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
56
+ expect(Util.resultcode_ok?(rc)).to eq(true)
57
+ expect(array.size).to eq(10)
58
58
  end
59
59
 
60
60
  end
@@ -64,26 +64,26 @@ module ZMQ
64
64
  it "read the single message and returns a successful result code" do
65
65
  poll_it_for_read(@receiver) do
66
66
  rc = @sender.send_string('test')
67
- Util.resultcode_ok?(rc).should be_true
67
+ expect(Util.resultcode_ok?(rc)).to eq(true)
68
68
  end
69
69
 
70
70
  array = []
71
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
72
- Util.resultcode_ok?(rc).should be_true
73
- array.size.should == 1 + 1 # extra 1 for envelope
71
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
72
+ expect(Util.resultcode_ok?(rc)).to eq(true)
73
+ expect(array.size).to eq(1 + 1) # extra 1 for envelope
74
74
  end
75
75
 
76
76
  it "read all message parts transmitted and returns a successful result code" do
77
77
  poll_it_for_read(@receiver) do
78
78
  strings = Array.new(10, 'test')
79
79
  rc = @sender.send_strings(strings)
80
- Util.resultcode_ok?(rc).should be_true
80
+ expect(Util.resultcode_ok?(rc)).to eq(true)
81
81
  end
82
82
 
83
83
  array = []
84
- rc = @receiver.recvmsgs(array, ZMQ::NonBlocking)
85
- Util.resultcode_ok?(rc).should be_true
86
- array.size.should == 10 + 1 # add 1 for the envelope
84
+ rc = @receiver.recvmsgs(array, ZMQ::DONTWAIT)
85
+ expect(Util.resultcode_ok?(rc)).to eq(true)
86
+ expect(array.size).to eq(10 + 1) # add 1 for the envelope
87
87
  end
88
88
 
89
89
  end
data/spec/poll_spec.rb CHANGED
@@ -8,7 +8,7 @@ module ZMQ
8
8
  include APIHelper
9
9
 
10
10
  it "should allocate a PollItems instance" do
11
- PollItems.should_receive(:new)
11
+ expect(PollItems).to receive(:new)
12
12
  Poller.new
13
13
  end
14
14
 
@@ -23,30 +23,30 @@ module ZMQ
23
23
  let(:fd) { 1 }
24
24
 
25
25
  it "returns false when given a nil pollable" do
26
- poller.register(nil, ZMQ::POLLIN).should be_false
26
+ expect(poller.register(nil, ZMQ::POLLIN)).to be_falsy
27
27
  end
28
28
 
29
29
  it "returns false when given 0 for +events+ (e.g. no registration)" do
30
- poller.register(pollable, 0).should be_false
30
+ expect(poller.register(pollable, 0)).to be_falsy
31
31
  end
32
32
 
33
33
  it "returns the default registered event value when given a valid pollable" do
34
- poller.register(pollable).should == (ZMQ::POLLIN | ZMQ::POLLOUT)
34
+ expect(poller.register(pollable)).to eq(ZMQ::POLLIN | ZMQ::POLLOUT)
35
35
  end
36
36
 
37
37
  it "returns the registered event value when given a pollable responding to socket (ZMQ::Socket)" do
38
- pollable.should_receive(:socket).and_return(socket)
39
- poller.register(pollable, ZMQ::POLLIN).should == ZMQ::POLLIN
38
+ expect(pollable).to receive(:socket).and_return(socket)
39
+ expect(poller.register(pollable, ZMQ::POLLIN)).to eq ZMQ::POLLIN
40
40
  end
41
41
 
42
42
  it "returns the registered event value when given a pollable responding to file descriptor (IO, BasicSocket)" do
43
- pollable.should_receive(:posix_fileno).and_return(fd)
44
- poller.register(pollable, ZMQ::POLLIN).should == ZMQ::POLLIN
43
+ expect(pollable).to receive(:posix_fileno).and_return(fd)
44
+ expect(poller.register(pollable, ZMQ::POLLIN)).to eq(ZMQ::POLLIN)
45
45
  end
46
46
 
47
47
  it "returns the registered event value when given a pollable responding to io (SSLSocket)" do
48
- pollable.should_receive(:io).and_return(io)
49
- poller.register(pollable, ZMQ::POLLIN).should == ZMQ::POLLIN
48
+ expect(pollable).to receive(:io).and_return(io)
49
+ expect(poller.register(pollable, ZMQ::POLLIN)).to eq(ZMQ::POLLIN)
50
50
  end
51
51
 
52
52
  end
@@ -60,49 +60,49 @@ module ZMQ
60
60
  let(:fd) { 1 }
61
61
 
62
62
  it "returns true when deregistered pollable from event" do
63
- pollable.should_receive(:socket).at_least(:once).and_return(socket)
63
+ expect(pollable).to receive(:socket).at_least(:once).and_return(socket)
64
64
  poller.register(pollable)
65
- poller.deregister(pollable, ZMQ::POLLIN).should be_true
65
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(true)
66
66
  end
67
67
 
68
68
  it "returns false when pollable not registered" do
69
- poller.deregister(pollable, ZMQ::POLLIN).should be_false
69
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(false)
70
70
  end
71
71
 
72
72
  it "returns false when pollable not registered for deregistered event" do
73
- pollable.should_receive(:socket).at_least(:once).and_return(socket)
73
+ expect(pollable).to receive(:socket).at_least(:once).and_return(socket)
74
74
  poller.register(pollable, ZMQ::POLLOUT)
75
- poller.deregister(pollable, ZMQ::POLLIN).should be_false
75
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(false)
76
76
  end
77
77
 
78
78
  it "deletes pollable when no events left" do
79
79
  poller.register(pollable, ZMQ::POLLIN)
80
- poller.deregister(pollable, ZMQ::POLLIN).should be_true
81
- poller.size.should == 0
80
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(true)
81
+ expect(poller.size).to eq 0
82
82
  end
83
83
 
84
84
  it "deletes closed pollable responding to socket (ZMQ::Socket)" do
85
- pollable.should_receive(:socket).and_return(socket)
85
+ expect(pollable).to receive(:socket).and_return(socket)
86
86
  poller.register(pollable)
87
- pollable.should_receive(:socket).and_return(nil)
88
- poller.deregister(pollable, ZMQ::POLLIN).should be_true
89
- poller.size.should == 0
87
+ expect(pollable).to receive(:socket).and_return(nil)
88
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(true)
89
+ expect(poller.size).to eq 0
90
90
  end
91
91
 
92
92
  it "deletes closed pollable responding to fileno (IO, BasicSocket)" do
93
- pollable.should_receive(:posix_fileno).and_return(fd)
93
+ expect(pollable).to receive(:posix_fileno).and_return(fd)
94
94
  poller.register(pollable)
95
- pollable.should_receive(:closed?).and_return(true)
96
- poller.deregister(pollable, ZMQ::POLLIN).should be_true
97
- poller.size.should == 0
95
+ expect(pollable).to receive(:closed?).and_return(true)
96
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(true)
97
+ expect(poller.size).to eq 0
98
98
  end
99
99
 
100
100
  it "deletes closed pollable responding to io (SSLSocket)" do
101
- pollable.should_receive(:io).at_least(:once).and_return(io)
101
+ expect(pollable).to receive(:io).at_least(:once).and_return(io)
102
102
  poller.register(pollable)
103
- io.should_receive(:closed?).and_return(true)
104
- poller.deregister(pollable, ZMQ::POLLIN).should be_true
105
- poller.size.should == 0
103
+ expect(io).to receive(:closed?).and_return(true)
104
+ expect(poller.deregister(pollable, ZMQ::POLLIN)).to eq(true)
105
+ expect(poller.size).to eq 0
106
106
  end
107
107
 
108
108
  end
@@ -123,7 +123,7 @@ module ZMQ
123
123
  end
124
124
 
125
125
  it "should return false for an unregistered socket (i.e. not found)" do
126
- @poller.delete(@socket).should be_false
126
+ expect(@poller.delete(@socket)).to eq(false)
127
127
  end
128
128
 
129
129
  it "returns true for a sucessfully deleted socket when only 1 is registered" do
@@ -131,7 +131,7 @@ module ZMQ
131
131
  socket1.setsockopt(LINGER, 0)
132
132
 
133
133
  @poller.register socket1
134
- @poller.delete(socket1).should be_true
134
+ expect(@poller.delete(socket1)).to eq(true)
135
135
  socket1.close
136
136
  end
137
137
 
@@ -143,7 +143,7 @@ module ZMQ
143
143
 
144
144
  @poller.register socket1
145
145
  @poller.register socket2
146
- @poller.delete(socket2).should be_true
146
+ expect(@poller.delete(socket2)).to eq(true)
147
147
  socket1.close
148
148
  socket2.close
149
149
  end
@@ -154,7 +154,7 @@ module ZMQ
154
154
 
155
155
  @poller.register socket1
156
156
  socket1.close
157
- @poller.delete(socket1).should be_true
157
+ expect(@poller.delete(socket1)).to eq(true)
158
158
  end
159
159
 
160
160
  end
@@ -177,12 +177,12 @@ module ZMQ
177
177
  after(:each) { @sockets.each(&:close) }
178
178
 
179
179
  it "returns 0 when there are no sockets to poll" do
180
- @poller.poll(100).should be_zero
180
+ expect(@poller.poll(100)).to eq 0
181
181
  end
182
182
 
183
183
  it "returns 0 when there is a single socket to poll and no events" do
184
184
  @poller.register(@sockets.first, 0)
185
- @poller.poll(100).should be_zero
185
+ expect(@poller.poll(100)).to eq 0
186
186
  end
187
187
 
188
188
  it "returns 1 when there is a read event on a socket" do
@@ -190,7 +190,7 @@ module ZMQ
190
190
  @poller.register_readable(last)
191
191
 
192
192
  first.send_string('test')
193
- @poller.poll(1000).should == 1
193
+ expect(@poller.poll(1000)).to eq 1
194
194
  end
195
195
 
196
196
  it "returns 1 when there is a read event on one socket and the second socket has been removed from polling" do
@@ -200,7 +200,7 @@ module ZMQ
200
200
 
201
201
  first.send_string('test')
202
202
  @poller.deregister_writable(first)
203
- @poller.poll(1000).should == 1
203
+ expect(@poller.poll(1000)).to eq 1
204
204
  end
205
205
 
206
206
  it "works with BasiSocket" do
@@ -214,12 +214,12 @@ module ZMQ
214
214
 
215
215
  client.send("message", 0)
216
216
 
217
- @poller.poll.should == 2
218
- @poller.readables.should == [s]
219
- @poller.writables.should == [client]
217
+ expect(@poller.poll).to eq 2
218
+ expect(@poller.readables).to eq [s]
219
+ expect(@poller.writables).to eq [client]
220
220
 
221
221
  msg = s.read_nonblock(7)
222
- msg.should == "message"
222
+ expect(msg).to eq "message"
223
223
  end
224
224
 
225
225
  it "works with IO objects" do
@@ -229,12 +229,12 @@ module ZMQ
229
229
 
230
230
  w.write("message")
231
231
 
232
- @poller.poll.should == 2
233
- @poller.readables.should == [r]
234
- @poller.writables.should == [w]
232
+ expect(@poller.poll).to eq 2
233
+ expect(@poller.readables).to eq [r]
234
+ expect(@poller.writables).to eq [w]
235
235
 
236
236
  msg = r.read(7)
237
- msg.should == "message"
237
+ expect(msg).to eq "message"
238
238
  end
239
239
 
240
240
  it "works with SSLSocket" do
@@ -262,12 +262,12 @@ module ZMQ
262
262
 
263
263
  client.write("message")
264
264
 
265
- @poller.poll.should == 2
266
- @poller.readables.should == [s]
267
- @poller.writables.should == [client]
265
+ expect(@poller.poll).to eq 2
266
+ expect(@poller.readables).to eq [s]
267
+ expect(@poller.writables).to eq [client]
268
268
 
269
269
  msg = s.read(7)
270
- msg.should == "message"
270
+ expect(msg).to eq "message"
271
271
  end
272
272
  end
273
273
 
@@ -35,7 +35,7 @@ module ZMQ
35
35
  received = ''
36
36
  rc = @pull.recv_string received
37
37
  assert_ok(rc)
38
- received.should == string
38
+ expect(received).to eq(string)
39
39
  end
40
40
 
41
41
  it "should receive an exact string copy of the message sent when receiving in non-blocking mode and using Message objects directly" do
@@ -44,12 +44,12 @@ module ZMQ
44
44
 
45
45
  poll_it_for_read(@pull) do
46
46
  rc = @push.sendmsg sent_message
47
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
47
+ expect(rc).to eq(string.size)
48
48
  end
49
49
 
50
- rc = @pull.recvmsg received_message, ZMQ::NonBlocking
51
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
52
- received_message.copy_out_string.should == string
50
+ rc = @pull.recvmsg received_message, ZMQ::DONTWAIT
51
+ expect(rc).to eq(string.size)
52
+ expect(received_message.copy_out_string).to eq(string)
53
53
  end
54
54
 
55
55
 
@@ -71,20 +71,21 @@ module ZMQ
71
71
  sockets << @pull
72
72
 
73
73
  sockets.each do |socket|
74
- threads << Thread.new do
74
+ thr = Thread.new do
75
75
  buffer = ''
76
76
  rc = socket.recv_string buffer
77
- version2? ? (rc.should == 0) : (rc.should == buffer.size)
77
+ expect(rc).to eq(buffer.size)
78
78
  mutex.synchronize { received << buffer }
79
79
  socket.close
80
80
  end
81
+ threads << thr
81
82
  end
82
-
83
+
83
84
  count.times { @push.send_string(string) }
84
85
 
85
86
  threads.each {|t| t.join}
86
87
 
87
- received.find_all {|r| r == string}.length.should == count
88
+ expect(received.find_all {|r| r == string}.length).to eq(count)
88
89
  end
89
90
 
90
91
  it "should receive a single message for each message sent when using a single shared socket protected by a mutex" do
@@ -98,7 +99,7 @@ module ZMQ
98
99
  buffer = ''
99
100
  rc = 0
100
101
  mutex.synchronize { rc = @pull.recv_string buffer }
101
- version2? ? (rc.should == 0) : (rc.should == buffer.size)
102
+ expect(rc).to eq(buffer.size)
102
103
  mutex.synchronize { received << buffer }
103
104
  end
104
105
  end
@@ -107,7 +108,7 @@ module ZMQ
107
108
 
108
109
  threads.each {|t| t.join}
109
110
 
110
- received.find_all {|r| r == string}.length.should == count
111
+ expect(received.find_all {|r| r == string}.length).to eq(count)
111
112
  end
112
113
 
113
114
  end # @context ping-pong
data/spec/reqrep_spec.rb CHANGED
@@ -40,25 +40,25 @@ module ZMQ
40
40
 
41
41
  it "should receive an exact string copy of the string message sent" do
42
42
  rc, received_message = send_ping(string)
43
- received_message.should == string
43
+ expect(received_message).to eq(string)
44
44
  end
45
45
 
46
46
  it "should generate a EFSM error when sending via the REQ socket twice in a row without an intervening receive operation" do
47
47
  send_ping(string)
48
48
  rc = @ping.send_string(string)
49
- rc.should == -1
50
- Util.errno.should == ZMQ::EFSM
49
+ expect(rc).to eq(-1)
50
+ expect(Util.errno).to eq(ZMQ::EFSM)
51
51
  end
52
52
 
53
53
  it "should receive an exact copy of the sent message using Message objects directly" do
54
54
  received_message = Message.new
55
55
 
56
56
  rc = @ping.sendmsg(Message.new(string))
57
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
57
+ expect(rc).to eq(string.size)
58
58
  rc = @pong.recvmsg received_message
59
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
59
+ expect(rc).to eq(string.size)
60
60
 
61
- received_message.copy_out_string.should == string
61
+ expect(received_message.copy_out_string).to eq(string)
62
62
  end
63
63
 
64
64
  it "should receive an exact copy of the sent message using Message objects directly in non-blocking mode" do
@@ -66,14 +66,14 @@ module ZMQ
66
66
  received_message = Message.new
67
67
 
68
68
  poll_it_for_read(@pong) do
69
- rc = @ping.sendmsg(Message.new(string), ZMQ::NonBlocking)
70
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
69
+ rc = @ping.sendmsg(Message.new(string), ZMQ::DONTWAIT)
70
+ expect(rc).to eq(string.size)
71
71
  end
72
72
 
73
- rc = @pong.recvmsg received_message, ZMQ::NonBlocking
74
- LibZMQ.version2? ? rc.should == 0 : rc.should == string.size
73
+ rc = @pong.recvmsg received_message, ZMQ::DONTWAIT
74
+ expect(rc).to eq(string.size)
75
75
 
76
- received_message.copy_out_string.should == string
76
+ expect(received_message.copy_out_string).to eq(string)
77
77
  end
78
78
 
79
79
  end # context ping-pong
data/spec/socket_spec.rb CHANGED
@@ -7,11 +7,8 @@ module ZMQ
7
7
  describe Socket do
8
8
  include APIHelper
9
9
 
10
- socket_types = if LibZMQ.version2?
11
- [ZMQ::REQ, ZMQ::REP, ZMQ::DEALER, ZMQ::ROUTER, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR]
12
- elsif LibZMQ.version3?
10
+ socket_types =
13
11
  [ZMQ::REQ, ZMQ::REP, ZMQ::DEALER, ZMQ::ROUTER, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR, ZMQ::XPUB, ZMQ::XSUB]
14
- end
15
12
 
16
13
  context "when initializing" do
17
14
  before(:all) { @ctx = Context.new }
@@ -19,21 +16,21 @@ module ZMQ
19
16
 
20
17
 
21
18
  it "should raise an error for a nil context" do
22
- lambda { Socket.new(FFI::Pointer.new(0), ZMQ::REQ) }.should raise_exception(ZMQ::ContextError)
19
+ expect { Socket.new(FFI::Pointer.new(0), ZMQ::REQ) }.to raise_exception(ZMQ::ContextError)
23
20
  end
24
21
 
25
22
  it "works with a Context#pointer as the context_ptr" do
26
- lambda do
23
+ expect do
27
24
  s = Socket.new(@ctx.pointer, ZMQ::REQ)
28
25
  s.close
29
- end.should_not raise_exception
26
+ end.not_to raise_exception
30
27
  end
31
28
 
32
29
  it "works with a Context instance as the context_ptr" do
33
- lambda do
30
+ expect do
34
31
  s = Socket.new(@ctx, ZMQ::SUB)
35
32
  s.close
36
- end.should_not raise_exception
33
+ end.not_to raise_exception
37
34
  end
38
35
 
39
36
 
@@ -41,22 +38,22 @@ module ZMQ
41
38
 
42
39
  it "should not raise an error for a [#{ZMQ::SocketTypeNameMap[socket_type]}] socket type" do
43
40
  sock = nil
44
- lambda { sock = Socket.new(@ctx.pointer, socket_type) }.should_not raise_error
41
+ expect { sock = Socket.new(@ctx.pointer, socket_type) }.not_to raise_error
45
42
  sock.close
46
43
  end
47
44
  end # each socket_type
48
45
 
49
46
  it "should set the :socket accessor to the raw socket allocated by libzmq" do
50
47
  socket = double('socket')
51
- socket.stub(:null? => false)
52
- LibZMQ.should_receive(:zmq_socket).and_return(socket)
48
+ allow(socket).to receive(:null?).and_return(false)
49
+ expect(LibZMQ).to receive(:zmq_socket).and_return(socket)
53
50
 
54
51
  sock = Socket.new(@ctx.pointer, ZMQ::REQ)
55
- sock.socket.should == socket
52
+ expect(sock.socket).to eq(socket)
56
53
  end
57
54
 
58
55
  it "should define a finalizer on this object" do
59
- ObjectSpace.should_receive(:define_finalizer).at_least(1)
56
+ expect(ObjectSpace).to receive(:define_finalizer).at_least(1)
60
57
  sock = Socket.new(@ctx.pointer, ZMQ::REQ)
61
58
  sock.close
62
59
  end
@@ -80,7 +77,7 @@ module ZMQ
80
77
  sock = Socket.new @ctx.pointer, ZMQ::REQ
81
78
  raw_socket = sock.socket
82
79
 
83
- LibZMQ.should_receive(:close).with(raw_socket)
80
+ expect(LibZMQ).to receive(:close).with(raw_socket)
84
81
  sock.close
85
82
  sock.close
86
83
  LibZMQ.close raw_socket # *really close it otherwise the context will block indefinitely
@@ -97,7 +94,7 @@ module ZMQ
97
94
  sock = Socket.new @ctx.pointer, ZMQ::REQ
98
95
 
99
96
  sock.identity = ('a' * 256)
100
- sock.identity.should == ''
97
+ expect(sock.identity).to eq('')
101
98
  sock.close
102
99
  end
103
100
 
@@ -105,7 +102,7 @@ module ZMQ
105
102
  sock = Socket.new @ctx.pointer, ZMQ::REQ
106
103
 
107
104
  sock.identity = ''
108
- sock.identity.should == ''
105
+ expect(sock.identity).to eq('')
109
106
  sock.close
110
107
  end
111
108
 
@@ -113,7 +110,7 @@ module ZMQ
113
110
  sock = Socket.new @ctx.pointer, ZMQ::REQ
114
111
 
115
112
  sock.identity = 'a'
116
- sock.identity.should == 'a'
113
+ expect(sock.identity).to eq('a')
117
114
  sock.close
118
115
  end
119
116
 
@@ -121,7 +118,7 @@ module ZMQ
121
118
  sock = Socket.new @ctx.pointer, ZMQ::REQ
122
119
 
123
120
  sock.identity = ('a' * 255)
124
- sock.identity.should == ('a' * 255)
121
+ expect(sock.identity).to eq('a' * 255)
125
122
  sock.close
126
123
  end
127
124
 
@@ -129,7 +126,7 @@ module ZMQ
129
126
  sock = Socket.new @ctx.pointer, ZMQ::REQ
130
127
 
131
128
  sock.identity = 7
132
- sock.identity.should == '7'
129
+ expect(sock.identity).to eq('7')
133
130
  sock.close
134
131
  end
135
132
  end # context identity=
@@ -160,8 +157,8 @@ module ZMQ
160
157
 
161
158
  array = []
162
159
  rc = socket.getsockopt(ZMQ::IDENTITY, array)
163
- rc.should == 0
164
- array[0].should == identity
160
+ expect(rc).to eq(0)
161
+ expect(array[0]).to eq(identity)
165
162
  end
166
163
  end
167
164
 
@@ -169,138 +166,63 @@ module ZMQ
169
166
  identity = 'a' * 256
170
167
  array = []
171
168
  rc = socket.setsockopt(ZMQ::IDENTITY, identity)
172
- rc.should == -1
169
+ expect(rc).to eq(-1)
173
170
  end
174
171
  end # context using option ZMQ::IDENTITY
175
172
 
173
+ context "using option ZMQ::IPV4ONLY" do
174
+ it "should enable use of IPV6 sockets when set to 0" do
175
+ value = 0
176
+ socket.setsockopt ZMQ::IPV4ONLY, value
177
+ array = []
178
+ rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
179
+ expect(rc).to eq(0)
180
+ expect(array[0]).to eq(value)
181
+ end
176
182
 
177
- if version2?
178
-
179
- context "using option ZMQ::HWM" do
180
- it "should set the high water mark given a positive value" do
181
- hwm = 4
182
- socket.setsockopt ZMQ::HWM, hwm
183
- array = []
184
- rc = socket.getsockopt(ZMQ::HWM, array)
185
- rc.should == 0
186
- array[0].should == hwm
187
- end
188
- end # context using option ZMQ::HWM
189
-
190
-
191
- context "using option ZMQ::SWAP" do
192
- it "should set the swap value given a positive value" do
193
- swap = 10_000
194
- socket.setsockopt ZMQ::SWAP, swap
195
- array = []
196
- rc = socket.getsockopt(ZMQ::SWAP, array)
197
- rc.should == 0
198
- array[0].should == swap
199
- end
200
-
201
- it "returns -1 given a negative value" do
202
- swap = -10_000
203
- rc = socket.setsockopt(ZMQ::SWAP, swap)
204
- rc.should == -1
205
- end
206
- end # context using option ZMQ::SWP
207
-
208
-
209
- context "using option ZMQ::MCAST_LOOP" do
210
- it "should enable the multicast loopback given a 1 (true) value" do
211
- socket.setsockopt ZMQ::MCAST_LOOP, 1
212
- array = []
213
- rc = socket.getsockopt(ZMQ::MCAST_LOOP, array)
214
- rc.should == 0
215
- array[0].should be_true
216
- end
217
-
218
- it "should disable the multicast loopback given a 0 (false) value" do
219
- socket.setsockopt ZMQ::MCAST_LOOP, 0
220
- array = []
221
- rc = socket.getsockopt(ZMQ::MCAST_LOOP, array)
222
- rc.should == 0
223
- array[0].should be_false
224
- end
225
- end # context using option ZMQ::MCAST_LOOP
226
-
227
-
228
- context "using option ZMQ::RECOVERY_IVL_MSEC" do
229
- it "should set the time interval for saving messages measured in milliseconds given a positive value" do
230
- value = 200
231
- socket.setsockopt ZMQ::RECOVERY_IVL_MSEC, value
232
- array = []
233
- rc = socket.getsockopt(ZMQ::RECOVERY_IVL_MSEC, array)
234
- rc.should == 0
235
- array[0].should == value
236
- end
237
-
238
- it "should default to a value of -1" do
239
- value = -1
240
- array = []
241
- rc = socket.getsockopt(ZMQ::RECOVERY_IVL_MSEC, array)
242
- rc.should == 0
243
- array[0].should == value
244
- end
245
- end # context using option ZMQ::RECOVERY_IVL_MSEC
246
-
247
- else # version3 or higher
248
-
249
- context "using option ZMQ::IPV4ONLY" do
250
- it "should enable use of IPV6 sockets when set to 0" do
251
- value = 0
252
- socket.setsockopt ZMQ::IPV4ONLY, value
253
- array = []
254
- rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
255
- rc.should == 0
256
- array[0].should == value
257
- end
258
-
259
- it "should default to a value of 1" do
260
- value = 1
261
- array = []
262
- rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
263
- rc.should == 0
264
- array[0].should == value
265
- end
266
-
267
- it "returns -1 given a negative value" do
268
- value = -1
269
- rc = socket.setsockopt ZMQ::IPV4ONLY, value
270
- rc.should == -1
271
- end
183
+ it "should default to a value of 1" do
184
+ value = 1
185
+ array = []
186
+ rc = socket.getsockopt(ZMQ::IPV4ONLY, array)
187
+ expect(rc).to eq(0)
188
+ expect(array[0]).to eq(value)
189
+ end
272
190
 
273
- it "returns -1 given a value > 1" do
274
- value = 2
275
- rc = socket.setsockopt ZMQ::IPV4ONLY, value
276
- rc.should == -1
277
- end
278
- end # context using option ZMQ::IPV4ONLY
191
+ it "returns -1 given a negative value" do
192
+ value = -1
193
+ rc = socket.setsockopt ZMQ::IPV4ONLY, value
194
+ expect(rc).to eq(-1)
195
+ end
279
196
 
280
- context "using option ZMQ::LAST_ENDPOINT" do
281
- it "should return last enpoint" do
282
- random_port = bind_to_random_tcp_port(socket, max_tries = 500)
283
- array = []
284
- rc = socket.getsockopt(ZMQ::LAST_ENDPOINT, array)
285
- ZMQ::Util.resultcode_ok?(rc).should == true
286
- endpoint_regex = %r{\Atcp://(.*):(\d+)\0\z}
287
- array[0].should =~ endpoint_regex
288
- Integer(array[0][endpoint_regex, 2]).should == random_port
289
- end
197
+ it "returns -1 given a value > 1" do
198
+ value = 2
199
+ rc = socket.setsockopt ZMQ::IPV4ONLY, value
200
+ expect(rc).to eq(-1)
290
201
  end
291
- end # version2? if/else block
202
+ end # context using option ZMQ::IPV4ONLY
292
203
 
204
+ context "using option ZMQ::LAST_ENDPOINT" do
205
+ it "should return last enpoint" do
206
+ random_port = bind_to_random_tcp_port(socket, max_tries = 500)
207
+ array = []
208
+ rc = socket.getsockopt(ZMQ::LAST_ENDPOINT, array)
209
+ expect(ZMQ::Util.resultcode_ok?(rc)).to eq(true)
210
+ endpoint_regex = %r{\Atcp://(.*):(\d+)\0\z}
211
+ expect(array[0]).to match(endpoint_regex)
212
+ expect(Integer(array[0][endpoint_regex, 2])).to eq(random_port)
213
+ end
214
+ end
293
215
 
294
216
  context "using option ZMQ::SUBSCRIBE" do
295
217
  if ZMQ::SUB == socket_type
296
218
  it "returns 0 for a SUB socket" do
297
219
  rc = socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string")
298
- rc.should == 0
220
+ expect(rc).to eq(0)
299
221
  end
300
222
  else
301
223
  it "returns -1 for non-SUB sockets" do
302
224
  rc = socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string")
303
- rc.should == -1
225
+ expect(rc).to eq(-1)
304
226
  end
305
227
  end
306
228
  end # context using option ZMQ::SUBSCRIBE
@@ -311,13 +233,13 @@ module ZMQ
311
233
  it "returns 0 given a topic string that was previously subscribed" do
312
234
  socket.setsockopt ZMQ::SUBSCRIBE, "topic.string"
313
235
  rc = socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string")
314
- rc.should == 0
236
+ expect(rc).to eq(0)
315
237
  end
316
238
 
317
239
  else
318
240
  it "returns -1 for non-SUB sockets" do
319
241
  rc = socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string")
320
- rc.should == -1
242
+ expect(rc).to eq(-1)
321
243
  end
322
244
  end
323
245
  end # context using option ZMQ::UNSUBSCRIBE
@@ -329,8 +251,8 @@ module ZMQ
329
251
  socket.setsockopt ZMQ::AFFINITY, affinity
330
252
  array = []
331
253
  rc = socket.getsockopt(ZMQ::AFFINITY, array)
332
- rc.should == 0
333
- array[0].should == affinity
254
+ expect(rc).to eq(0)
255
+ expect(array[0]).to eq(affinity)
334
256
  end
335
257
  end # context using option ZMQ::AFFINITY
336
258
 
@@ -341,14 +263,14 @@ module ZMQ
341
263
  socket.setsockopt ZMQ::RATE, rate
342
264
  array = []
343
265
  rc = socket.getsockopt(ZMQ::RATE, array)
344
- rc.should == 0
345
- array[0].should == rate
266
+ expect(rc).to eq(0)
267
+ expect(array[0]).to eq(rate)
346
268
  end
347
269
 
348
270
  it "returns -1 given a negative value" do
349
271
  rate = -200
350
272
  rc = socket.setsockopt ZMQ::RATE, rate
351
- rc.should == -1
273
+ expect(rc).to eq(-1)
352
274
  end
353
275
  end # context using option ZMQ::RATE
354
276
 
@@ -359,14 +281,14 @@ module ZMQ
359
281
  socket.setsockopt ZMQ::RECOVERY_IVL, rate
360
282
  array = []
361
283
  rc = socket.getsockopt(ZMQ::RECOVERY_IVL, array)
362
- rc.should == 0
363
- array[0].should == rate
284
+ expect(rc).to eq(0)
285
+ expect(array[0]).to eq(rate)
364
286
  end
365
287
 
366
288
  it "returns -1 given a negative value" do
367
289
  rate = -200
368
290
  rc = socket.setsockopt ZMQ::RECOVERY_IVL, rate
369
- rc.should == -1
291
+ expect(rc).to eq(-1)
370
292
  end
371
293
  end # context using option ZMQ::RECOVERY_IVL
372
294
 
@@ -377,8 +299,8 @@ module ZMQ
377
299
  socket.setsockopt ZMQ::SNDBUF, size
378
300
  array = []
379
301
  rc = socket.getsockopt(ZMQ::SNDBUF, array)
380
- rc.should == 0
381
- array[0].should == size
302
+ expect(rc).to eq(0)
303
+ expect(array[0]).to eq(size)
382
304
  end
383
305
  end # context using option ZMQ::SNDBUF
384
306
 
@@ -389,8 +311,8 @@ module ZMQ
389
311
  socket.setsockopt ZMQ::RCVBUF, size
390
312
  array = []
391
313
  rc = socket.getsockopt(ZMQ::RCVBUF, array)
392
- rc.should == 0
393
- array[0].should == size
314
+ expect(rc).to eq(0)
315
+ expect(array[0]).to eq(size)
394
316
  end
395
317
  end # context using option ZMQ::RCVBUF
396
318
 
@@ -401,8 +323,8 @@ module ZMQ
401
323
  socket.setsockopt ZMQ::LINGER, value
402
324
  array = []
403
325
  rc = socket.getsockopt(ZMQ::LINGER, array)
404
- rc.should == 0
405
- array[0].should == value
326
+ expect(rc).to eq(0)
327
+ expect(array[0]).to eq(value)
406
328
  end
407
329
 
408
330
  it "should set the socket message linger option to 0 for dropping packets" do
@@ -410,27 +332,17 @@ module ZMQ
410
332
  socket.setsockopt ZMQ::LINGER, value
411
333
  array = []
412
334
  rc = socket.getsockopt(ZMQ::LINGER, array)
413
- rc.should == 0
414
- array[0].should == value
335
+ expect(rc).to eq(0)
336
+ expect(array[0]).to eq(value)
415
337
  end
416
338
 
417
- if (ZMQ::SUB == socket_type) && version3? || (defined?(ZMQ::XSUB) && ZMQ::XSUB == socket_type)
418
339
  it "should default to a value of 0" do
419
- value = 0
420
- array = []
421
- rc = socket.getsockopt(ZMQ::LINGER, array)
422
- rc.should == 0
423
- array[0].should == value
424
- end
425
- else
426
- it "should default to a value of -1" do
427
- value = -1
340
+ value = [SUB, XSUB].include?(socket_type) ? 0 : -1
428
341
  array = []
429
342
  rc = socket.getsockopt(ZMQ::LINGER, array)
430
- rc.should == 0
431
- array[0].should == value
343
+ expect(rc).to eq(0)
344
+ expect(array[0]).to eq(value)
432
345
  end
433
- end
434
346
  end # context using option ZMQ::LINGER
435
347
 
436
348
 
@@ -440,16 +352,16 @@ module ZMQ
440
352
  socket.setsockopt ZMQ::RECONNECT_IVL, value
441
353
  array = []
442
354
  rc = socket.getsockopt(ZMQ::RECONNECT_IVL, array)
443
- rc.should == 0
444
- array[0].should == value
355
+ expect(rc).to eq(0)
356
+ expect(array[0]).to eq(value)
445
357
  end
446
358
 
447
359
  it "should default to a value of 100" do
448
360
  value = 100
449
361
  array = []
450
362
  rc = socket.getsockopt(ZMQ::RECONNECT_IVL, array)
451
- rc.should == 0
452
- array[0].should == value
363
+ expect(rc).to eq(0)
364
+ expect(array[0]).to eq(value)
453
365
  end
454
366
  end # context using option ZMQ::RECONNECT_IVL
455
367
 
@@ -457,19 +369,20 @@ module ZMQ
457
369
  context "using option ZMQ::BACKLOG" do
458
370
  it "should set the maximum number of pending socket connections given a positive value" do
459
371
  value = 200
460
- socket.setsockopt ZMQ::BACKLOG, value
372
+ rc = socket.setsockopt ZMQ::BACKLOG, value
373
+ expect(rc).to eq(0)
461
374
  array = []
462
375
  rc = socket.getsockopt(ZMQ::BACKLOG, array)
463
- rc.should == 0
464
- array[0].should == value
376
+ expect(rc).to eq(0)
377
+ expect(array[0]).to eq(value)
465
378
  end
466
379
 
467
380
  it "should default to a value of 100" do
468
381
  value = 100
469
382
  array = []
470
383
  rc = socket.getsockopt(ZMQ::BACKLOG, array)
471
- rc.should == 0
472
- array[0].should == value
384
+ expect(rc).to eq(0)
385
+ expect(array[0]).to eq(value)
473
386
  end
474
387
  end # context using option ZMQ::BACKLOG
475
388
 
@@ -495,8 +408,8 @@ module ZMQ
495
408
  it "should return an FD as a positive integer" do
496
409
  array = []
497
410
  rc = socket.getsockopt(ZMQ::FD, array)
498
- rc.should == 0
499
- array[0].should > 0
411
+ expect(rc).to eq(0)
412
+ expect(array[0]).to be > 0
500
413
  end
501
414
 
502
415
  it "returns a valid FD that is accepted by the system poll() function" do
@@ -514,14 +427,14 @@ module ZMQ
514
427
 
515
428
  class PollFD < FFI::Struct
516
429
  layout :fd, :int,
517
- :events, :short,
518
- :revents, :short
430
+ :events, :short,
431
+ :revents, :short
519
432
  end
520
433
  end # module LibSocket
521
434
 
522
435
  array = []
523
436
  rc = socket.getsockopt(ZMQ::FD, array)
524
- rc.should be_zero
437
+ expect(rc).to eq(0)
525
438
  fd = array[0]
526
439
 
527
440
  # setup the BSD poll_fd struct
@@ -531,7 +444,7 @@ module ZMQ
531
444
  pollfd[:revents] = 0
532
445
 
533
446
  rc = LibSocket.poll(pollfd, 1, 0)
534
- rc.should be_zero
447
+ expect(rc).to eq(0)
535
448
  end
536
449
  end
537
450
 
@@ -541,8 +454,8 @@ module ZMQ
541
454
  it "should return a mask of events as a Fixnum" do
542
455
  array = []
543
456
  rc = socket.getsockopt(ZMQ::EVENTS, array)
544
- rc.should == 0
545
- array[0].should be_a(Fixnum)
457
+ expect(rc).to eq(0)
458
+ expect(array[0]).to be_a(Fixnum)
546
459
  end
547
460
  end
548
461
 
@@ -550,8 +463,8 @@ module ZMQ
550
463
  it "should return the socket type" do
551
464
  array = []
552
465
  rc = socket.getsockopt(ZMQ::TYPE, array)
553
- rc.should == 0
554
- array[0].should == socket_type
466
+ expect(rc).to eq(0)
467
+ expect(array[0]).to eq(socket_type)
555
468
  end
556
469
  end
557
470
  end # context #getsockopt
@@ -566,29 +479,29 @@ module ZMQ
566
479
  it "SUB socket that received a message always has POLLIN set" do
567
480
  events = []
568
481
  rc = @sub.getsockopt(ZMQ::EVENTS, events)
569
- rc.should == 0
570
- events[0].should == ZMQ::POLLIN
482
+ expect(rc).to eq(0)
483
+ expect(events[0]).to eq ZMQ::POLLIN
571
484
  end
572
485
 
573
486
  it "PUB socket always has POLLOUT set" do
574
487
  events = []
575
488
  rc = @pub.getsockopt(ZMQ::EVENTS, events)
576
- rc.should == 0
577
- events[0].should == ZMQ::POLLOUT
489
+ expect(rc).to eq(0)
490
+ expect(events[0]).to eq ZMQ::POLLOUT
578
491
  end
579
492
 
580
493
  it "PUB socket never has POLLIN set" do
581
494
  events = []
582
495
  rc = @pub.getsockopt(ZMQ::EVENTS, events)
583
- rc.should == 0
584
- events[0].should_not == ZMQ::POLLIN
496
+ expect(rc).to eq(0)
497
+ expect(events[0]).not_to eq ZMQ::POLLIN
585
498
  end
586
499
 
587
500
  it "SUB socket never has POLLOUT set" do
588
501
  events = []
589
502
  rc = @sub.getsockopt(ZMQ::EVENTS, events)
590
- rc.should == 0
591
- events[0].should_not == ZMQ::POLLOUT
503
+ expect(rc).to eq(0)
504
+ expect(events[0]).not_to eq ZMQ::POLLOUT
592
505
  end
593
506
  end # shared example for pubsub
594
507
 
@@ -601,7 +514,7 @@ module ZMQ
601
514
  endpoint = "inproc://socket_test"
602
515
  @sub = @ctx.socket ZMQ::SUB
603
516
  rc = @sub.setsockopt ZMQ::SUBSCRIBE, ''
604
- rc.should == 0
517
+ expect(rc).to eq(0)
605
518
 
606
519
  @pub = @ctx.socket ZMQ::PUB
607
520
  @sub.bind(endpoint)