ffi-rzmq 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/context_spec.rb CHANGED
@@ -13,6 +13,14 @@ module ZMQ
13
13
  it "should raise an error for negative io threads" do
14
14
  lambda { Context.new(-1) }.should raise_exception(ZMQ::ContextError)
15
15
  end
16
+
17
+ it "should default to requesting 1 i/o thread when no argument is passed" do
18
+ ctx = mock('ctx')
19
+ ctx.stub!(:null? => false)
20
+ LibZMQ.should_receive(:zmq_init).with(1).and_return(ctx)
21
+
22
+ Context.new
23
+ end
16
24
 
17
25
  it "should set the :pointer accessor to non-nil" do
18
26
  ctx = Context.new 1
@@ -43,7 +51,7 @@ module ZMQ
43
51
  ctx.terminate
44
52
  end
45
53
 
46
- it "should raise an exception when it fails" do
54
+ it "should raise a ZMQ::ContextError exception when it fails" do
47
55
  ctx = Context.new 1
48
56
  LibZMQ.stub(:zmq_term => 1)
49
57
  lambda { ctx.terminate }.should raise_error(ZMQ::ContextError)
@@ -57,7 +65,7 @@ module ZMQ
57
65
  ctx.socket(ZMQ::REQ).should be_kind_of(ZMQ::Socket)
58
66
  end
59
67
 
60
- it "should raise an exception when allocation fails" do
68
+ it "should raise a ZMQ::SocketError exception when allocation fails" do
61
69
  ctx = Context.new 1
62
70
  Socket.stub(:new => nil)
63
71
  lambda { ctx.socket(ZMQ::REQ) }.should raise_error(ZMQ::SocketError)
@@ -0,0 +1,42 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
3
+
4
+ module ZMQ
5
+
6
+
7
+ describe Message do
8
+
9
+ context "when initializing with an argument" do
10
+
11
+ it "should *not* define a finalizer on this object" do
12
+ ObjectSpace.should_not_receive(:define_finalizer)
13
+ Message.new "text"
14
+ end
15
+ end # context initializing
16
+
17
+
18
+ context "when copying in data" do
19
+ it "should raise a MessageError when the Message is being reused" do
20
+ message = Message.new "text"
21
+ lambda { message.copy_in_string("new text") }.should raise_error(MessageError)
22
+ end
23
+ end
24
+
25
+ end # describe Message
26
+
27
+
28
+ describe ManagedMessage do
29
+
30
+ context "when initializing with an argument" do
31
+
32
+ it "should define a finalizer on this object" do
33
+ ObjectSpace.should_receive(:define_finalizer)
34
+ ManagedMessage.new "text"
35
+ end
36
+ end # context initializing
37
+
38
+
39
+ end # describe ManagedMessage
40
+
41
+
42
+ end # module ZMQ
@@ -0,0 +1,70 @@
1
+
2
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
3
+
4
+ module ZMQ
5
+ describe Context do
6
+ context "when running basic push pull" do
7
+ include APIHelper
8
+
9
+ let(:string) { "booga-booga" }
10
+
11
+ before(:each) do
12
+ $stdout.flush
13
+ @context = ZMQ::Context.new 1
14
+ @push = @context.socket ZMQ::PUSH
15
+ @pull = @context.socket ZMQ::PULL
16
+ @link = "tcp://127.0.0.1:#{random_port}"
17
+ @pull.connect @link
18
+ @push.bind @link
19
+ end
20
+
21
+ it "should receive an exact copy of the sent message using Message objects directly on one pull socket" do
22
+ @push.send_string string
23
+ received = @pull.recv_string
24
+ received.should == string
25
+ end
26
+
27
+ it "should receive an exact string copy of the message sent when receiving in non-blocking mode and using Message objects directly" do
28
+ sent_message = Message.new string
29
+ received_message = Message.new
30
+
31
+ @push.send sent_message
32
+ sleep 0.1 # give it time for delivery
33
+ @pull.recv received_message, ZMQ::NOBLOCK
34
+ received_message.copy_out_string.should == string
35
+ end
36
+
37
+ it "should receive an exact string copy of the message sent when sending in non-blocking mode and using Message objects directly" do
38
+ sent_message = Message.new string
39
+ received_message = Message.new
40
+
41
+ @push.send sent_message, ZMQ::NOBLOCK
42
+ sleep 0.1 # give it time for delivery
43
+ @pull.recv received_message, ZMQ::NOBLOCK
44
+ received_message.copy_out_string.should == string
45
+ end
46
+
47
+ it "should receive a single message for each message sent on each socket listening, when an equal number pulls to messages" do
48
+ received = []
49
+ threads = []
50
+ count = 4
51
+
52
+ count.times {
53
+ threads << Thread.new {
54
+ pull = @context.socket ZMQ::PULL
55
+ pull.connect @link
56
+ received << pull.recv_string
57
+ pull.close
58
+ }
59
+ }
60
+
61
+ count.times { @push.send_string(string); sleep 0.001 }
62
+
63
+ threads.each {|t| t.join}
64
+
65
+ received.find_all {|r| r == string}.length.should == count
66
+ end
67
+
68
+ end # @context ping-pong
69
+ end # describe
70
+ end # module ZMQ
data/spec/socket_spec.rb CHANGED
@@ -9,7 +9,7 @@ module ZMQ
9
9
  context "when initializing" do
