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() logger.error.assert_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() logger.error.assert_called()
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 mock_sax_parse(self, parser): with patch('kombu.async.aws.connection.sax_parse') as sax_parse: with patch('kombu.async.aws.connection.XmlHandler') as xh: def effect(body, h): return parser(xh.call_args[0][0], body, h) sax_parse.side_effect = effect yield (sax_parse, xh) self.assertTrue(sax_parse.called)
def mock_sax_parse(self, parser): with patch('kombu.async.aws.connection.sax_parse') as sax_parse: with patch('kombu.async.aws.connection.XmlHandler') as xh: def effect(body, h): return parser(xh.call_args[0][0], body, h) sax_parse.side_effect = effect yield (sax_parse, xh) sax_parse.assert_called()
def test_detect_environment_eventlet(self): with patch('eventlet.patcher.is_monkey_patched', create=True) as m: self.assertTrue(sys.modules['eventlet']) m.return_value = True env = syn._detect_environment() m.assert_called_with(socket) self.assertEqual(env, 'eventlet')
def test_with_pyrabbit(self): with patch('pyrabbit.Client', create=True) as Client: manager = Connection('amqp://').get_manager() self.assertIsNotNone(manager) Client.assert_called_with( 'localhost:15672', 'guest', 'guest', )
def test_wraps(self): with patch('kombu.utils.debug.get_logger') as get_logger: logger = get_logger.return_value = Mock() W = Logwrapped(Mock(), 'kombu.test') get_logger.assert_called_with('kombu.test') self.assertIsNotNone(W.instance) self.assertIs(W.logger, logger) W.instance.__repr__ = lambda s: 'foo' self.assertEqual(repr(W), 'foo') W.instance.some_attr = 303 self.assertEqual(W.some_attr, 303) W.instance.some_method.__name__ = 'some_method' W.some_method(1, 2, kw=1) W.instance.some_method.assert_called_with(1, 2, kw=1) W.some_method() W.instance.some_method.assert_called_with() W.some_method(kw=1) W.instance.some_method.assert_called_with(kw=1) W.ident = 'ident' W.some_method(kw=1) self.assertTrue(logger.debug.called) self.assertIn('ident', logger.debug.call_args[0][0]) self.assertEqual(dir(W), dir(W.instance))
def test_when_containing_high_chars(self): with patch("sys.getfilesystemencoding") as encoding: encoding.return_value = "ascii" s = "The quiæk fåx jømps øver the lazy dåg" res = safe_str(s) self.assertIsInstance(res, str) self.assertEqual(len(s), len(res))
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_restore(self): message = Mock(name='message') with patch('kombu.transport.redis.loads') as loads: loads.return_value = 'M', 'EX', 'RK' client = self.channel.client = Mock(name='client') client.pipeline = ContextMock() restore = self.channel._do_restore_message = Mock( name='_do_restore_message', ) pipe = client.pipeline.return_value pipe_hget = Mock(name='pipe.hget') pipe.hget.return_value = pipe_hget pipe_hget_hdel = Mock(name='pipe.hget.hdel') pipe_hget.hdel.return_value = pipe_hget_hdel result = Mock(name='result') pipe_hget_hdel.execute.return_value = None, None self.channel._restore(message) client.pipeline.assert_called_with() unacked_key = self.channel.unacked_key self.assertFalse(loads.called) tag = message.delivery_tag pipe.hget.assert_called_with(unacked_key, tag) pipe_hget.hdel.assert_called_with(unacked_key, tag) pipe_hget_hdel.execute.assert_called_with() pipe_hget_hdel.execute.return_value = result, None self.channel._restore(message) loads.assert_called_with(result) restore.assert_called_with('M', 'EX', 'RK', client, False)
def test_when_encoding_utf8(self): with patch('sys.getdefaultencoding') as encoding: encoding.return_value = 'utf-8' self.assertEqual(default_encoding(), 'utf-8') s = 'The quiæk fåx jømps øver the lazy dåg' res = safe_str(s) self.assertIsInstance(res, str)
def test_when_containing_high_chars(self): with patch('sys.getdefaultencoding') as encoding: encoding.return_value = 'ascii' s = 'The quiæk fåx jømps øver the lazy dåg' res = safe_str(s) self.assertIsInstance(res, str) self.assertEqual(len(s), len(res))
def test_restore(self): message = Mock(name='message') with patch('kombu.transport.redis.loads') as loads: loads.return_value = 'M', 'EX', 'RK' client = self.channel.client = Mock(name='client') restore = self.channel._do_restore_message = Mock( name='_do_restore_message', ) pipe = Mock(name='pipe') client.pipeline.return_value = pipe pipe_hget = Mock(name='pipe.hget') pipe.hget.return_value = pipe_hget pipe_hget_hdel = Mock(name='pipe.hget.hdel') pipe_hget.hdel.return_value = pipe_hget_hdel result = Mock(name='result') pipe_hget_hdel.execute.return_value = None, None self.channel._restore(message) client.pipeline.assert_called_with() unacked_key = self.channel.unacked_key self.assertFalse(loads.called) tag = message.delivery_tag pipe.hget.assert_called_with(unacked_key, tag) pipe_hget.hdel.assert_called_with(unacked_key, tag) pipe_hget_hdel.execute.assert_called_with() pipe_hget_hdel.execute.return_value = result, None self.channel._restore(message) loads.assert_called_with(result) restore.assert_called_with('M', 'EX', 'RK', client, False)
def test_socket_connection(self): with patch('kombu.transport.redis.Channel._create_client'): with Connection('redis+socket:///tmp/redis.sock') as conn: connparams = conn.default_channel._connparams() self.assertEqual(connparams['connection_class'], redis.redis.UnixDomainSocketConnection) self.assertEqual(connparams['path'], '/tmp/redis.sock')
def test_restore(self): message = Mock(name="message") with patch("kombu.transport.redis.loads") as loads: loads.return_value = "M", "EX", "RK" client = self.channel.client = Mock(name="client") restore = self.channel._do_restore_message = Mock(name="_do_restore_message") pipe = Mock(name="pipe") client.pipeline.return_value = pipe pipe_hget = Mock(name="pipe.hget") pipe.hget.return_value = pipe_hget pipe_hget_hdel = Mock(name="pipe.hget.hdel") pipe_hget.hdel.return_value = pipe_hget_hdel result = Mock(name="result") pipe_hget_hdel.execute.return_value = None, None self.channel._restore(message) client.pipeline.assert_called_with() unacked_key = self.channel.unacked_key self.assertFalse(loads.called) tag = message.delivery_tag pipe.hget.assert_called_with(unacked_key, tag) pipe_hget.hdel.assert_called_with(unacked_key, tag) pipe_hget_hdel.execute.assert_called_with() pipe_hget_hdel.execute.return_value = result, None self.channel._restore(message) loads.assert_called_with(result) restore.assert_called_with("M", "EX", "RK", client, False)
def test_wraps(self): with patch('kombu.utils.debug.get_logger') as get_logger: logger = get_logger.return_value = Mock() W = Logwrapped(Mock(), 'kombu.test') get_logger.assert_called_with('kombu.test') self.assertIsNotNone(W.instance) self.assertIs(W.logger, logger) W.instance.__repr__ = lambda s: 'foo' self.assertEqual(repr(W), 'foo') W.instance.some_attr = 303 self.assertEqual(W.some_attr, 303) W.instance.some_method.__name__ = bytes_if_py2('some_method') W.some_method(1, 2, kw=1) W.instance.some_method.assert_called_with(1, 2, kw=1) W.some_method() W.instance.some_method.assert_called_with() W.some_method(kw=1) W.instance.some_method.assert_called_with(kw=1) W.ident = 'ident' W.some_method(kw=1) self.assertTrue(logger.debug.called) self.assertIn('ident', logger.debug.call_args[0][0]) self.assertEqual(dir(W), dir(W.instance))
def test_when_encoding_utf8(self): with patch("sys.getfilesystemencoding") as encoding: encoding.return_value = "utf-8" self.assertEqual(default_encoding(), "utf-8") s = "The quiæk fåx jømps øver the lazy dåg" res = safe_str(s) self.assertIsInstance(res, str)
def test_rediss_connection(self): with patch('kombu.transport.redis.Channel._create_client'): with Connection('rediss://') as conn: connparams = conn.default_channel._connparams() assert issubclass( connparams['connection_class'], redis.redis.SSLConnection, )
def test_with_pkg_resources(self): with patch('pkg_resources.iter_entry_points', create=True) as iterep: eps = iterep.return_value = [Mock(), Mock()] self.assertTrue(list(utils.entrypoints('kombu.test'))) iterep.assert_called_with('kombu.test') eps[0].load.assert_called_with() eps[1].load.assert_called_with()
def test_close(self): with patch('kombu.async.http.curl.pycurl'): x = self.Client() x._timeout_check_tref = Mock(name='timeout_check_tref') x.close() x._timeout_check_tref.cancel.assert_called_with() for _curl in x._curls: _curl.close.assert_called_with() x._multi.close.assert_called_with()
def test_adds_handlers_sets_level(self): with patch('kombu.utils.debug.get_logger') as get_logger: logger = get_logger.return_value = Mock() setup_logging(loggers=['kombu.test']) get_logger.assert_called_with('kombu.test') self.assertTrue(logger.addHandler.called) logger.setLevel.assert_called_with(logging.DEBUG)
def test_on_readable_on_writeable(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() x._on_event = Mock(name='on_event') fd = Mock(name='fd') x.on_readable(fd, _pycurl=_pycurl) x._on_event.assert_called_with(fd, _pycurl.CSELECT_IN) x.on_writable(fd, _pycurl=_pycurl) x._on_event.assert_called_with(fd, _pycurl.CSELECT_OUT)
def test_detect_environment_gevent(self): with patch('gevent.socket', create=True) as m: prev, socket.socket = socket.socket, m.socket try: self.assertTrue(sys.modules['gevent']) env = syn._detect_environment() self.assertEqual(env, 'gevent') finally: socket.socket = prev
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_add_request(self): with patch('kombu.async.http.curl.pycurl'): x = self.Client() x._process_queue = Mock(name='_process_queue') x._set_timeout = Mock(name='_set_timeout') request = Mock(name='request') x.add_request(request) self.assertIn(request, x._pending) x._process_queue.assert_called_with() x._set_timeout.assert_called_with(0)
def test_timeout_check(self): with patch('kombu.async.http.curl.pycurl') as _pycurl: x = self.Client() x._process_pending_requests = Mock(name='process_pending') x._multi.socket_all.return_value = 333, 1 _pycurl.error = KeyError x._timeout_check(_pycurl=_pycurl) x._multi.socket_all.return_value = None x._multi.socket_all.side_effect = _pycurl.error(333) x._timeout_check(_pycurl=_pycurl)
def test_url_parser(self): with patch('kombu.transport.sqlalchemy.Channel._open'): url = 'sqlalchemy+sqlite:///celerydb.sqlite' Connection(url).connect() url = 'sqla+sqlite:///celerydb.sqlite' Connection(url).connect() # Should prevent regression fixed by f187ccd url = 'sqlb+sqlite:///celerydb.sqlite' with self.assertRaises(KeyError): Connection(url).connect()
def test_transport_options(self): with patch('pyrabbit.Client', create=True) as Client: manager = Connection('amqp://', transport_options={ 'manager_hostname': 'admin.mq.vandelay.com', 'manager_port': 808, 'manager_userid': 'george', 'manager_password': '******', }).get_manager() self.assertIsNotNone(manager) Client.assert_called_with( 'admin.mq.vandelay.com:808', 'george', 'bosco', )
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_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_handle_error(self): from datetime import datetime scratch = [None] def on_error(exc_info): scratch[0] = exc_info s = Timer(on_error=on_error) with patch('kombu.async.timer.to_timestamp') as tot: tot.side_effect = OverflowError() s.enter_at(Entry(lambda: None, (), {}), eta=datetime.now()) s.enter_at(Entry(lambda: None, (), {}), eta=None) s.on_error = None with self.assertRaises(OverflowError): s.enter_at(Entry(lambda: None, (), {}), eta=datetime.now()) exc = scratch[0] self.assertIsInstance(exc, OverflowError)
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 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_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 pipe = client.pipeline.return_value = Mock(name="pipe") 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_handle_error(self): from datetime import datetime scratch = [None] def on_error(exc_info): scratch[0] = exc_info s = Timer(on_error=on_error) with patch("kombu.async.timer.to_timestamp") as tot: tot.side_effect = OverflowError() s.enter_at(Entry(lambda: None, (), {}), eta=datetime.now()) s.enter_at(Entry(lambda: None, (), {}), eta=None) s.on_error = None with self.assertRaises(OverflowError): s.enter_at(Entry(lambda: None, (), {}), eta=datetime.now()) exc = scratch[0] self.assertIsInstance(exc, OverflowError)
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 setup(self): self._cencoding = patch('sys.getfilesystemencoding') self._encoding = self._cencoding.__enter__() self._encoding.return_value = 'ascii'
def test_ssl_argument__dict(self): with patch('kombu.transport.redis.Channel._create_client'): with Connection('redis://', ssl={'ca_cert': '/foo'}) as conn: connparams = conn.default_channel._connparams() self.assertTrue(connparams['ssl']) self.assertEqual(connparams['ca_cert'], '/foo')
def setUp(self): self._cencoding = patch('sys.getdefaultencoding') self._encoding = self._cencoding.__enter__() self._encoding.return_value = 'ascii'
def test_ssl_argument__bool(self): with patch('kombu.transport.redis.Channel._create_client'): with Connection('redis://', ssl=True) as conn: connparams = conn.default_channel._connparams() self.assertTrue(connparams['ssl'])