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()
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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 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)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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 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)
Exemplo n.º 10
0
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 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)
Exemplo n.º 12
0
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)
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)
Exemplo n.º 15
0
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)
Exemplo n.º 16
0
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()
Exemplo n.º 17
0
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)