Permalink
Browse files

Merge pull request #32 from jnicklas/specs

Add basic specs
  • Loading branch information...
2 parents 4d886e4 + 3c416f1 commit 4c2ab1af7b757ab7b5f4758afe09743c141c46ea @tarcieri tarcieri committed Feb 4, 2014
Showing with 168 additions and 4 deletions.
  1. +11 −4 lib/celluloid/zmq.rb
  2. +1 −0 lib/celluloid/zmq/waker.rb
  3. +152 −0 spec/celluloid/zmq_spec.rb
  4. +4 −0 spec/spec_helper.rb
View
@@ -10,6 +10,8 @@
module Celluloid
# Actors which run alongside 0MQ sockets
module ZMQ
+ UninitializedError = Class.new StandardError
+
class << self
attr_writer :context
@@ -19,15 +21,20 @@ def included(klass)
klass.mailbox_class Celluloid::ZMQ::Mailbox
end
- # Obtain a 0MQ context (or lazily initialize it)
- def context(worker_threads = 1)
+ # Obtain a 0MQ context
+ def init(worker_threads = 1)
return @context if @context
@context = ::ZMQ::Context.new(worker_threads)
end
- alias_method :init, :context
+
+ def context
+ raise UninitializedError, "you must initialize Celluloid::ZMQ by calling Celluloid::ZMQ.init" unless @context
+ @context
+ end
def terminate
- @context.terminate
+ @context.terminate if @context
+ @context = nil
end
end
@@ -50,6 +50,7 @@ def cleanup
@receiver.close rescue nil
nil
end
+ alias_method :shutdown, :cleanup
end
end
end
View
@@ -0,0 +1,152 @@
+require 'spec_helper'
+
+describe Celluloid::ZMQ do
+ before { @sockets = [] }
+ after { @sockets.each(&:close) }
+
+ def connect(socket, index=0)
+ socket.connect("inproc://celluloid-spec-#{index}")
+ @sockets << socket
+ socket
+ end
+
+ def bind(socket, index=0)
+ socket.bind("inproc://celluloid-spec-#{index}")
+ @sockets << socket
+ socket
+ end
+
+ describe ".init" do
+ it "inits a ZMQ context", :no_init do
+ Celluloid::ZMQ.init
+ server = bind(Celluloid::ZMQ.context.socket(::ZMQ::REQ))
+ client = connect(Celluloid::ZMQ.context.socket(::ZMQ::REP))
+
+ server.send_string("hello world")
+ message = ""
+ client.recv_string(message)
+ message.should eq("hello world")
+ end
+
+ it "can set ZMQ context manually", :no_init do
+ context = ::ZMQ::Context.new(1)
+ begin
+ Celluloid::ZMQ.context = context
+ Celluloid::ZMQ.context.should eq(context)
+ ensure
+ context.terminate
+ end
+ end
+
+ it "raises an error when trying to access context and it isn't initialized", :no_init do
+ expect { Celluloid::ZMQ.context }.to raise_error(Celluloid::ZMQ::UninitializedError)
+ end
+
+ it "raises an error when trying to access context after it is terminated" do
+ Celluloid::ZMQ.terminate
+ expect { Celluloid::ZMQ.context }.to raise_error(Celluloid::ZMQ::UninitializedError)
+ Celluloid::ZMQ.init
+ Celluloid::ZMQ.context.should_not be_nil
+ end
+ end
+
+ describe Celluloid::ZMQ::RepSocket do
+ let(:actor) do
+ Class.new do
+ include Celluloid::ZMQ
+
+ finalizer :close_socket
+
+ def initialize(index)
+ @socket = Celluloid::ZMQ::RepSocket.new
+ @socket.connect("inproc://celluloid-spec-#{index}")
+ end
+
+ def say_hi
+ "Hi!"
+ end
+
+ def fetch
+ @socket.read
+ end
+
+ def close_socket
+ @socket.close
+ end
+ end
+ end
+
+ it "receives messages" do
+ server = bind(Celluloid::ZMQ.context.socket(::ZMQ::REQ))
+ client = actor.new(0)
+
+ server.send_string("hello world")
+ result = client.fetch
+ result.should eq("hello world")
+ end
+
+ it "suspends actor while waiting for message" do
+ server = bind(Celluloid::ZMQ.context.socket(::ZMQ::REQ))
+ client = actor.new(0)
+
+ result = client.future.fetch
+ client.say_hi.should eq("Hi!")
+ server.send_string("hello world")
+ result.value.should eq("hello world")
+ end
+ end
+
+ describe Celluloid::ZMQ::ReqSocket do
+ let(:actor) do
+ Class.new do
+ include Celluloid::ZMQ
+
+ finalizer :close_socket
+
+ def initialize(index)
+ @socket = Celluloid::ZMQ::ReqSocket.new
+ @socket.connect("inproc://celluloid-spec-#{index}")
+ end
+
+ def say_hi
+ "Hi!"
+ end
+
+ def send(message)
+ @socket.write(message)
+ true
+ end
+
+ def close_socket
+ @socket.close
+ end
+ end
+ end
+
+ it "sends messages" do
+ client = bind(Celluloid::ZMQ.context.socket(::ZMQ::REP))
+ server = actor.new(0)
+
+ server.send("hello world")
+
+ message = ""
+ client.recv_string(message)
+ message.should eq("hello world")
+ end
+
+ it "suspends actor while waiting for message to be sent" do
+ client = bind(Celluloid::ZMQ.context.socket(::ZMQ::REP))
+ server = actor.new(0)
+
+ result = server.future.send("hello world")
+
+ server.say_hi.should eq("Hi!")
+
+ message = ""
+ client.recv_string(message)
+ message.should eq("hello world")
+
+ result.value.should be_true
+ end
+ end
+end
View
@@ -10,9 +10,13 @@
Celluloid.shutdown_timeout = 1
RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+
config.around do |ex|
+ Celluloid::ZMQ.init(1) unless example.metadata[:no_init]
Celluloid.boot
ex.run
Celluloid.shutdown
+ Celluloid::ZMQ.terminate
end
end

0 comments on commit 4c2ab1a

Please sign in to comment.