def test_on_message_close_callback_returns_true(self): """Test the connection closing behavior of the on_message() handler when the callback returns True.""" callback = Mock(return_value=True) handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'test-topic', callback) event = Mock() handler.on_message(event) event.receiver.detach.assert_not_called() event.receiver.close.assert_called_once_with() event.connection.close.assert_called_once_with()
def test_accept_message(self, accept): def message_handler(message, data): return False, True handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'myqueue', message_handler, auto_accept=False) event = Mock() handler.on_message(event) accept.assert_called_once_with(event.delivery)
def test_handle_one_message_and_return(self): message_handler = Mock() message_handler.return_value = 'result' handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'myqueue', message_handler) event = Mock(message='hello world') handler.on_message(event) message_handler.assert_has_calls([call('hello world', None)]) event.connection.close.assert_called_once() self.assertEqual('result', handler.result)
def test_on_message_close_callback_returns_false_with_sub_name(self): """Test the connection closing behavior of the on_message() handler when the callback returns False and there is a subscription_name (a durable subscription).""" callback = Mock(return_value=False) handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'test-topic', callback, subscription_name='test-sub') event = Mock() handler.on_message(event) event.receiver.detach.assert_not_called() event.receiver.close.assert_not_called() event.connection.close.assert_not_called()
def test_on_start_no_sub_name(self): """Test the behavior of the on_start() handler when there is no subscription_name provided (a non-durable subscription).""" handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'test-topic', Mock()) event = Mock() handler.on_start(event) event.container.connect.assert_called_once_with( urls=CONSUMER_CONFIG['urls'], ssl_domain=None, heartbeat=500) conn = event.container.connect.return_value event.container.create_receiver.assert_called_once_with(conn, 'test-topic', name=None, options=[])
def test_handle_messages_continuously(self): message_handler = Mock(side_effect=[None, None, 'result']) handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'myqueue', message_handler) events = [ Mock(message='hello'), Mock(message='world'), Mock(message='!') ] [handler.on_message(event) for event in events] calls = [call(event.message, None) for event in events] message_handler.assert_has_calls(calls) events[-1].connection.close.assert_called_once() self.assertEqual('result', handler.result)
def test_fail_if_callback_not_return_a_tuple(self): def message_handler(message, data): return None handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'myqueue', message_handler, auto_accept=False) event = Mock() self.assertRaises(ValueError, handler.on_message, event)
def test_on_start_sub_name(self): """Test the behavior of the on_start() handler when there is a subscription_name provided (a durable subscription).""" handler = ReceiverHandler(CONSUMER_CONFIG['urls'], 'test-topic', Mock(), subscription_name='test-sub') event = Mock() handler.on_start(event) event.container.container_id == 'test-sub' event.container.connect.assert_called_once_with( urls=CONSUMER_CONFIG['urls'], ssl_domain=None, heartbeat=500) conn = event.container.connect.return_value event.container.create_receiver.assert_called_once_with( conn, 'test-topic', name='test-sub', options=ANY) kwargs = event.container.create_receiver.call_args[1] opts = kwargs['options'] self.assertEqual(len(opts), 1) self.assertIsInstance(opts[0], DurableSubscription)