forked from perplexes/m2r
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In the future we would like to use multiple threads. Every thread will obtain requests from separate `ZMQ` connection as it is recommended in the tutorial. Because of that we will have to create the sockets for every thread. For that reason `Handler` takes now in constructor a factory capable of creating new `Connection` instead of the `Connection` itself. `Connection#connection` method is introduced so that `Connection` has interface compatible with `ConnectionFactory` and can be used instead returning always `self` whenever `connection` is needed. That should work fine for simplest testing case or in single-threaded environment.
- Loading branch information
Showing
12 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'm2r' | ||
|
||
module M2R | ||
class ConnectionFactory | ||
|
||
def initialize(sender_id, request_addr, response_addr, request_parser = Request, context = M2R.zmq_context) | ||
@sender_id = sender_id.to_s | ||
@request_addr = request_addr.to_s | ||
@response_addr = response_addr.to_s | ||
@request_parser = request_parser | ||
@context = context | ||
end | ||
|
||
def connection | ||
request_socket = @context.socket(ZMQ::PULL) | ||
request_socket.connect(@request_addr) | ||
|
||
response_socket = @context.socket(ZMQ::PUB) | ||
response_socket.connect(@response_addr) | ||
response_socket.setsockopt(ZMQ::IDENTITY, @sender_id) | ||
|
||
Connection.new(request_socket, response_socket, @request_parser) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'test_helper' | ||
require 'securerandom' | ||
|
||
module M2R | ||
class ConnectionFactoryTest < MiniTest::Unit::TestCase | ||
def test_factory | ||
sender_id = "sid" | ||
request_addr = "req" | ||
response_addr = "req" | ||
request_parser = Object.new | ||
|
||
pull = stub(:pull) | ||
pub = stub(:pub) | ||
context = stub(:context) | ||
|
||
context.expects(:socket).with(ZMQ::PULL).returns(pull) | ||
context.expects(:socket).with(ZMQ::PUB).returns(pub) | ||
|
||
pull.expects(:connect).with(request_addr) | ||
|
||
pub.expects(:connect).with(response_addr) | ||
pub.expects(:setsockopt).with(ZMQ::IDENTITY, sender_id) | ||
|
||
Connection.expects(:new).with(pull, pub, request_parser) | ||
cf = ConnectionFactory.new sender_id, request_addr, response_addr, request_parser, context | ||
cf.connection | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,49 @@ | ||
require 'test_helper' | ||
require 'securerandom' | ||
|
||
module M2R | ||
class ConnectionTest < MiniTest::Unit::TestCase | ||
def test_receive_message | ||
request_addr = "inproc://requests" | ||
response_addr = "inproc://responses" | ||
|
||
push = M2R.zmq_context.socket(ZMQ::PUSH) | ||
assert_equal 0, push.bind(request_addr), "Could not bind push socket in tests" | ||
def setup | ||
@request_addr = "inproc://requests" | ||
@response_addr = "inproc://responses" | ||
|
||
@push = M2R.zmq_context.socket(ZMQ::PUSH) | ||
assert_equal 0, @push.bind(@request_addr), "Could not bind push socket in tests" | ||
|
||
@sub = M2R.zmq_context.socket(ZMQ::SUB) | ||
assert_equal 0, @sub.bind(@response_addr), "Could not bind sub socket in tests" | ||
|
||
sub = M2R.zmq_context.socket(ZMQ::SUB) | ||
assert_equal 0, sub.bind(response_addr), "Could not bind sub socket in tests" | ||
|
||
connection = Connection.for("a65c2d89-96ee-4bc9-971e-59b38ae24645", request_addr, response_addr) | ||
@request_socket = M2R.zmq_context.socket(ZMQ::PULL) | ||
@request_socket.connect(@request_addr) | ||
|
||
@response_socket = M2R.zmq_context.socket(ZMQ::PUB) | ||
@response_socket.connect(@response_addr) | ||
@response_socket.setsockopt(ZMQ::IDENTITY, @sender_id = SecureRandom.uuid) | ||
end | ||
|
||
push.send_string("1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) | ||
def teardown | ||
@request_socket.close | ||
@response_socket.close | ||
@push.close | ||
@sub.close | ||
end | ||
|
||
def test_receive_message | ||
connection = Connection.new(@request_socket, @response_socket) | ||
@push.send_string("1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) | ||
assert_instance_of Request, connection.receive | ||
end | ||
|
||
def test_different_parser | ||
msg = "1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:," | ||
parser = stub(:parser) | ||
parser.expects(:parse).with(msg).returns(request = Object.new) | ||
connection = Connection.new(@request_socket, @response_socket, parser) | ||
@push.send_string(msg = "1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) | ||
assert_equal request, connection.receive | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters