Exemplo n.º 1
0
    def test_close_connection(self):
        connection = Mock()
        connection.client = Mock()
        self.transport.close_connection(connection)

        self.assertIsNone(connection.client)
        connection.close.assert_called_with()
Exemplo n.º 2
0
    def test_verify_connection(self):
        connection = Mock()
        connection.channels = None
        self.assertFalse(self.transport.verify_connection(connection))

        connection.channels = {1: 1, 2: 2}
        self.assertTrue(self.transport.verify_connection(connection))
Exemplo n.º 3
0
    def test_register_BRPOP(self):
        p = self.Poller()
        channel = Mock()
        channel.client.connection._sock = None
        p._register = Mock()

        channel._in_poll = False
        p._register_BRPOP(channel)
        self.assertEqual(channel._brpop_start.call_count, 1)
        self.assertEqual(p._register.call_count, 1)

        channel.client.connection._sock = Mock()
        p._chan_to_sock[(channel, channel.client, 'BRPOP')] = True
        channel._in_poll = True
        p._register_BRPOP(channel)
        self.assertEqual(channel._brpop_start.call_count, 1)
        self.assertEqual(p._register.call_count, 1)
Exemplo n.º 4
0
    def test_register_LISTEN(self):
        p = self.Poller()
        channel = Mock()
        channel.subclient.connection._sock = None
        channel._in_listen = False
        p._register = Mock()

        p._register_LISTEN(channel)
        p._register.assert_called_with(channel, channel.subclient, 'LISTEN')
        self.assertEqual(p._register.call_count, 1)
        self.assertEqual(channel._subscribe.call_count, 1)

        channel._in_listen = True
        channel.subclient.connection._sock = Mock()
        p._register_LISTEN(channel)
        self.assertEqual(p._register.call_count, 1)
        self.assertEqual(channel._subscribe.call_count, 1)
Exemplo n.º 5
0
    def create_get(self, events=None, queues=None, fanouts=None):
        _pr = [] if events is None else events
        _aq = [] if queues is None else queues
        _af = [] if fanouts is None else fanouts
        p = self.Poller()
        p.poller = Mock()
        p.poller.poll.return_value = _pr

        p._register_BRPOP = Mock()
        p._register_LISTEN = Mock()

        channel = Mock()
        p._channels = [channel]
        channel.active_queues = _aq
        channel.active_fanout_queues = _af

        return p, channel
Exemplo n.º 6
0
    def test_has_queue(self):
        self.channel._in_poll = False
        exists = self.channel.client.exists = Mock()
        exists.return_value = True
        self.assertTrue(self.channel._has_queue('foo'))
        exists.assert_has_call('foo')

        exists.return_value = False
        self.assertFalse(self.channel._has_queue('foo'))
Exemplo n.º 7
0
 def test_receive(self):
     s = self.channel.subclient = Mock()
     self.channel._fanout_to_queue['a'] = 'b'
     s.parse_response.return_value = [
         'message', 'a', dumps({'hello': 'world'})
     ]
     payload, queue = self.channel._receive()
     self.assertDictEqual(payload, {'hello': 'world'})
     self.assertEqual(queue, 'b')
Exemplo n.º 8
0
    def test_basic_cancel_not_in_active_queues(self):
        c = self.channel
        c._consumers.add('x')
        c._tag_to_queue['x'] = 'foo'
        c._active_queues = Mock()
        c._active_queues.remove.side_effect = ValueError()

        c.basic_cancel('x')
        c._active_queues.remove.assert_called_with('foo')
Exemplo n.º 9
0
 def test_receive(self):
     s = self.channel.subclient = Mock()
     self.channel._fanout_to_queue["a"] = "b"
     s.parse_response.return_value = [
         "message", "a", dumps({"hello": "world"})
     ]
     payload, queue = self.channel._receive()
     self.assertDictEqual(payload, {"hello": "world"})
     self.assertEqual(queue, "b")
Exemplo n.º 10
0
    def test_close_unregisters_fds(self):
        p = self.Poller()
        poller = p.poller = Mock()
        p._chan_to_sock.update({1: 1, 2: 2, 3: 3})

        p.close()

        self.assertEqual(poller.unregister.call_count, 3)
        u_args = poller.unregister.call_args_list

        self.assertItemsEqual(u_args, [((1, ), {}), ((2, ), {}), ((3, ), {})])
