Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Device Class + Specs added

  • Loading branch information...
commit 0caeeb725d01ebda9c936b1b4a1ae6cbdaafb78b 1 parent 4847a3b
@andrewvc andrewvc authored
Showing with 84 additions and 1 deletion.
  1. +1 −1  lib/ffi-rzmq.rb
  2. +15 −0 lib/ffi-rzmq/device.rb
  3. +68 −0 spec/device_spec.rb
View
2  lib/ffi-rzmq.rb
@@ -67,6 +67,6 @@ def self.require_all_libs_relative_to( fname, dir = nil )
RBX = defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/ ? true : false
# the order of files is important
-%w(wrapper zmq exceptions context message socket poll_items poll).each do |file|
+%w(wrapper zmq exceptions context message socket poll_items poll device).each do |file|
require ZMQ.libpath(['ffi-rzmq', file])
end
View
15 lib/ffi-rzmq/device.rb
@@ -0,0 +1,15 @@
+module ZMQ
+ class Device
+ attr_reader :device
+
+ def initialize(device_type,frontend,backend)
+ [["frontend", frontend],["backend", backend]].each do |name,socket|
+ unless socket.is_a?(ZMQ::Socket)
+ raise ArgumentError, "Expected a ZMQ::Socket, not a #{socket.class} as the #{name}"
+ end
+ end
+
+ LibZMQ.zmq_device(device_type, frontend.socket, backend.socket)
+ end
+ end
+end
View
68 spec/device_spec.rb
@@ -0,0 +1,68 @@
+
+require File.join(File.dirname(__FILE__), %w[spec_helper])
+
+module ZMQ
+ describe Device do
+ include APIHelper
+
+ def create_streamer
+ @back_addr = "tcp://127.0.0.1:#{random_port}"
+ @front_addr = "tcp://127.0.0.1:#{random_port}"
+ Thread.new do
+ back = SPEC_CTX.socket(ZMQ::PULL)
+ back.bind(@back_addr)
+ front = SPEC_CTX.socket(ZMQ::PUSH)
+ front.bind(@front_addr)
+ Device.new(ZMQ::STREAMER, back, front)
+ end
+ end
+
+ it "should create a streamer device without error given valid opts" do
+ create_streamer
+ end
+
+ it "should be able to send messages through the device" do
+ create_streamer
+
+ pusher = SPEC_CTX.socket(ZMQ::PUSH)
+ pusher.connect(@back_addr)
+ puller = SPEC_CTX.socket(ZMQ::PULL)
+ puller.connect(@front_addr)
+
+ pusher.send_string("hello")
+ sleep 0.5
+ res = puller.recv_string(ZMQ::NOBLOCK)
+ res.should == "hello"
+ end
+
+ it "should raise an ArgumentError when trying to pass non-socket objects into the device" do
+ lambda {
+ Device.new(ZMQ::STREAMER, 1,2)
+ }.should raise_exception(ArgumentError)
+ end
+
+ it "should be able to create a forwarder device without error" do
+ back_addr = "tcp://127.0.0.1:#{random_port}"
+ front_addr = "tcp://127.0.0.1:#{random_port}"
+ Thread.new do
+ back = SPEC_CTX.socket(ZMQ::SUB)
+ back.bind(back_addr)
+ front = SPEC_CTX.socket(ZMQ::PUB)
+ front.bind(front_addr)
+ Device.new(ZMQ::FORWARDER, back, front)
+ end
+ end
+
+ it "should be able to create a queue device without error" do
+ back_addr = "tcp://127.0.0.1:#{random_port}"
+ front_addr = "tcp://127.0.0.1:#{random_port}"
+ Thread.new do
+ back = SPEC_CTX.socket(ZMQ::XREP)
+ back.bind(back_addr)
+ front = SPEC_CTX.socket(ZMQ::XREQ)
+ front.bind(front_addr)
+ Device.new(ZMQ::QUEUE, back, front)
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.