10
10
 
11
11
  let(:ctx) { Context.new 1 }
12
-
12
+
13
13
  it "should raise an error for a nil context" do
14
14
  lambda { Socket.new(FFI::Pointer::NULL, ZMQ::REQ) }.should raise_exception(ZMQ::ContextError)
15
15
  end
@@ -54,9 +54,13 @@ module ZMQ
54
54
  lambda { Socket.new(ctx.pointer, 80) }.should raise_exception(ZMQ::SocketError)
55
55
  end
56
56
 
57
- it "should set the :socket accessor to non-nil" do
58
- sock = Socket.new(Context.new(1).pointer, ZMQ::REQ)
59
- sock.socket.should_not be_nil
57
+ it "should set the :socket accessor to the raw socket allocated by libzmq" do
58
+ socket = mock('socket')#.as_null_object
59
+ socket.stub!(:null? => false)
60
+ LibZMQ.should_receive(:zmq_socket).and_return(socket)
61
+
62
+ sock = Socket.new(ctx.pointer, ZMQ::REQ)
63
+ sock.socket.should == socket
60
64
  end
61
65
 
62
66
  it "should define a finalizer on this object" do
@@ -65,47 +69,222 @@ module ZMQ
65
69
  ctx = Context.new 1
66
70
  end
67
71
  end # context initializing
68
-
69
-
72
+
73
+
70
74
  context "identity=" do
71
75
  it "should raise an exception for identities in excess of 255 bytes" do
72
76
  ctx = Context.new 1
73
77
  sock = Socket.new ctx.pointer, ZMQ::REQ
74
-
78
+
75
79
  lambda { sock.identity = ('a' * 256) }.should raise_exception(ZMQ::SocketError)
76
80
  end
77
81
 
78
82
  it "should raise an exception for identities of length 0" do
79
83
  ctx = Context.new 1
80
84
  sock = Socket.new ctx.pointer, ZMQ::REQ
81
-
85
+
82
86
  lambda { sock.identity = '' }.should raise_exception(ZMQ::SocketError)
83
87
  end
84
88
 
85
89
  it "should NOT raise an exception for identities of 1 byte" do
86
90
  ctx = Context.new 1
87
91
  sock = Socket.new ctx.pointer, ZMQ::REQ
88
-
92
+
89
93
  lambda { sock.identity = 'a' }.should_not raise_exception(ZMQ::SocketError)
90
94
  end
91
95
 
92
96
  it "should NOT raise an exception for identities of 255 bytes" do
93
97
  ctx = Context.new 1
94
98
  sock = Socket.new ctx.pointer, ZMQ::REQ
95
-
99
+
96
100
  lambda { sock.identity = ('a' * 255) }.should_not raise_exception(ZMQ::SocketError)
97
101
  end
98
102
 
99
103
  it "should convert numeric identities to strings" do
100
104
  ctx = Context.new 1
101
105
  sock = Socket.new ctx.pointer, ZMQ::REQ
102
-
106
+
103
107
  sock.identity = 7
104
108
  sock.identity.should == '7'
105
109
  end
106
110
  end # context identity=