Exemplo n.º 11
0
    def test_restore_unacked_once_when_unrestored(self, say,
                                                  emergency_dump_state):
        q = self.channel.qos
        q._flush = Mock()

        class State(dict):
            restored = False

        q._delivered = State({1: 1})
        ru = q.restore_unacked = Mock()
        exc = None
        try:
            raise KeyError()
        except KeyError as exc_:
            exc = exc_
        ru.return_value = [(exc, 1)]

        self.channel.do_restore = True
        q.restore_unacked_once()
        self.assertTrue(say.called)
        self.assertTrue(emergency_dump_state.called)
Exemplo n.º 12
0
    def test_register_when_registered_reregisters(self):
        p = self.Poller()
        p.poller = Mock()
        channel, client, type = Mock(), Mock(), Mock()
        sock = client.connection._sock = Mock()
        sock.fileno.return_value = 10

        p._chan_to_sock = {(channel, client, type): 6}
        p._register(channel, client, type)
        p.poller.unregister.assert_called_with(6)
        self.assertTupleEqual(p._fd_to_chan[10], (channel, type))
        self.assertEqual(p._chan_to_sock[(channel, client, type)], sock)
        p.poller.register.assert_called_with(sock, p.eventflags)

        # when client not connected yet
        client.connection._sock = None

        def after_connected():
            client.connection._sock = Mock()
        client.connection.connect.side_effect = after_connected

        p._register(channel, client, type)
        client.connection.connect.assert_called_with()
Exemplo n.º 13
0
    def test_subscribe(self):
        self.channel.subclient = Mock()
        self.channel.active_fanout_queues.add('a')
        self.channel.active_fanout_queues.add('b')
        self.channel._fanout_queues.update(a='a', b='b')

        self.channel._subscribe()
        self.assertTrue(self.channel.subclient.subscribe.called)
        s_args, _ = self.channel.subclient.subscribe.call_args
        self.assertItemsEqual(s_args[0], ['a', 'b'])

        self.channel.subclient.connection._sock = None
        self.channel._subscribe()
        self.channel.subclient.connection.connect.assert_called_with()
Exemplo n.º 14
0
    def create_get(self, events=None, queues=None, fanouts=None):
        _pr = [] if events is None else events
        _aq = [] if queues is None else queues
        _af = [] if fanouts is None else fanouts
        p = self.Poller()
        p.poller = Mock()
        p.poller.poll.return_value = _pr

        p._register_BRPOP = Mock()
        p._register_LISTEN = Mock()

        channel = Mock()
        p._channels = [channel]
        channel.active_queues = _aq
        channel.active_fanout_queues = _af

        return p, channel
Exemplo n.º 15
0
    def setUp(self):
        if pyamqp is None:
            raise SkipTest('py-amqp not installed')

        class Channel(pyamqp.Channel):
            wait_returns = []

            def _x_open(self, *args, **kwargs):
                pass

            def wait(self, *args, **kwargs):
                return self.wait_returns

            def _send_method(self, *args, **kwargs):
                pass

        self.conn = Mock()
        self.conn.channels = {}
        self.channel = Channel(self.conn, 0)
Exemplo n.º 16
0
    def setUp(self):
        if pyamqp is None:
            raise SkipTest('py-amqp not installed')

        class Channel(pyamqp.Channel):
            wait_returns = []

            def _x_open(self, *args, **kwargs):
                pass

            def wait(self, *args, **kwargs):
                return self.wait_returns

            def _send_method(self, *args, **kwargs):
                pass

        self.conn = Mock()
        self.conn._get_free_channel_id.side_effect = partial(next, count(0))
        self.conn.channels = {}
        self.channel = Channel(self.conn, 0)
Exemplo n.º 17
0
    def test_register_BRPOP(self):
        p = self.Poller()
        channel = Mock()
        channel.client.connection._sock = None
        p._register = Mock()

        channel._in_poll = False
        p._register_BRPOP(channel)
        self.assertEqual(channel._brpop_start.call_count, 1)
        self.assertEqual(p._register.call_count, 1)

        channel.client.connection._sock = Mock()
        p._chan_to_sock[(channel, channel.client, 'BRPOP')] = True
        channel._in_poll = True
        p._register_BRPOP(channel)
        self.assertEqual(channel._brpop_start.call_count, 1)
        self.assertEqual(p._register.call_count, 1)
