Пример #1
0
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))
Пример #2
0
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
Пример #3
0
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)