107
-
108
-
111
+
112
+
113
+ [ZMQ::REQ, ZMQ::REP, ZMQ::XREQ, ZMQ::XREP, ZMQ::PUB, ZMQ::SUB, ZMQ::PUSH, ZMQ::PULL, ZMQ::PAIR].each do |socket_type|
114
+
115
+ context "#setsockopt for a #{ZMQ::SocketTypeNameMap[socket_type]} socket" do
116
+ let(:socket) do
117
+ ctx = Context.new
118
+ Socket.new ctx.pointer, socket_type
119
+ end
120
+
121
+
122
+ context "using option ZMQ::SUBSCRIBE" do
123
+ if ZMQ::SUB == socket_type
124
+ it "should *not* raise a ZMQ::SocketError" do
125
+ lambda { socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string") }.should_not raise_error(SocketError)
126
+ end
127
+ else
128
+ it "should raise a ZMQ::SocketError" do
129
+ lambda { socket.setsockopt(ZMQ::SUBSCRIBE, "topic.string") }.should raise_error(SocketError)
130
+ end
131
+ end
132
+ end # context using option ZMQ::SUBSCRIBE
133
+
134
+
135
+ context "using option ZMQ::UNSUBSCRIBE" do
136
+ if ZMQ::SUB == socket_type
137
+ it "should *not* raise a ZMQ::SocketError given a topic string that was previously subscribed" do
138
+ socket.setsockopt ZMQ::SUBSCRIBE, "topic.string"
139
+ lambda { socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string") }.should_not raise_error(SocketError)
140
+ end
141
+
142
+ it "should raise a ZMQ::SocketError given a topic string that was never subscribed" do
143
+ socket.setsockopt ZMQ::SUBSCRIBE, "topic.string"
144
+ lambda { socket.setsockopt(ZMQ::UNSUBSCRIBE, "unknown") }.should raise_error(SocketError)
145
+ end
146
+ else
147
+ it "should raise a ZMQ::SocketError" do
148
+ lambda { socket.setsockopt(ZMQ::UNSUBSCRIBE, "topic.string") }.should raise_error(SocketError)
149
+ end
150
+ end
151
+ end # context using option ZMQ::UNSUBSCRIBE
152
+
153
+
154
+ context "using option ZMQ::HWM" do
155
+ it "should set the high water mark given a positive value" do
156
+ hwm = 4
157
+ socket.setsockopt ZMQ::HWM, hwm
158
+ socket.getsockopt(ZMQ::HWM).should == hwm
159
+ end
160
+
161
+ it "should convert a negative value to a positive value" do
162
+ hwm = -4
163
+ socket.setsockopt ZMQ::HWM, hwm
164
+ socket.getsockopt(ZMQ::HWM).should == hwm.abs
165
+ end
166
+ end # context using option ZMQ::HWM
167
+
168
+
169
+ # context "using option ZMQ::SWAP" do
170
+ # it "should set the swap value given a positive value" do
171
+ # swap = 10_000
172
+ # socket.setsockopt ZMQ::SWAP, swap
173
+ # socket.getsockopt(ZMQ::SWAP).should == swap
174
+ # end
175
+ #
176
+ # it "should raise a SocketError given a negative value" do
177
+ # swap = -10_000
178
+ # lambda { socket.setsockopt(ZMQ::SWAP, swap) }.should raise_error(SocketError)
179
+ # end
180
+ # end # context using option ZMQ::SWP
181
+
182
+
183
+ context "using option ZMQ::AFFINITY" do
184
+ it "should set the affinity value given a positive value" do
185
+ affinity = 3
186
+ socket.setsockopt ZMQ::AFFINITY, affinity
187
+ socket.getsockopt(ZMQ::AFFINITY).should == affinity
188
+ end
189
+
190
+ it "should set the affinity value as a positive value given a negative value" do
191
+ affinity = -3
192
+ socket.setsockopt ZMQ::AFFINITY, affinity
193
+ socket.getsockopt(ZMQ::AFFINITY).should == affinity.abs
194
+ end
195
+ end # context using option ZMQ::AFFINITY
196
+
197
+
198
+ context "using option ZMQ::IDENTITY" do
199
+ it "should set the identity given any string under 255 characters" do
200
+ (1..255).each do |length|
201
+ identity = 'a' * length
202
+ socket.setsockopt ZMQ::IDENTITY, identity
203
+ socket.getsockopt(ZMQ::IDENTITY).should == identity
204
+ end
205
+ end
206
+
207
+ it "should raise a SocketError given a string 256 characters or longer" do
208
+ identity = 'a' * 256
209
+ lambda { socket.setsockopt(ZMQ::IDENTITY, identity) }.should raise_error(SocketError)
210
+ end
211
+ end # context using option ZMQ::IDENTITY
212
+
213
+
214
+ # context "using option ZMQ::RATE" do
215
+ # it "should set the multicast send rate given a positive value" do
216
+ # rate = 200
217
+ # socket.setsockopt ZMQ::RATE, rate
218
+ # socket.getsockopt(ZMQ::RATE).should == rate
219
+ # end
220
+ #
221
+ # it "should raise a SocketError given a negative value" do
222
+ # rate = -200
223
+ # lambda { socket.setsockopt ZMQ::RATE, rate }.should raise_error(SocketError)
224
+ # end
225
+ # end # context using option ZMQ::RATE
226
+ #
227
+ #
228
+ # context "using option ZMQ::RECOVERY_IVL" do
229
+ # it "should set the multicast recovery buffer measured in seconds given a positive value" do
230
+ # rate = 200
231
+ # socket.setsockopt ZMQ::RECOVERY_IVL, rate
232
+ # socket.getsockopt(ZMQ::RECOVERY_IVL).should == rate
233
+ # end
234
+ #
235
+ # it "should raise a SocketError given a negative value" do
236
+ # rate = -200
237
+ # lambda { socket.setsockopt ZMQ::RECOVERY_IVL, rate }.should raise_error(SocketError)
238
+ # end
239
+ # end # context using option ZMQ::RECOVERY_IVL
240
+
241
+
242
+ context "using option ZMQ::MCAST_LOOP" do
243
+ it "should enable the multicast loopback given a true value" do
244
+ socket.setsockopt ZMQ::MCAST_LOOP, true
245
+ socket.getsockopt(ZMQ::MCAST_LOOP).should be_true
246
+ end
247
+
248
+ it "should disable the multicast loopback given a false value" do
249
+ socket.setsockopt ZMQ::MCAST_LOOP, false
250
+ socket.getsockopt(ZMQ::MCAST_LOOP).should be_false
251
+ end
252
+ end # context using option ZMQ::MCAST_LOOP
253
+
254
+
255
+ context "using option ZMQ::SNDBUF" do
256
+ it "should set the OS send buffer given a positive value" do
257
+ size = 100
258
+ socket.setsockopt ZMQ::SNDBUF, size
259
+ socket.getsockopt(ZMQ::SNDBUF).should == size
260
+ end
261
+
262
+ it "should set the OS send buffer to a positive value given a false value" do
263
+ size = -100
264
+ socket.setsockopt ZMQ::SNDBUF, size
265
+ socket.getsockopt(ZMQ::SNDBUF).should == size.abs
266
+ end
267
+ end # context using option ZMQ::SNDBUF
268
+
269
+
270
+ context "using option ZMQ::RCVBUF" do
271
+ it "should set the OS receive buffer given a positive value" do
272
+ size = 100
273
+ socket.setsockopt ZMQ::RCVBUF, size
274
+ socket.getsockopt(ZMQ::RCVBUF).should == size
275
+ end
276
+
277
+ it "should set the OS receive buffer to a positive value given a false value" do
278
+ size = -100
279
+ socket.setsockopt ZMQ::RCVBUF, size
280
+ socket.getsockopt(ZMQ::RCVBUF).should == size.abs
281
+ end
282
+ end # context using option ZMQ::RCVBUF
283
+ end # context #setsockopt
284
+
285
+ end # each socket_type
286
+
287
+
109
288
  end # describe Socket
110
289
 
111
290
 
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,8 @@
5
5
  require File.expand_path(
6
6
  File.join(File.dirname(__FILE__), %w[.. lib ffi-rzmq]))
7
7
 
8
+ Thread.abort_on_exception = true
9
+
8
10
  # turns off all warnings; added so I don't have to see the warnings
9
11
  # for included libraries like FFI.
10
12
  $VERBOSE = false
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
metadata CHANGED
@@ -3,39 +3,40 @@ name: ffi-rzmq
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
- - 0
7
- - 5
8
- - 1
9
- version: 0.5.1
6
+ - 0
7
+ - 6
8
+ - 0
9
+ version: 0.6.0
10
10
  platform: ruby
11
11
  authors:
12
- - Chuck Remes
12
+ - Chuck Remes
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-30 00:00:00 -05:00
17
+ date: 2010-09-21 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: bones
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 3
29
- - 4
30
- - 7
31
- version: 3.4.7
32
- type: :development
33
- version_requirements: *id001
20
+ - !ruby/object:Gem::Dependency
21
+ name: bones
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 4
31
+ - 7
32
+ version: 3.4.7
33
+ type: :development
34
+ version_requirements: *id001
34
35
  description: |-
35
36
  This gem wraps the ZeroMQ networking library using the ruby FFI (foreign
36
37
  function interface). It's a pure ruby wrapper so this gem can be loaded
37
- and run by any ruby runtime that supports FFI. Right now that means
38
- MRI 1.9.x and JRuby.
38
+ and run by any ruby runtime that supports FFI. That's all of them:
39
+ MRI 1.8.7+, 1.9.x, Rubinius and JRuby.
39
40
 
40
41
  The impetus behind this library was to provide support for ZeroMQ in
41
42
  JRuby which has native threads. Unlike MRI, MacRuby, IronRuby and
@@ -49,67 +50,73 @@ executables: []
49
50
  extensions: []
50
51
 
51
52
  extra_rdoc_files:
52
- - History.txt
53
- - README.rdoc
54
- - version.txt
53
+ - History.txt
54
+ - README.rdoc
55
+ - examples/README.rdoc
56
+ - version.txt
55
57
  files:
56
- - .bnsignore
57
- - History.txt
58
- - README.rdoc
59
- - Rakefile
60
- - examples/async_req_rep.rb
61
- - examples/local_lat.rb
62
- - examples/local_lat_zerocopy.rb
63
- - examples/publish_subscribe.rb
64
- - examples/remote_lat.rb
65
- - examples/remote_lat_zerocopy.rb
66
- - examples/reqrep_poll.rb
67
- - examples/request_response.rb
68
- - examples/t
69
- - examples/xreqxrep_poll.rb
70
- - ffi-rzmq.gemspec
71
- - lib/ffi-rzmq.rb
72
- - lib/ffi-rzmq/context.rb
73
- - lib/ffi-rzmq/exceptions.rb
74
- - lib/ffi-rzmq/message.rb
75
- - lib/ffi-rzmq/poll.rb
76
- - lib/ffi-rzmq/poll_items.rb
77
- - lib/ffi-rzmq/socket.rb
78
- - lib/ffi-rzmq/wrapper.rb
79
- - lib/ffi-rzmq/zmq.rb
80
- - spec/context_spec.rb
81
- - spec/reqrep_spec.rb
82
- - spec/socket_spec.rb
83
- - spec/spec_helper.rb
84
- - version.txt
58
+ - .gitignore
59
+ - History.txt
60
+ - README.rdoc
61
+ - Rakefile
62
+ - examples/README.rdoc
63
+ - examples/local_lat.rb
64
+ - examples/local_lat_zerocopy.rb
65
+ - examples/local_throughput.rb
66
+ - examples/publish_subscribe.rb
67
+ - examples/remote_lat.rb
68
+ - examples/remote_lat_zerocopy.rb
69
+ - examples/remote_throughput.rb
70
+ - examples/reqrep_poll.rb
71
+ - examples/request_response.rb
72
+ - examples/xreqxrep_poll.rb
73
+ - ffi-rzmq.gemspec
74
+ - lib/ffi-rzmq.rb
75
+ - lib/ffi-rzmq/context.rb
76
+ - lib/ffi-rzmq/exceptions.rb
77
+ - lib/ffi-rzmq/message.rb
78
+ - lib/ffi-rzmq/poll.rb
79
+ - lib/ffi-rzmq/poll_items.rb
80
+ - lib/ffi-rzmq/socket.rb
81
+ - lib/ffi-rzmq/wrapper.rb
82
+ - lib/ffi-rzmq/zmq.rb
83
+ - spec/context_spec.rb
84
+ - spec/message_spec.rb
85
+ - spec/pushpull_spec.rb
86
+ - spec/reqrep_spec.rb
87
+ - spec/socket_spec.rb
88
+ - spec/spec_helper.rb
89
+ - version.txt
85
90
  has_rdoc: true
86
91
  homepage: http://github.com/chuckremes/ffi-rzmq
87
92
  licenses: []
88
93
 
89
94
  post_install_message:
90
95
  rdoc_options:
91
- - --main
92
- - README.rdoc
96
+ - --main
97
+ - README.rdoc
93
98
  require_paths:
94
- - lib
99
+ - lib
95
100
  required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
96
102
  requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- segments:
100
- - 0
101
- version: "0"
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
102
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
103
110
  requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- segments:
107
- - 0
108
- version: "0"
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 0
115
+ version: "0"
109
116
  requirements: []
110
117
 
111
118
  rubyforge_project: ffi-rzmq
112
- rubygems_version: 1.3.6
119
+ rubygems_version: 1.3.7
113
120
  signing_key:
114
121
  specification_version: 3
115
122
  summary: This gem wraps the ZeroMQ networking library using the ruby FFI (foreign function interface)