class WhenInitializingConnectionAttempt(_BaseTestCase): __contexts__ = ( ('shuffle', patch(mod + '.random.shuffle')), ) def configure(self): self.broker = Broker() self.broker._nodes = sentinel.nodes self.broker._abort_on_error = True def execute(self): self.broker._initialize_connection_attempt( sentinel.connection_attempts) def should_shuffle_nodes(self): self.ctx.shuffle.assert_called_once_with(sentinel.nodes) def should_set_attempts(self): self.assertEqual(self.broker._attempts, 0) def should_set_max_attempts(self): self.assertEqual(self.broker._max_attempts, sentinel.connection_attempts) def should_initialize_abort_on_error_flag(self): self.assertFalse(self.broker._abort_on_error)
class WhenReachingMaximumConnectionAttempts(_BaseTestCase): def configure(self): self.broker = Broker() self.broker._initialize_connection_attempt(max_attempts=1) def execute(self): self.broker._increment_connection_attempts() def should_activate_abort_on_error_flag(self): self.assertTrue(self.broker._abort_on_error)
class WhenUsingUnlimitedConnectionAttempts(_BaseTestCase): def configure(self): self.broker = Broker() self.broker._initialize_connection_attempt(max_attempts=None) def execute(self): self.broker._increment_connection_attempts() def should_preserve_abort_on_error_flag(self): self.assertFalse(self.broker._abort_on_error)
class WhenIncrementingConnectionAttempts(_BaseTestCase): def configure(self): self.broker = Broker() self.broker._initialize_connection_attempt(max_attempts=2) def execute(self): self.broker._increment_connection_attempts() def should_increment_attempts(self): self.assertEqual(self.broker._attempts, 1) def should_preserve_abort_on_error_flag(self): self.assertFalse(self.broker._abort_on_error)
class WhenExhaustingConnectionAttempts(_BaseTestCase): __contexts__ = (('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ) def setUp(self): pass def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) def should_raise_exception(self): with self.context() as self.ctx: self.configure() with self.assertRaisesRegexp(AMQPConnectionError, r'^2$'): self.broker.connect()
class WhenGettingConnectionParameters(_BaseTestCase): __contexts__ = ( ('ConnectionParameters', patch(mod + '.ConnectionParameters', return_value=sentinel.parameters)), ) def configure(self): self.broker = Broker() self.options = MagicMock() self.broker._connect_options = NonCallableMagicMock() self.broker._connect_options.copy.return_value = self.options def execute(self): self.parameters = self.broker._get_connection_parameters( sentinel.node_parameters) def should_create_connection_parameters(self): self.ctx.ConnectionParameters.assert_called_once_with(**self.options) def should_return_connection_parameters(self): self.assertIs(self.parameters, sentinel.parameters) def should_copy_connect_options(self): self.broker._connect_options.copy.assert_called_once_with() def should_merge_node_parameters(self): self.options.update.assert_called_once_with(sentinel.node_parameters)
class WhenExhaustingConnectionAttempts(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ) def setUp(self): pass def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) def should_raise_exception(self): with self.context() as self.ctx: self.configure() with self.assertRaisesRegexp(AMQPConnectionError, r'^2$'): self.broker.connect()
class WhenExhaustingConnectionAttemptsWithCallback(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ('BrokerConnectionError', patch(mod + '.BrokerConnectionError', return_value=sentinel.broker_error)) ) def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) self.on_failure_callback = MagicMock() def execute(self): self.broker.connect(on_failure_callback=self.on_failure_callback) def should_invoke_on_failure_callback(self): self.on_failure_callback.assert_called_once_with(sentinel.broker_error)
class WhenExhaustingConnectionAttemptsWithCallback(_BaseTestCase): __contexts__ = (('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ('BrokerConnectionError', patch(mod + '.BrokerConnectionError', return_value=sentinel.broker_error))) def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) self.on_failure_callback = MagicMock() def execute(self): self.broker.connect(on_failure_callback=self.on_failure_callback) def should_invoke_on_failure_callback(self): self.on_failure_callback.assert_called_once_with(sentinel.broker_error)
class WhenPublishingMessage(_BaseTestCase): host = 'localhost' user = '******' virtual_host = '/integration' admin = pyrabbit.api.Client(host + ':15672', user, 'guest') broker = Broker({'rabbitmq': { 'host': host }}, {'virtual_host': virtual_host}) exchange = 'my.exchange' routing_key = 'my.routing.key' queue = 'my.queue' payload = "It's not wise to upset a Wookie." @property def escaped_vhost(self): return self.virtual_host.replace('/', '%2F') def configure(self): self._cleanup_virtual_host() self._init_virtual_host() def execute(self): BlockingPublisher(self.broker).publish(self.exchange, self.routing_key, self.payload) def tearDown(self): super(WhenPublishingMessage, self).tearDown() self._cleanup_virtual_host() def _cleanup_virtual_host(self): if self.virtual_host in self.admin.get_vhost_names(): self.admin.delete_vhost(self.escaped_vhost) def _init_virtual_host(self): self.admin.create_vhost(self.escaped_vhost) self.admin.set_vhost_permissions(self.escaped_vhost, self.user, config='.*', rd='.*', wr='.*') self.admin.create_exchange(self.escaped_vhost, self.exchange, 'direct') self.admin.create_queue(self.escaped_vhost, self.queue) self.admin.create_binding(self.escaped_vhost, self.exchange, self.queue, self.routing_key) def should_publish_message(self): messages = self.admin.get_messages(self.escaped_vhost, self.queue) expected = [{ 'exchange': self.exchange, 'routing_key': self.routing_key, 'payload': self.payload, 'payload_bytes': len(self.payload), 'message_count': 0, 'payload_encoding': 'string', 'redelivered': False, 'properties': [], }] self.assertEqual(messages, expected)
class WhenGettingSynchronousConnection(_BaseConnectionTestCase): def configure(self): self.broker = Broker(self.nodes, self.connect_options) def execute(self): self.connection = self.broker.connect(blocking=True) def should_return_connection(self): self.assertIsInstance(self.connection, BlockingConnection)
class WhenAbortingConnectionAttemptWithCallback(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ('BrokerConnectionError', patch(mod + '.BrokerConnectionError')), ('_initialize_connection_attempt', patch(sut + '._initialize_connection_attempt')), ('_increment_connection_attempts', patch(sut + '._increment_connection_attempts')), ) def configure(self): self.broker = Broker() self.broker._abort_on_error = True self.broker._attempts = 1 self.on_failure_callback = MagicMock(autospec=True) self.ctx.BrokerConnectionError.return_value = sentinel.exception def execute(self): self.broker.connect(on_failure_callback=self.on_failure_callback) def should_invoke_on_failure_callback(self): self.on_failure_callback.assert_called_once_with(sentinel.exception)
class WhenAbortingConnectionAttempt(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=AMQPConnectionError(1))), ('_initialize_connection_attempt', patch(sut + '._initialize_connection_attempt')), ('_increment_connection_attempts', patch(sut + '._increment_connection_attempts')), ) def setUp(self): pass def configure(self): self.broker = Broker() self.broker._abort_on_error = True self.broker._attempts = 1 def should_raise_exception(self): with self.context() as self.ctx: self.configure() with self.assertRaisesRegexp(AMQPConnectionError, '^1$'): self.broker.connect()
class WhenEncounteringConnectionErrors(_BaseTestCase): __contexts__ = (('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=[ AMQPConnectionError(1), AMQPConnectionError(1), TornadoConnection ])), ) def configure(self): self.ctx.TornadoConnection._adapter_connect = MagicMock() self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) def execute(self): self.connection = self.broker.connect(connection_attempts=3) def should_try_try_again(self): self.assertIsNotNone(self.connection)
class WhenEncounteringConnectionErrors(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=[AMQPConnectionError(1), AMQPConnectionError(1), TornadoConnection])), ) def configure(self): self.ctx.TornadoConnection._adapter_connect = MagicMock() self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) def execute(self): self.connection = self.broker.connect(connection_attempts=3) def should_try_try_again(self): self.assertIsNotNone(self.connection)
class WhenRecyclingNodesWithDelay(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=[AMQPConnectionError(1), AMQPConnectionError(1), TornadoConnection])), ('sleep', patch(mod + '.time.sleep')), ) def configure(self): self.ctx.TornadoConnection._adapter_connect = MagicMock() self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) def execute(self): self.connection = self.broker.connect(connection_attempts=5, cycle_delay=sentinel.delay) def should_sleep(self): self.ctx.sleep.assert_called_once_with(sentinel.delay)
class WhenGettingAsynchronousConnection(_BaseConnectionTestCase): def configure(self): self.broker = Broker(self.nodes, self.connect_options) self.on_open_callback = MagicMock() def execute(self): self.connection = self.broker.connect(self.on_open_callback, stop_ioloop_on_close=True) def should_return_connection(self): self.assertIsInstance(self.connection, TornadoConnection) def should_register_on_open_callback(self): self.connection.callbacks.process(0, '_on_connection_open', sentinel.caller, self.connection) self.on_open_callback.assert_called_once_with(self.connection) def should_set_stop_ioloop_on_close(self): self.assertTrue(self.connection.stop_ioloop_on_close)
class WhenGettingAsynchronousConnection(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', return_value=sentinel.connection)), ('_get_connection_parameters', patch(sut + '._get_connection_parameters', return_value=sentinel.parameters)), ('_initialize_connection_attempt', patch(sut + '._initialize_connection_attempt')), ('_increment_connection_attempts', patch(sut + '._increment_connection_attempts')), ) nodes = {'localhost': {'host': '127.0.0.1'}} def configure(self): self.broker = Broker(self.nodes) def execute(self): self.connection = self.broker.connect( connection_attempts=sentinel.connection_attempts) def should_initialize_connection_attempt(self): self.ctx._initialize_connection_attempt.assert_called_once_with( sentinel.connection_attempts) def should_increment_connection_attempts(self): self.ctx._increment_connection_attempts.assert_called_once_with() def should_get_connection_parameters(self): self.ctx._get_connection_parameters.assert_called_once_with( self.nodes['localhost']) def should_create_connection(self): self.ctx.TornadoConnection.assert_called_once_with( sentinel.parameters, on_open_callback=None, stop_ioloop_on_close=False) def should_return_connection(self): self.assertIs(self.connection, sentinel.connection)
class WhenConnectingWithOnOpenCallback(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', return_value=sentinel.connection)), ('_get_connection_parameters', patch(sut + '._get_connection_parameters', return_value=sentinel.parameters)), ) def configure(self): self.ctx.TornadoConnection.return_value = sentinel.connection self.broker = Broker() self.on_open_callback = MagicMock() def execute(self): self.connection = self.broker.connect(self.on_open_callback) def should_create_connection_with_callback(self): self.ctx.TornadoConnection.assert_called_once_with( sentinel.parameters, on_open_callback=self.on_open_callback, stop_ioloop_on_close=False)
class WhenEncounteringConnectionError(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=[AMQPConnectionError(1), TornadoConnection])), ('_initialize_connection_attempt', patch(sut + '._initialize_connection_attempt')), ('_increment_connection_attempts', patch(sut + '._increment_connection_attempts')), ) nodes = {'rabbit1': {}, 'rabbit2': {}} def configure(self): self.broker = Broker(self.nodes) self.broker._nodes = PropertyMock() self.broker._nodes.__iter__ = MagicMock( return_value=iter(self.nodes.items())) def execute(self): self.connection = self.broker.connect() def should_iterate_over_nodes(self): self.broker._nodes.__iter__.assert_called_once_with()
class WhenExhaustingNodeList(_BaseTestCase): __contexts__ = ( ('TornadoConnection', patch(mod + '.TornadoConnection', side_effect=[AMQPConnectionError(1), AMQPConnectionError(1), TornadoConnection])), ('_initialize_connection_attempt', patch(sut + '._initialize_connection_attempt')), ('_increment_connection_attempts', patch(sut + '._increment_connection_attempts')), ) nodes = {'rabbit1': {}, 'rabbit2': {}} def configure(self): self.broker = Broker(self.nodes) self.broker._nodes = PropertyMock() get_node_iterator = lambda: iter(self.nodes.items()) self.broker._nodes.__iter__ = MagicMock(side_effect=get_node_iterator) def execute(self): self.connection = self.broker.connect() def should_recycle_nodes(self): self.assertEqual(len(self.broker._nodes.__iter__.mock_calls), 2)
def configure(self): self.broker = Broker(self.nodes, self.connect_options)
def configure(self): self.ctx.TornadoConnection.return_value = sentinel.connection self.broker = Broker() self.on_open_callback = MagicMock()
def configure(self): self.broker = Broker(self.nodes, self.connect_options) self.on_open_callback = MagicMock()
def configure(self): self.consumer = Consumer() self.broker = Broker() self.bindings = [('myqueue', 'myexchange', 'my.routing.key')]
class AsyncAgentTestCase(AsyncTestCase): host = 'localhost' user = '******' virtual_host = '/integration' admin = pyrabbit.api.Client(host + ':15672', user, 'guest') broker = Broker({'rabbitmq': { 'host': host }}, {'virtual_host': virtual_host}) consumer = LoggingConsumer() bindings = ({ 'queue': 'my.queue', 'exchange': 'my.exchange', 'routing_key': 'my.routing.key', }, ) config = { 'exchanges': { 'my.exchange': { 'exchange_type': 'topic', 'durable': True } }, 'queues': { 'my.queue': { 'durable': True, 'arguments': { 'x-ha-policy': 'all' } } }, } payload = "It's not wise to upset a Wookie." def get_new_ioloop(self): return self.agent.connection.ioloop @cached_property def agent(self): agent = MockAgent(self.consumer, self.broker, self.bindings, False, self.config, stop_callback=self.stop) agent.connect() return agent @property def escaped_vhost(self): return self.virtual_host.replace('/', '%2F') def setUp(self): self._cleanup_virtual_host() self._init_virtual_host() super(AsyncAgentTestCase, self).setUp() self.wait() def tearDown(self): super(AsyncAgentTestCase, self).tearDown() self._cleanup_virtual_host() del self.agent def _cleanup_virtual_host(self): if self.virtual_host in self.admin.get_vhost_names(): self.admin.delete_vhost(self.escaped_vhost) def _init_virtual_host(self): self.admin.create_vhost(self.escaped_vhost) self.admin.set_vhost_permissions(self.escaped_vhost, self.user, config='.*', rd='.*', wr='.*') def _poll_queue_statistics(self, queue, callback): attempts = 10 for i in range(attempts): queue_stats = self.admin.get_queue(self.escaped_vhost, queue) if callback(queue_stats): return queue_stats time.sleep(1) raise AssertionError('Callback condition not satisfied after %d ' 'attempts in _poll_queue_statistics' % attempts)
def configure(self): self.broker = Broker() self.broker._initialize_connection_attempt(max_attempts=None)
def configure(self): self.broker = Broker() self.broker._abort_on_error = True self.broker._attempts = 1 self.on_failure_callback = MagicMock(autospec=True) self.ctx.BrokerConnectionError.return_value = sentinel.exception
def execute(self): self.broker = Broker()
def execute(self): self.broker = Broker(None, self.connect_options)
def configure(self): self.broker = Broker() self.options = MagicMock() self.broker._connect_options = NonCallableMagicMock() self.broker._connect_options.copy.return_value = self.options
def configure(self): self.broker = Broker() self.broker._abort_on_error = True self.broker._attempts = 1
def configure(self): self.broker = Broker(self.nodes) self.broker._nodes = PropertyMock() get_node_iterator = lambda: iter(self.nodes.items()) self.broker._nodes.__iter__ = MagicMock(side_effect=get_node_iterator)
def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}}) self.on_failure_callback = MagicMock()
def configure(self): self.ctx.TornadoConnection._adapter_connect = MagicMock() self.broker = Broker({'rabbit1': {}, 'rabbit2': {}})
def configure(self): self.broker = Broker({'rabbit1': {}, 'rabbit2': {}})
def configure(self): self.broker = Broker(self.nodes) self.broker._nodes = PropertyMock() self.broker._nodes.__iter__ = MagicMock( return_value=iter(self.nodes.items()))
def execute(self): self.broker = Broker(self.nodes)
def broker_from_config(config): """Create a :class:`pikachewie.broker.Broker` from the given `config`.""" options = config.copy() nodes = options.pop('nodes') return Broker(nodes, options)
def configure(self): self.broker = Broker() self.broker._nodes = sentinel.nodes self.broker._abort_on_error = True