def test_send(self): # Send auto ACK response = rpc.RPCResponse(self.connection, 'addTwo', '123') self.io_loop.call_later(1, self.stop) self.wait() self.handler.assert_called_with(msg('P|A|REQ|addTwo|123+')) # Send response response.send(14) self.handler.assert_called_with(msg('P|RES|addTwo|123|N14+'))
def test_send_no_autoack(self): # Create response but disable autoack response = rpc.RPCResponse(self.connection, 'addTwo', '123') response.auto_ack = False self.handler.assert_not_called() # Send the ack response.ack() self.handler.assert_called_with(msg('P|A|REQ|addTwo|123+')) # Do not send multiple ack messages self.handler.reset_mock() response.ack() self.handler.assert_not_called()
def test_make_rpcs(self): # RPCHandler is created rpchandler = self.client.rpc self.assertTrue(isinstance(self.client.rpc, rpc.RPCHandler)) # Make a successful RPC for addTwo rpc_callback = mock.Mock() rpchandler.make('addTwo', {'numA': 3, 'numB': 8}, rpc_callback) self.assertTrue(self.handler.call_args[0][0] in ( msg('P|REQ|addTwo|1|O{"numA":3,"numB":8}+'), msg('P|REQ|addTwo|1|O{"numB":8,"numA":3}+'))) rpchandler.handle({ 'topic': 'RPC', 'action': 'RES', 'data': ['addTwo', u'1', 'N11'] }) rpc_callback.assert_called_with(None, 11) # Make RPC for addTwo but receive an error self.assertTrue(self.handler.call_args[0][0] in ( msg('P|REQ|addTwo|1|O{"numA":3,"numB":8}+'), msg('P|REQ|addTwo|1|O{"numB":8,"numA":3}+'))) rpchandler.make('addTwo', {'numA': 3, 'numB': 8}, rpc_callback) rpchandler.handle({ 'topic': 'RPC', 'action': 'E', 'data': ['NO_PROVIDER', 'addTwo', '1'] }) rpc_callback.assert_called_with('NO_PROVIDER', None) rpc_callback.reset_mock() # Make RPC for addTwo but receive no ack in time rpchandler.make('addTwo', {'numA': 3, 'numB': 8}, rpc_callback) self.assertTrue(self.handler.call_args[0][0] in ( msg('P|REQ|addTwo|1|O{"numA":3,"numB":8}+'), msg('P|REQ|addTwo|1|O{"numB":8,"numA":3}+'))) self.connection._io_loop.call_later(2, self.stop) self.wait() rpc_callback.assert_called_with('ACK_TIMEOUT', None)
def testhandle_rpc_providers(self): self.assertEqual(self.client._factory._state, constants.connection_state.OPEN) # RPCHandler is created rpchandler = self.client.rpc self.assertTrue(isinstance(self.client.rpc, rpc.RPCHandler)) # Register a provider for addTwo RPC rpchandler.provide('addTwo', self._add_two_callback) self.handler.assert_called_once_with(msg('P|S|addTwo+')) self.assertEquals(self.rpc_calls, 0) # Timeout error emitted if no ack message received on time self.wait() expected_error = ('No ACK message received in time for addTwo', 'ACK_TIMEOUT', 'P') self.assertTrue(expected_error in self.client_errors) # Reply to a sync RPC request rpchandler.handle({ 'topic': 'RPC', 'action': 'REQ', 'data': ['addTwo', '678', 'O{"numA":2,"numB":3,"sync":true}'] }) self.wait() self.handler.assert_called_with(msg('P|RES|addTwo|678|N5+')) # Reply to an async RPC request rpchandler.handle({ 'topic': 'RPC', 'action': 'REQ', 'data': ['addTwo', '123', 'O{"numA":7,"numB":3}'] }) self.connection._io_loop.call_later(0.1, self.stop) self.wait() self.handler.assert_called_with(msg('P|A|REQ|addTwo|123+')) self.wait() self.handler.assert_called_with(msg('P|RES|addTwo|123|N10+')) # Send rejection if no provider exists rpchandler.handle({ 'topic': 'RPC', 'action': 'REQ', 'data': ['doesNotExist', '432', 'O{"numA":7,"numB":3}'] }) self.handler.assert_called_with(msg('P|REJ|doesNotExist|432+')) # Deregister provider for the addTwo RPC rpchandler.unprovide('addTwo') self.handler.assert_called_with(msg('P|US|addTwo+')) # Timeout emitted after no ACK message received for the unprovide self.client_errors = [] self.connection._io_loop.call_later(3.5, self.stop) self.wait() expected_error = ('No ACK message received in time for addTwo', 'ACK_TIMEOUT', 'P') self.assertTrue(expected_error in self.client_errors) # Reject call to deregistered provider rpchandler.handle({ 'topc': 'RPC', 'action': 'REQ', 'data': ['addTwo', '434', 'O{"numA":2,"numB":7, "sync": true}'] }) self.handler.assert_called_with(msg('P|REJ|addTwo|434+'))
def test_error(self): response = rpc.RPCResponse(self.connection, 'addTwo', '123') response.error('Error message') self.handler.assert_called_with(msg('P|E|Error message|addTwo|123+')) self.assertRaises(ValueError, functools.partial(response.send, 'abc'))
def test_reject(self): response = rpc.RPCResponse(self.connection, 'addTwo', '123') response.reject() self.handler.assert_called_with(msg('P|REJ|addTwo|123+')) self.assertRaises(ValueError, functools.partial(response.send, 'abc'))