def test_on_message_ready(self): channel = Mock(name='channel') msg = Mock(name='msg') callback = Mock(name='callback') self.transport._callbacks = {'q1': callback} self.transport.on_message_ready(channel, msg, queue='q1') callback.assert_called_with(msg)
def test_register_with_event_loop(self): conn = Mock(name='conn') loop = Mock(name='loop') self.T.register_with_event_loop(conn, loop) loop.add_reader.assert_called_with( conn.fileno(), self.T.on_readable, conn, loop, )
def test_call_repeatedly(self): t = Timer() try: t.schedule.enter_after = Mock() myfun = Mock() myfun.__name__ = bytes_if_py2("myfun") t.call_repeatedly(0.03, myfun) self.assertEqual(t.schedule.enter_after.call_count, 1) args1, _ = t.schedule.enter_after.call_args_list[0] sec1, tref1, _ = args1 self.assertEqual(sec1, 0.03) tref1() self.assertEqual(t.schedule.enter_after.call_count, 2) args2, _ = t.schedule.enter_after.call_args_list[1] sec2, tref2, _ = args2 self.assertEqual(sec2, 0.03) tref2.canceled = True tref2() self.assertEqual(t.schedule.enter_after.call_count, 2) finally: t.stop()
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_close_connection(self): connection = Mock() connection.client = Mock() self.transport.close_connection(connection) self.assertIsNone(connection.client) connection.close.assert_called_with()
def passthrough(*args, **kwargs): m = Mock(*args, **kwargs) def side_effect(ret): return ret m.side_effect = side_effect return m
def test_getresponse__real_response(self): client = Mock(name='client') client.add_request = passthrough(name='client.add_request') callback = PromiseMock(name='callback') x = AsyncHTTPConnection('aws.vandelay.com', http_client=client) request = x.getresponse(callback) x.http_client.add_request.assert_called_with(request) buf = WhateverIO() buf.write('The quick brown fox jumps') headers = http.Headers({'X-Foo': 'Hello', 'X-Bar': 'World'}) response = http.Response(request, 200, headers, buf) request.on_ready(response) self.assertTrue(callback.called) wresponse = callback.call_args[0][0] self.assertEqual(wresponse.read(), 'The quick brown fox jumps') self.assertEqual(wresponse.status, 200) self.assertEqual(wresponse.getheader('X-Foo'), 'Hello') self.assertDictEqual(dict(wresponse.getheaders()), headers) self.assertTrue(wresponse.msg) self.assertTrue(wresponse.msg) self.assertTrue(repr(wresponse))
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))
def test_repr(self): obj = Mock(name='cb') self.assertIn( 'promise', repr(utils.ChannelPromise(obj)), ) obj.assert_not_called()
def test_on_poll_start(self): p = self.Poller() p._channels = [] p.on_poll_start() p._register_BRPOP = Mock(name='_register_BRPOP') p._register_LISTEN = Mock(name='_register_LISTEN') chan1 = Mock(name='chan1') p._channels = [chan1] chan1.active_queues = [] chan1.active_fanout_queues = [] p.on_poll_start() chan1.active_queues = ['q1'] chan1.active_fanout_queues = ['q2'] chan1.qos.can_consume.return_value = False p.on_poll_start() p._register_LISTEN.assert_called_with(chan1) self.assertFalse(p._register_BRPOP.called) chan1.qos.can_consume.return_value = True p._register_LISTEN.reset_mock() p.on_poll_start() p._register_BRPOP.assert_called_with(chan1) p._register_LISTEN.assert_called_with(chan1)
def test_with_error(self): r = Mock(name='response') r.error = HttpError(404, 'NotFound') x = AsyncHTTPResponse(r) self.assertEqual(x.reason, 'NotFound') r.error = None self.assertFalse(x.reason)
def test_enter_after(self): t = Timer() t._enter = Mock() fun = Mock(name="fun") time = Mock(name="time") time.return_value = 10 t.enter_after(10, fun, time=time) time.assert_called_with() t._enter.assert_called_with(20, 0, fun)
def test_apply_entry_error_not_handled(self, stdout, stderr): t = Timer() t.schedule.on_error = Mock() fun = Mock() fun.side_effect = ValueError() t.schedule.apply_entry(fun) fun.assert_called_with() self.assertFalse(stderr.getvalue())
def test_apply_entry_error_handled(self, logger): t = Timer() t.schedule.on_error = None fun = Mock() fun.side_effect = ValueError() t.schedule.apply_entry(fun) logger.error.assert_called()
def test_connect_to_region(self): with patch('kombu.async.aws.sqs.regions') as regions: region = Mock(name='region') region.name = 'us-west-1' regions.return_value = [region] conn = connect_to_region('us-west-1', kw=3.33) self.assertIs(conn, region.connect.return_value) region.connect.assert_called_with(kw=3.33) self.assertIsNone(connect_to_region('foo'))
def test_acquire_release(self): x = LaxBoundedSemaphore(2) c1 = Mock() x.acquire(c1, 1) self.assertEqual(x.value, 1) c1.assert_called_with(1) c2 = Mock() x.acquire(c2, 2) self.assertEqual(x.value, 0) c2.assert_called_with(2) c3 = Mock() x.acquire(c3, 3) self.assertEqual(x.value, 0) self.assertFalse(c3.called) x.release() self.assertEqual(x.value, 0) x.release() self.assertEqual(x.value, 1) x.release() self.assertEqual(x.value, 2) c3.assert_called_with(3)
def test_mexe__with_sender(self, _): x = AsyncAWSAuthConnection('aws.vandelay.com', http_client=Mock(name='client')) Conn = x.get_http_connection = Mock(name='get_http_connection') request = x.build_base_http_request('GET', 'foo', '/auth') sender = Mock(name='sender') callback = PromiseMock(name='callback') x._mexe(request, sender=sender, callback=callback) sender.assert_called_with( Conn.return_value, request.method, request.path, request.body, request.headers, callback, )
def test_getresponse(self): client = Mock(name='client') client.add_request = passthrough(name='client.add_request') x = AsyncHTTPConnection('aws.vandelay.com', http_client=client) x.Response = Mock(name='x.Response') request = x.getresponse() x.http_client.add_request.assert_called_with(request) self.assertIsInstance(request, Thenable) self.assertIsInstance(request.on_ready, Thenable) response = Mock(name='Response') request.on_ready(response) x.Response.assert_called_with(response)
def MockMessage(self, id=None, receipt_handle=None, body=None): m = Mock(name='message') m.id = id or uuid() m.receipt_handle = receipt_handle or uuid() m._body = body def _get_body(): return m._body m.get_body.side_effect = _get_body def _set_body(value): m._body = value m.set_body.side_effect = _set_body return m
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)
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
def test_simple(self): prev_count, utils.count = utils.count, Mock() try: utils.count.return_value = list(range(1)) x = utils.retry_over_time(self.myfun, self.Predicate, errback=None, interval_max=14) self.assertIsNone(x) utils.count.return_value = list(range(10)) cb = Mock() x = utils.retry_over_time(self.myfun, self.Predicate, errback=self.errback, callback=cb, interval_max=14) self.assertEqual(x, 42) self.assertEqual(self.index, 9) cb.assert_called_with() finally: utils.count = prev_count
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)
def test_handle_event(self): p = self.Poller() chan = Mock(name='chan') p._fd_to_chan[13] = chan, 'BRPOP' chan.handlers = {'BRPOP': Mock(name='BRPOP')} chan.qos.can_consume.return_value = False p.handle_event(13, redis.READ) self.assertFalse(chan.handlers['BRPOP'].called) chan.qos.can_consume.return_value = True p.handle_event(13, redis.READ) chan.handlers['BRPOP'].assert_called_with() p.handle_event(13, redis.ERR) chan._poll_error.assert_called_with('BRPOP') p.handle_event(13, ~(redis.READ | redis.ERR))
def test_mutex(self, lock_id='xxx'): client = Mock(name='client') with patch('kombu.transport.redis.uuid') as uuid: # Won uuid.return_value = lock_id client.setnx.return_value = True client.pipeline = ContextMock() pipe = client.pipeline.return_value pipe.get.return_value = lock_id held = False with redis.Mutex(client, 'foo1', 100): held = True self.assertTrue(held) client.setnx.assert_called_with('foo1', lock_id) pipe.get.return_value = 'yyy' held = False with redis.Mutex(client, 'foo1', 100): held = True self.assertTrue(held) # Did not win client.expire.reset_mock() pipe.get.return_value = lock_id client.setnx.return_value = False with self.assertRaises(redis.MutexHeld): held = False with redis.Mutex(client, 'foo1', '100'): held = True self.assertFalse(held) client.ttl.return_value = 0 with self.assertRaises(redis.MutexHeld): held = False with redis.Mutex(client, 'foo1', '100'): held = True self.assertFalse(held) self.assertTrue(client.expire.called) # Wins but raises WatchError (and that is ignored) client.setnx.return_value = True pipe.watch.side_effect = redis.redis.WatchError() held = False with redis.Mutex(client, 'foo1', 100): held = True self.assertTrue(held)
def test_fire_timers_raises(self): eback = Mock() eback.side_effect = KeyError('foo') self.hub.timer = Mock() self.hub.scheduler = iter([(0, eback)]) with self.assertRaises(KeyError): self.hub.fire_timers(propagate=(KeyError,)) eback.side_effect = ValueError('foo') self.hub.scheduler = iter([(0, eback)]) with patch('kombu.async.hub.logger') as logger: with self.assertRaises(StopIteration): self.hub.fire_timers() self.assertTrue(logger.error.called) eback.side_effect = MemoryError('foo') self.hub.scheduler = iter([(0, eback)]) with self.assertRaises(MemoryError): self.hub.fire_timers() eback.side_effect = OSError() eback.side_effect.errno = errno.ENOMEM self.hub.scheduler = iter([(0, eback)]) with self.assertRaises(OSError): self.hub.fire_timers() eback.side_effect = OSError() eback.side_effect.errno = errno.ENOENT self.hub.scheduler = iter([(0, eback)]) with patch('kombu.async.hub.logger') as logger: with self.assertRaises(StopIteration): self.hub.fire_timers() self.assertTrue(logger.error.called)
def test_collect__with_conn(self): self.T.client.drain_events = 1234 conn = Mock(name='connection') chans = conn.channels = {1: Mock(name='chan1'), 2: Mock(name='chan2')} conn.callbacks = {'foo': Mock(name='cb1'), 'bar': Mock(name='cb2')} for i, chan in enumerate(conn.channels.values()): chan.connection = i with patch('os.close') as close: self.T._collect(conn) close.assert_called_with(conn.fileno()) self.assertFalse(conn.channels) self.assertFalse(conn.callbacks) for chan in chans.values(): self.assertIsNone(chan.connection) self.assertIsNone(self.client.drain_events) self.assertIsNone(self.T.client) with patch('os.close') as close: self.T.client = self.client close.side_effect = OSError() self.T._collect(conn) close.assert_called_with(conn.fileno())
def test_grow_shrink(self): x = LaxBoundedSemaphore(1) self.assertEqual(x.initial_value, 1) cb1 = Mock() x.acquire(cb1, 1) cb1.assert_called_with(1) self.assertEqual(x.value, 0) cb2 = Mock() x.acquire(cb2, 2) self.assertFalse(cb2.called) self.assertEqual(x.value, 0) cb3 = Mock() x.acquire(cb3, 3) self.assertFalse(cb3.called) x.grow(2) cb2.assert_called_with(2) cb3.assert_called_with(3) self.assertEqual(x.value, 2) self.assertEqual(x.initial_value, 3) self.assertFalse(x._waiting) x.grow(3) for i in range(x.initial_value): self.assertTrue(x.acquire(Mock())) self.assertFalse(x.acquire(Mock())) x.clear() x.shrink(3) for i in range(x.initial_value): self.assertTrue(x.acquire(Mock())) self.assertFalse(x.acquire(Mock())) self.assertEqual(x.value, 0) for i in range(100): x.release() self.assertEqual(x.value, x.initial_value)
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)
def test_qos_reject(self): p, channel = self.create_get() qos = redis.QoS(channel) qos.ack = Mock(name='Qos.ack') qos.reject(1234) qos.ack.assert_called_with(1234)
def _get_pool(self): return Mock()
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()
def after_connected(): client.connection._sock = Mock()
def test_stop(self): self.hub.call_soon = Mock(name='call_soon') self.hub.stop() self.hub.call_soon.assert_called_with(_raise_stop_error)
def test_run_forever(self): self.hub.run_once = Mock(name='hub.run_once') self.hub.run_once.side_effect = Stop() self.hub.run_forever()
def test_verify_connection(self): conn = Mock(name='connection') conn.connected = True self.assertTrue(self.T.verify_connection(conn))
def test_call_at(self): callback = Mock(name='callback') self.hub.timer = Mock(name='hub.timer') self.hub.call_at(21231122, callback, 1, 2) self.hub.timer.call_at.assert_called_with(21231122, callback, (1, 2))
def test_grow_shrink(self): x = LaxBoundedSemaphore(1) self.assertEqual(x.initial_value, 1) cb1 = Mock() x.acquire(cb1, 1) cb1.assert_called_with(1) self.assertEqual(x.value, 0) cb2 = Mock() x.acquire(cb2, 2) cb2.assert_not_called() self.assertEqual(x.value, 0) cb3 = Mock() x.acquire(cb3, 3) cb3.assert_not_called() x.grow(2) cb2.assert_called_with(2) cb3.assert_called_with(3) self.assertEqual(x.value, 2) self.assertEqual(x.initial_value, 3) self.assertFalse(x._waiting) x.grow(3) for i in range(x.initial_value): self.assertTrue(x.acquire(Mock())) self.assertFalse(x.acquire(Mock())) x.clear() x.shrink(3) for i in range(x.initial_value): self.assertTrue(x.acquire(Mock())) self.assertFalse(x.acquire(Mock())) self.assertEqual(x.value, 0) for i in range(100): x.release() self.assertEqual(x.value, x.initial_value)
def test_receive_empty(self): s = self.channel.subclient = Mock() s.parse_response.return_value = None with self.assertRaises(redis.Empty): self.channel._receive()
def test_remove__unregister_raises(self): self.hub.poller = Mock() self.hub.poller.unregister.side_effect = OSError() self.hub.remove(313)
def test_can_remove_unknown_fds(self): self.hub.poller = Mock() self.hub.remove(30) self.hub.remove(File(301))
def _create_client(self): self._client = Mock(name='client')
def test_on_callback_error(self, logger): self.hub.on_callback_error(Mock(name='callback'), KeyError()) logger.error.assert_called()
def test_add__consolidate(self): self.hub.poller = Mock(name='hub.poller') self.hub.add(2, Mock(), WRITE, consolidate=True) self.assertIn(2, self.hub.consolidate) self.assertIsNone(self.hub.writers[2])
def test_call_soon(self, promise): callback = Mock(name='callback') ret = self.hub.call_soon(callback, 1, 2, 3) promise.assert_called_with(callback, (1, 2, 3)) self.assertIn(promise(), self.hub._ready) self.assertIs(ret, promise())
def test_call_later(self): callback = Mock(name='callback') self.hub.timer = Mock(name='hub.timer') self.hub.call_later(10.0, callback, 1, 2) self.hub.timer.call_after.assert_called_with(10.0, callback, (1, 2))
def test_reset(self): self.hub.close = Mock(name='close') self.hub._create_poller = Mock(name='_create_poller') self.hub.reset() self.hub.close.assert_called_with() self.hub._create_poller.assert_called_with()
def test_loop_property(self): self.hub._loop = None self.hub.create_loop = Mock(name='hub.create_loop') self.assertIs(self.hub.loop, self.hub.create_loop()) self.assertIs(self.hub._loop, self.hub.create_loop())
def test_remove_reader__not_writeable(self): self.hub.poller = Mock(name='hub.poller') self.hub.add(2, Mock(), READ) self.hub.remove_reader(2) self.assertNotIn(2, self.hub.readers)
def test_remove_writer__not_readable(self): self.hub.poller = Mock(name='hub.poller') self.hub.add(2, Mock(), WRITE) self.hub.remove_writer(2) self.assertNotIn(2, self.hub.writers)
def test__close_poller(self): poller = self.hub.poller = Mock(name='poller') self.hub._close_poller() poller.close.assert_called_with() self.assertIsNone(self.hub.poller)
def test_deliver_when_fanout_unsupported(self): self.e.channel = Mock() self.e.channel.supports_fanout = False self.e.deliver(Mock(), 'exchange', None) self.assertFalse(self.e.channel._put_fanout.called)
def test_message_to_python(self): message = Mock() message.headers = {} message.properties = {} self.assertTrue(self.channel.message_to_python(message))
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)
def test_receive_different_message_Type(self): s = self.channel.subclient = Mock() s.parse_response.return_value = ['message', '/foo/', 0, 'data'] with self.assertRaises(redis.Empty): self.channel._receive()
def test_call_soon__promise_argument(self): callback = promise(Mock(name='callback'), (1, 2, 3)) ret = self.hub.call_soon(callback) self.assertIs(ret, callback) self.assertIn(ret, self.hub._ready)
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()
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()
def test_acquire_release(self): x = LaxBoundedSemaphore(2) c1 = Mock() x.acquire(c1, 1) self.assertEqual(x.value, 1) c1.assert_called_with(1) c2 = Mock() x.acquire(c2, 2) self.assertEqual(x.value, 0) c2.assert_called_with(2) c3 = Mock() x.acquire(c3, 3) self.assertEqual(x.value, 0) c3.assert_not_called() x.release() self.assertEqual(x.value, 0) x.release() self.assertEqual(x.value, 1) x.release() self.assertEqual(x.value, 2) c3.assert_called_with(3)
def test_connection(self): x = connect_sqs('AAKI', 'ASAK', http_client=Mock()) self.assertTrue(x) self.assertTrue(x.connection)