|
1 | 1 | require 'test_helper' |
| 2 | +require 'securerandom' |
2 | 3 |
|
3 | 4 | module M2R |
4 | 5 | class ConnectionTest < MiniTest::Unit::TestCase |
5 | | - def test_receive_message |
6 | | - request_addr = "inproc://requests" |
7 | | - response_addr = "inproc://responses" |
8 | 6 |
|
9 | | - push = M2R.zmq_context.socket(ZMQ::PUSH) |
10 | | - assert_equal 0, push.bind(request_addr), "Could not bind push socket in tests" |
| 7 | + def setup |
| 8 | + @request_addr = "inproc://requests" |
| 9 | + @response_addr = "inproc://responses" |
| 10 | + |
| 11 | + @push = M2R.zmq_context.socket(ZMQ::PUSH) |
| 12 | + assert_equal 0, @push.bind(@request_addr), "Could not bind push socket in tests" |
| 13 | + |
| 14 | + @sub = M2R.zmq_context.socket(ZMQ::SUB) |
| 15 | + assert_equal 0, @sub.bind(@response_addr), "Could not bind sub socket in tests" |
11 | 16 |
|
12 | | - sub = M2R.zmq_context.socket(ZMQ::SUB) |
13 | | - assert_equal 0, sub.bind(response_addr), "Could not bind sub socket in tests" |
14 | 17 |
|
15 | | - connection = Connection.for("a65c2d89-96ee-4bc9-971e-59b38ae24645", request_addr, response_addr) |
| 18 | + @request_socket = M2R.zmq_context.socket(ZMQ::PULL) |
| 19 | + @request_socket.connect(@request_addr) |
| 20 | + |
| 21 | + @response_socket = M2R.zmq_context.socket(ZMQ::PUB) |
| 22 | + @response_socket.connect(@response_addr) |
| 23 | + @response_socket.setsockopt(ZMQ::IDENTITY, @sender_id = SecureRandom.uuid) |
| 24 | + end |
16 | 25 |
|
17 | | - push.send_string("1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) |
| 26 | + def teardown |
| 27 | + @request_socket.close |
| 28 | + @response_socket.close |
| 29 | + @push.close |
| 30 | + @sub.close |
| 31 | + end |
18 | 32 |
|
| 33 | + def test_receive_message |
| 34 | + connection = Connection.new(@request_socket, @response_socket) |
| 35 | + @push.send_string("1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) |
19 | 36 | assert_instance_of Request, connection.receive |
20 | 37 | end |
| 38 | + |
| 39 | + def test_different_parser |
| 40 | + msg = "1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:," |
| 41 | + parser = stub(:parser) |
| 42 | + parser.expects(:parse).with(msg).returns(request = Object.new) |
| 43 | + connection = Connection.new(@request_socket, @response_socket, parser) |
| 44 | + @push.send_string(msg = "1c5fd481-1121-49d8-a706-69127975db1a ebb407b2-49aa-48a5-9f96-9db121051484 / 2:{},0:,", ZMQ::NOBLOCK) |
| 45 | + assert_equal request, connection.receive |
| 46 | + end |
| 47 | + |
21 | 48 | end |
22 | 49 | end |
0 commit comments