test/unit/connection_test.rb in m2r-1.0.0 vs test/unit/connection_test.rb in m2r-2.0.0

- old
+ new

@@ -11,10 +11,11 @@ @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.setsockopt(ZMQ::SUBSCRIBE, "") @request_socket = M2R.zmq_context.socket(ZMQ::PULL) @request_socket.connect(@request_addr) @@ -30,20 +31,70 @@ @sub.close if @sub 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 + @push.send_string(msg = "1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) + assert_equal msg, 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 + def test_deliver_message + connection = Connection.new(@request_socket, @response_socket) + connection.deliver('uuid', ['conn1', 'conn2'], 'ddaattaa') + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 11:conn1 conn2, ddaattaa", msg + end + + def test_string_replay_non_close + connection = Connection.new(@request_socket, @response_socket) + connection.reply( stub(sender: 'uuid', conn_id: 'conn1', close?: false), 'ddaattaa') + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ddaattaa", msg + assert_equal -1, @sub.recv_string(msg = "", ZMQ::NOBLOCK) + end + + def test_string_replay_close + connection = Connection.new(@request_socket, @response_socket) + connection.reply( stub(sender: 'uuid', conn_id: 'conn1', close?: true), 'ddaattaa') + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ddaattaa", msg + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ", msg + end + + def test_response_replay_non_close + connection = Connection.new(@request_socket, @response_socket) + connection.reply( stub(sender: 'uuid', conn_id: 'conn1'), mock(to_s: 'ddaattaa', close?: false)) + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ddaattaa", msg + assert_equal -1, @sub.recv_string(msg = "", ZMQ::NOBLOCK) + end + + def test_response_replay_close + connection = Connection.new(@request_socket, @response_socket) + connection.reply( stub(sender: 'uuid', conn_id: 'conn1'), mock(to_s: 'ddaattaa', close?: true)) + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ddaattaa", msg + assert_equal 0, @sub.recv_string(msg = "") + assert_equal "uuid 5:conn1, ", msg + end + + def test_exception_when_receiving + request_socket = mock(:recv_string => -1) + connection = Connection.new request_socket, nil + assert_raises(Connection::Error) { connection.receive } + end + + def test_exception_when_deliverying + response_socket = mock(:send_string => -1) + connection = Connection.new nil, response_socket + assert_raises(Connection::Error) { connection.deliver('uuid', ['connection_ids'], 'data') } + end + + def test_exception_when_replying + response_socket = mock(:send_string => -1) + connection = Connection.new nil, response_socket + assert_raises(Connection::Error) { connection.reply( Struct.new(:sender, :conn_id).new('sender', 'conn_id') , 'data' ) } end end end