Exemplo n.º 18
0
    def test_register_LISTEN(self):
        p = self.Poller()
        channel = Mock()
        channel.subclient.connection._sock = None
        channel._in_listen = False
        p._register = Mock()

        p._register_LISTEN(channel)
        p._register.assert_called_with(channel, channel.subclient, 'LISTEN')
        self.assertEqual(p._register.call_count, 1)
        self.assertEqual(channel._subscribe.call_count, 1)

        channel._in_listen = True
        channel.subclient.connection._sock = Mock()
        p._register_LISTEN(channel)
        self.assertEqual(p._register.call_count, 1)
        self.assertEqual(channel._subscribe.call_count, 1)
Exemplo n.º 19
0
 def _get_pool(self):
     return Mock()
Exemplo n.º 20
0
    def test_subscribe_no_queues(self):
        self.channel.subclient = Mock()
        self.channel.active_fanout_queues.clear()
        self.channel._subscribe()

        self.assertFalse(self.channel.subclient.subscribe.called)
Exemplo n.º 21
0
 def test_drain_events(self):
     connection = Mock()
     self.transport.drain_events(connection, timeout=10.0)
     connection.drain_events.assert_called_with(timeout=10.0)
Exemplo n.º 22
0
 def test_create_channel(self):
     connection = Mock()
     self.transport.create_channel(connection)
     connection.channel.assert_called_with()
Exemplo n.º 23
0
 def test_message_to_python(self):
     message = Mock()
     message.headers = {}
     message.properties = {}
     self.assertTrue(self.channel.message_to_python(message))
Exemplo n.º 24
0
 def test_close_when_unregister_raises_KeyError(self):
     p = self.Poller()
     p.poller = Mock()
     p._chan_to_sock.update({1: 1})
     p.poller.unregister.side_effect = KeyError(1)
     p.close()
Exemplo n.º 25
0
    def test_receive_different_message_Type(self):
        s = self.channel.subclient = Mock()
        s.parse_response.return_value = ['pmessage', '/foo/', 0, 'data']

        with self.assertRaises(redis.Empty):
            self.channel._receive()
Exemplo n.º 26
0
 def test_heartbeat_check(self):
     t = pyamqp.Transport(Mock())
     conn = Mock()
     t.heartbeat_check(conn, rate=4.331)
     conn.heartbeat_tick.assert_called_with(rate=4.331)
Exemplo n.º 27
0
 def test_event_interface(self):
     t = pyamqp.Transport(Mock())
     t.on_poll_init(Mock())
     t.on_poll_start()
Exemplo n.º 28
0
    def test_receive_empty(self):
        s = self.channel.subclient = Mock()
        s.parse_response.return_value = None

        with self.assertRaises(redis.Empty):
            self.channel._receive()
Exemplo n.º 29
0
 def test_ack_log_error_when_no_error(self):
     ack = self.message.ack = Mock()
     self.message.ack_log_error(Mock(), KeyError)
     ack.assert_called_with()
Exemplo n.º 30
0
    def test_avail_client_when_not_in_poll(self):
        self.channel._in_poll = False
        c = self.channel.client = Mock()

        with self.channel.conn_or_acquire() as client:
            self.assertIs(client, c)
Exemplo n.º 31
0
 def test_message_to_python(self):
     message = Mock()
     message.headers = {}
     message.properties = {}
     self.assertTrue(self.channel.message_to_python(message))
Exemplo n.º 32
0
 def test_get_manager(self):
     with patch('kombu.transport.pyamqp.get_manager') as get_manager:
         t = pyamqp.Transport(Mock())
         t.get_manager(1, kw=2)
         get_manager.assert_called_with(t.client, 1, kw=2)
Exemplo n.º 33
0
    def test_brpop_read_gives_None(self):
        c = self.channel.client = Mock()
        c.parse_response.return_value = None

        with self.assertRaises(redis.Empty):
            self.channel._brpop_read()
Exemplo n.º 34
0
    def test_close_client_close_raises(self):
        c = self.channel.client = Mock()
        c.connection.disconnect.side_effect = self.channel.ResponseError()

        self.channel.close()
        c.connection.disconnect.assert_called_with()
Exemplo n.º 35
0
 def test_eventmap(self):
     t = pyamqp.Transport(Mock())
     conn = Mock()
     self.assertDictEqual(t.eventmap(conn),
                          {conn.sock: t.client.drain_nowait})
Exemplo n.º 36
0
 def after_connected():
     client.connection._sock = Mock()