async def setUp(self): self.queue = JsonQueue("127.0.0.1", "guest", "guest", delegate=DumbConsumer()) self.consume_conn = self.queue.conn_for(ConnType.CONSUME) self.write_conn = self.queue.conn_for(ConnType.WRITE)
class JsonQueueTest(TestCase): async def setUp(self): self.queue = JsonQueue("127.0.0.1", "guest", "guest", delegate=DumbConsumer()) self.consume_conn = self.queue.conn_for(ConnType.CONSUME) self.write_conn = self.queue.conn_for(ConnType.WRITE) async def test_put_doesn_connect_consume_connection(self): """ Certifica que uma Queue que faz apenas escrita não abre a conexão de consumers. Abre apenas a conexão de escrita. """ self.assertFalse(self.consume_conn.is_connected) self.assertIsNone(self.consume_conn.channel) await self.queue.put(routing_key="some-queue", data={"OK": True}) self.assertTrue(self.write_conn.is_connected, "Não abriu a write conn") self.assertTrue(self.write_conn.channel.is_open) self.assertFalse( self.consume_conn.is_connected, "Conectou a consume conn, não deveria", ) self.assertIsNone(self.consume_conn.channel) async def test_consume_doesnt_open_write_connection(self): """ Certifica que se uma queue faz apenas consume ela não abre a conexão que é dedicada a escrita. """ self.assertFalse(self.consume_conn.is_connected) self.assertIsNone(self.consume_conn.channel) with self.assertRaises(ChannelClosed): await self.queue.consume("some-queue", DumbConsumer()) self.assertTrue(self.consume_conn.is_connected, "Não connectou a consume connection") self.assertIsNotNone(self.consume_conn.channel) self.assertFalse( self.write_conn.is_connected, "Conectou a write connection, não deveria", ) self.assertIsNone(self.write_conn.channel) async def test_get_conn_for_consume(self): self.assertEqual(self.queue.connection, self.queue.conn_for(ConnType.CONSUME)) async def test_get_conn_for_write(self): self.assertEqual(self.queue._write_connection, self.queue.conn_for(ConnType.WRITE))
def setUp(self): self.conn_params = dict( host="money.que.é.good", username="******", password="******", virtual_host="have", heartbeat=5, ) self.queue = JsonQueue(**self.conn_params, delegate=self.get_consumer()) self.write_conn = self.queue.conn_for(ConnType.WRITE) self.consume_conn = self.queue.conn_for(ConnType.CONSUME) self.mock_connection()
class AsyncBaseTestCase: test_queue_name = "test_queue" consumer_tag = "consumer_666" def setUp(self): self.conn_params = dict( host="money.que.é.good", username="******", password="******", virtual_host="have", heartbeat=5, ) self.queue = JsonQueue(**self.conn_params, delegate=self.get_consumer()) self.write_conn = self.queue.conn_for(ConnType.WRITE) self.consume_conn = self.queue.conn_for(ConnType.CONSUME) self.mock_connection() def tearDown(self): self._connect_patch.stop() def mock_connection(self): class SubscriptableCoroutineMock(CoroutineMock): def __getitem__(_, item): if item == "consumer_tag": return self.consumer_tag raise NotImplementedError self._transport = CoroutineMock(name="transport") self._protocol = CoroutineMock(name="protocol", close=CoroutineMock()) self._protocol.channel = SubscriptableCoroutineMock( return_value=CoroutineMock( publish=CoroutineMock(), basic_qos=CoroutineMock(), basic_consume=CoroutineMock( return_value={"consumer_tag": self.consumer_tag} ), ) ) mocked_connection = CoroutineMock( return_value=[self._transport, self._protocol] ) self._connect_patch = patch.object( aioamqp, "connect", mocked_connection ) self._connect = self._connect_patch.start() def get_consumer(self) -> QueueConsumerDelegate: raise NotImplementedError
def __init__( self, route_info: Union[Dict, AMQPRoute], host: str, username: str, password: str, prefetch_count: int = 128, bucket_class: Type[Bucket] = Bucket[RabbitMQMessage], ) -> None: self.route = route_info self._handler = route_info["handler"] self._queue_name = route_info["routes"] self._route_options = route_info["options"] self.host = host self.vhost = route_info.get("vhost", "/") if self.vhost != "/": self.vhost = self.vhost.lstrip("/") self.bucket = bucket_class( size=min(self._route_options["bulk_size"], prefetch_count)) self.queue: JsonQueue = JsonQueue( host, username, password, virtual_host=self.vhost, delegate=self, prefetch_count=prefetch_count, ) self.clock = ClockTicker(seconds=conf.settings.FLUSH_TIMEOUT) self.clock_task = None
def __init__( self, route_info: Union[Dict, AMQPRoute], host: str, username: str, password: str, prefetch_count: int = 128, bucket_class: Type[Bucket] = Bucket[RabbitMQMessage], ) -> None: self.route = route_info self._handler = route_info["handler"] self._queue_name = route_info["routes"] self._route_options = route_info["options"] self.host = host self.vhost = route_info.get("vhost", "/") self.bucket = bucket_class( size=min(self._route_options["bulk_size"], prefetch_count)) self.queue: JsonQueue = JsonQueue( host, username, password, virtual_host=self.vhost, delegate=self, prefetch_count=prefetch_count, logger=conf.logger, connection_fail_callback=self._route_options.get( Options.CONNECTION_FAIL_CALLBACK, None), ) self.clock = ClockTicker(seconds=self._route_options.get( Options.BULK_FLUSH_INTERVAL, conf.settings.FLUSH_TIMEOUT)) self.clock_task = None
async def test_its_possibile_to_initialize_without_a_delegate(self): queue = JsonQueue( host="diogommartins.com", username="******", password="******", loop=Mock(), ) self.assertIsInstance(queue, JsonQueue)
async def setUp(self): self.seconds = 666 self.queue = JsonQueue( "127.0.0.1", "guest", "guest", seconds_between_conn_retry=self.seconds, logger=Mock(spec=logging.Logger), connection_fail_callback=CoroutineMock(), )
async def test_it_raises_an_error_if_its_initialized_with_both_delegate_and_delegate_class( self): with self.assertRaises(ValueError): JsonQueue( host="diogommartins.com", username="******", password="******", loop=Mock(), delegate=Mock(), delegate_class=Mock(), )
async def test_it_doesnt_raise_value_error_if_max_message_length_is_zero( self): valid_value = 0 queue = JsonQueue( host="diogommartins.com", username="******", password="******", loop=Mock(), delegate=Mock(), max_message_length=valid_value, ) self.assertEqual(queue.max_message_length, valid_value)
async def test_it_doesnt_raise_value_error_if_max_message_length_is_a_positive_number( self): valid_value = 666 queue = JsonQueue( host="Essa", username="******", password="******", loop=Mock(), delegate=Mock(), max_message_length=valid_value, ) self.assertEqual(queue.max_message_length, valid_value)
async def test_it_raises_value_error_if_max_message_length_is_a_negative_number( self): invalid_value = -666 with self.assertRaises(ValueError): JsonQueue( host="Olha", username="******", password="******", loop=Mock(), delegate=Mock(), max_message_length=invalid_value, )
def __getitem__(self, key: str) -> JsonQueue: """ Gets a JsonQueue instance for a given virtual host :param key: The virtual host of the connection :return: An instance of the connection """ try: return self.__connections[key] except KeyError: conn: JsonQueue = JsonQueue( host=self.hostname, username=self.username, password=self.password, virtual_host=key, ) self.__connections[key] = conn return conn
async def setUp(self): self.queue = JsonQueue(host="127.0.0.1", username="******", password="******") self.write_conn = self.queue.conn_for(ConnType.WRITE) self.wrapped = _ensure_conn_is_ready(ConnType.WRITE)(self._func)
class EnsureConnectedTest(TestCase): async def setUp(self): self.queue = JsonQueue(host="127.0.0.1", username="******", password="******") self.write_conn = self.queue.conn_for(ConnType.WRITE) self.wrapped = _ensure_conn_is_ready(ConnType.WRITE)(self._func) async def _func(self, arg1): return 42 async def tearDown(self): await self.write_conn.close() async def test_create_new_channel_if_channel_is_closed(self): await self.wrapped(self.queue) await self.write_conn.channel.close() channel, proto, transp = ( self.write_conn.channel, self.write_conn._protocol, self.write_conn._transport, ) self.assertEqual(42, await self.wrapped(self.queue)) self.assertNotEqual(channel, self.write_conn.channel) self.assertEqual(proto, self.write_conn._protocol) self.assertEqual(transp, self.write_conn._transport) async def test_do_nothing_if_protocol_and_channel_are_open(self): await self.wrapped(self.queue) channel, proto, transp = ( self.write_conn.channel, self.write_conn._protocol, self.write_conn._transport, ) self.assertEqual(42, await self.wrapped(self.queue)) self.assertEqual(channel, self.write_conn.channel) self.assertEqual(proto, self.write_conn._protocol) self.assertEqual(transp, self.write_conn._transport) async def test_recreate_connected_if_protocol_is_closed(self): await self.wrapped(self.queue) await self.write_conn.channel.close() await self.write_conn._protocol.close() channel, proto, transp = ( self.write_conn.channel, self.write_conn._protocol, self.write_conn._transport, ) self.assertEqual(42, await self.wrapped(self.queue)) self.assertNotEqual(channel, self.write_conn.channel) self.assertNotEqual(proto, self.write_conn._protocol) self.assertNotEqual(transp, self.write_conn._transport) self.assertTrue(self.write_conn.channel.is_open) self.assertEqual(self.write_conn._protocol.state, OPEN)
async def test_it_initializes_a_delegate_if_delegate_class_is_provided( self ): delegate_class = Mock() JsonQueue(Mock(), Mock(), Mock(), delegate_class=delegate_class) delegate_class.assert_called_once_with()