def test_do_restore_message(self): client = Mock(name='client') pl1 = {'body': 'BODY'} spl1 = dumps(pl1) lookup = self.channel._lookup = Mock(name='_lookup') lookup.return_value = ['george', 'elaine'] self.channel._do_restore_message( pl1, 'ex', 'rkey', client, ) client.rpush.assert_has_calls([ call('george', spl1), call('elaine', spl1), ]) pl2 = {'body': 'BODY2', 'headers': {'x-funny': 1}} headers_after = dict(pl2['headers'], redelivered=True) spl2 = dumps(dict(pl2, headers=headers_after)) self.channel._do_restore_message( pl2, 'ex', 'rkey', client, ) client.rpush.assert_has_calls([ call('george', spl2), call('elaine', spl2), ]) client.rpush.side_effect = KeyError() with patch('kombu.transport.redis.crit') as crit: self.channel._do_restore_message( pl2, 'ex', 'rkey', client, ) self.assertTrue(crit.called)
def test_enter__exit(self): P = self.hub.poller = Mock() on_close = Mock() self.hub.on_close.add(on_close) try: read_A = Mock() read_B = Mock() self.hub.add_reader(10, read_A) self.hub.add_reader(File(11), read_B) write_A = Mock() write_B = Mock() self.hub.add_writer(20, write_A) self.hub.add_writer(File(21), write_B) self.assertTrue(self.hub.readers) self.assertTrue(self.hub.writers) finally: assert self.hub.poller self.hub.close() self.assertFalse(self.hub.readers) self.assertFalse(self.hub.writers) P.unregister.assert_has_calls([ call(10), call(11), call(20), call(21), ], any_order=True) on_close.assert_called_with(self.hub)
def test_qos_restore_visible(self): client = self.channel.client = Mock(name='client') def pipe(*args, **kwargs): return Pipeline(client) client.pipeline = pipe client.zrevrangebyscore.return_value = [ (1, 10), (2, 20), (3, 30), ] qos = redis.QoS(self.channel) restore = qos.restore_by_tag = Mock(name='restore_by_tag') qos._vrestore_count = 1 qos.restore_visible() self.assertFalse(client.zrevrangebyscore.called) self.assertEqual(qos._vrestore_count, 2) qos._vrestore_count = 0 qos.restore_visible() restore.assert_has_calls([ call(1, client), call(2, client), call(3, client), ]) self.assertEqual(qos._vrestore_count, 1) qos._vrestore_count = 0 restore.reset_mock() client.zrevrangebyscore.return_value = [] qos.restore_visible() self.assertFalse(restore.called) self.assertEqual(qos._vrestore_count, 1) qos._vrestore_count = 0 client.setnx.side_effect = redis.MutexHeld() qos.restore_visible()
def test_register_with_event_loop(self): transport = self.connection.transport transport.cycle = Mock(name="cycle") transport.cycle.fds = {12: "LISTEN", 13: "BRPOP"} conn = Mock(name="conn") loop = Mock(name="loop") redis.Transport.register_with_event_loop(transport, conn, loop) transport.cycle.on_poll_init.assert_called_with(loop.poller) loop.call_repeatedly.assert_called_with(10, transport.cycle.maybe_restore_messages) self.assertTrue(loop.on_tick.add.called) on_poll_start = loop.on_tick.add.call_args[0][0] on_poll_start() transport.cycle.on_poll_start.assert_called_with() loop.add_reader.assert_has_calls([call(12, transport.on_readable, 12), call(13, transport.on_readable, 13)])
def test_close_deletes_autodelete_fanout_queues(self): self.channel._fanout_queues = {'foo': ('foo', ''), 'bar': ('bar', '')} self.channel.auto_delete_queues = ['foo'] self.channel.queue_delete = Mock(name='queue_delete') self.channel.close() self.channel.queue_delete.assert_has_calls([call('foo')])
def test_close_deletes_autodelete_fanout_queues(self): self.channel._fanout_queues = {"foo": ("foo", ""), "bar": ("bar", "")} self.channel.auto_delete_queues = ["foo"] self.channel.queue_delete = Mock(name="queue_delete") self.channel.close() self.channel.queue_delete.assert_has_calls([call("foo")])
def test_init(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() self.assertIsNotNone(x._multi) self.assertIsNotNone(x._pending) self.assertIsNotNone(x._free_list) self.assertIsNotNone(x._fds) self.assertEqual( x._socket_action, x._multi.socket_action, ) self.assertEqual(len(x._curls), x.max_clients) self.assertTrue(x._timeout_check_tref) x._multi.setopt.assert_has_calls([ call(_pycurl.M_TIMERFUNCTION, x._set_timeout), call(_pycurl.M_SOCKETFUNCTION, x._handle_socket), ])
def test_do_restore_message(self): client = Mock(name="client") pl1 = {"body": "BODY"} spl1 = dumps(pl1) lookup = self.channel._lookup = Mock(name="_lookup") lookup.return_value = ["george", "elaine"] self.channel._do_restore_message(pl1, "ex", "rkey", client) client.rpush.assert_has_calls([call("george", spl1), call("elaine", spl1)]) pl2 = {"body": "BODY2", "headers": {"x-funny": 1}} headers_after = dict(pl2["headers"], redelivered=True) spl2 = dumps(dict(pl2, headers=headers_after)) self.channel._do_restore_message(pl2, "ex", "rkey", client) client.rpush.assert_has_calls([call("george", spl2), call("elaine", spl2)]) client.rpush.side_effect = KeyError() with patch("kombu.transport.redis.crit") as crit: self.channel._do_restore_message(pl2, "ex", "rkey", client) self.assertTrue(crit.called)
def test_register_with_event_loop(self): transport = self.connection.transport transport.cycle = Mock(name='cycle') transport.cycle.fds = {12: 'LISTEN', 13: 'BRPOP'} conn = Mock(name='conn') loop = Mock(name='loop') redis.Transport.register_with_event_loop(transport, conn, loop) transport.cycle.on_poll_init.assert_called_with(loop.poller) loop.call_repeatedly.assert_called_with( 10, transport.cycle.maybe_restore_messages, ) loop.on_tick.add.assert_called() on_poll_start = loop.on_tick.add.call_args[0][0] on_poll_start() transport.cycle.on_poll_start.assert_called_with() loop.add_reader.assert_has_calls([ call(12, transport.on_readable, 12), call(13, transport.on_readable, 13), ])
def test_delete(self): x = self.channel self.channel._in_poll = False delete = x.client.delete = Mock() srem = x.client.srem = Mock() x._delete('queue', 'exchange', 'routing_key', None) delete.assert_has_calls([ call(x._q_for_pri('queue', pri)) for pri in redis.PRIORITY_STEPS ]) srem.assert_called_with(x.keyprefix_queue % ('exchange',), x.sep.join(['routing_key', '', 'queue']))
def test_register_with_event_loop(self): transport = self.connection.transport transport.cycle = Mock(name='cycle') transport.cycle.fds = {12: 'LISTEN', 13: 'BRPOP'} conn = Mock(name='conn') loop = Mock(name='loop') redis.Transport.register_with_event_loop(transport, conn, loop) transport.cycle.on_poll_init.assert_called_with(loop.poller) loop.call_repeatedly.assert_called_with( 10, transport.cycle.maybe_restore_messages, ) self.assertTrue(loop.on_tick.add.called) on_poll_start = loop.on_tick.add.call_args[0][0] on_poll_start() transport.cycle.on_poll_start.assert_called_with() loop.add_reader.assert_has_calls([ call(12, transport.on_readable, 12), call(13, transport.on_readable, 13), ])
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_calls([ call(self.channel._q_for_pri('foo', pri)) for pri in redis.PRIORITY_STEPS ]) exists.return_value = False self.assertFalse(self.channel._has_queue('foo'))
def test_do_restore_message(self): client = Mock(name='client') pl1 = {'body': 'BODY'} spl1 = dumps(pl1) lookup = self.channel._lookup = Mock(name='_lookup') lookup.return_value = ['george', 'elaine'] self.channel._do_restore_message( pl1, 'ex', 'rkey', client, ) client.rpush.assert_has_calls([ call('george', spl1), call('elaine', spl1), ]) pl2 = {'body': 'BODY2', 'headers': {'x-funny': 1}} headers_after = dict(pl2['headers'], redelivered=True) spl2 = dumps(dict(pl2, headers=headers_after)) self.channel._do_restore_message( pl2, 'ex', 'rkey', client, ) client.rpush.assert_has_calls([ call('george', spl2), call('elaine', spl2), ]) client.rpush.side_effect = KeyError() with patch('kombu.transport.redis.logger') as logger: self.channel._do_restore_message( pl2, 'ex', 'rkey', client, ) self.assertTrue(logger.critical.called)
def test_qos_restore_visible(self): client = self.channel._create_client = Mock(name='client') client = client() def pipe(*args, **kwargs): return Pipeline(client) client.pipeline = pipe client.zrevrangebyscore.return_value = [ (1, 10), (2, 20), (3, 30), ] qos = redis.QoS(self.channel) restore = qos.restore_by_tag = Mock(name='restore_by_tag') qos._vrestore_count = 1 qos.restore_visible() self.assertFalse(client.zrevrangebyscore.called) self.assertEqual(qos._vrestore_count, 2) qos._vrestore_count = 0 qos.restore_visible() restore.assert_has_calls([ call(1, client), call(2, client), call(3, client), ]) self.assertEqual(qos._vrestore_count, 1) qos._vrestore_count = 0 restore.reset_mock() client.zrevrangebyscore.return_value = [] qos.restore_visible() self.assertFalse(restore.called) self.assertEqual(qos._vrestore_count, 1) qos._vrestore_count = 0 client.setnx.side_effect = redis.MutexHeld() qos.restore_visible()
def test_add_writers(self): P = self.hub.poller = Mock() write_A = Mock() write_B = Mock() self.hub.add_writer(20, write_A) self.hub.add_writer(File(21), write_B) P.register.assert_has_calls([ call(20, self.hub.WRITE), call(21, self.hub.WRITE), ], any_order=True) self.assertEqual(self.hub.writers[20], (write_A, ())) self.assertEqual(self.hub.writers[21], (write_B, ())) self.hub.remove(20) self.assertNotIn(20, self.hub.writers) self.hub.remove(File(21)) self.assertNotIn(21, self.hub.writers) P.unregister.assert_has_calls([ call(20), call(21), ])
def test_add_remove_readers(self): P = self.hub.poller = Mock() read_A = Mock() read_B = Mock() self.hub.add_reader(10, read_A, 10) self.hub.add_reader(File(11), read_B, 11) P.register.assert_has_calls([ call(10, self.hub.READ | self.hub.ERR), call(11, self.hub.READ | self.hub.ERR), ], any_order=True) self.assertEqual(self.hub.readers[10], (read_A, (10,))) self.assertEqual(self.hub.readers[11], (read_B, (11,))) self.hub.remove(10) self.assertNotIn(10, self.hub.readers) self.hub.remove(File(11)) self.assertNotIn(11, self.hub.readers) P.unregister.assert_has_calls([ call(10), call(11), ])
def test_handle_socket(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: hub = Mock(name='hub') x = self.Client(hub) fd = Mock(name='fd1') # POLL_REMOVE x._fds[fd] = fd x._handle_socket(_pycurl.POLL_REMOVE, fd, x._multi, None, _pycurl) hub.remove.assert_called_with(fd) self.assertNotIn(fd, x._fds) x._handle_socket(_pycurl.POLL_REMOVE, fd, x._multi, None, _pycurl) # POLL_IN hub = x.hub = Mock(name='hub') fds = [fd, Mock(name='fd2'), Mock(name='fd3')] x._fds = {f: f for f in fds} x._handle_socket(_pycurl.POLL_IN, fd, x._multi, None, _pycurl) hub.remove.assert_has_calls([call(fd)]) hub.add_reader.assert_called_with(fd, x.on_readable, fd) self.assertEqual(x._fds[fd], READ) # POLL_OUT hub = x.hub = Mock(name='hub') x._handle_socket(_pycurl.POLL_OUT, fd, x._multi, None, _pycurl) hub.add_writer.assert_called_with(fd, x.on_writable, fd) self.assertEqual(x._fds[fd], WRITE) # POLL_INOUT hub = x.hub = Mock(name='hub') x._handle_socket(_pycurl.POLL_INOUT, fd, x._multi, None, _pycurl) hub.add_reader.assert_called_with(fd, x.on_readable, fd) hub.add_writer.assert_called_with(fd, x.on_writable, fd) self.assertEqual(x._fds[fd], READ | WRITE) # UNKNOWN EVENT hub = x.hub = Mock(name='hub') x._handle_socket(0xff3f, fd, x._multi, None, _pycurl) # FD NOT IN FDS hub = x.hub = Mock(name='hub') x._fds.clear() x._handle_socket(0xff3f, fd, x._multi, None, _pycurl) self.assertFalse(hub.remove.called)
def assert_collection_accessed(self, *collections): self.channel.client.__getitem__.assert_has_calls( [call(c) for c in collections], any_order=True)
def assert_operation_called_with(self, cname, mname, *args, **kwargs): self.assert_operation_has_calls(cname, mname, [call(*args, **kwargs)])