Пример #1
0
    async def test_get_many(self):
        expected = [
            BrokerHandlerEntry(1, "fooReply", 0,
                               FakeModel("test1").avro_bytes),
            BrokerHandlerEntry(2, "fooReply", 0,
                               FakeModel("test2").avro_bytes),
            BrokerHandlerEntry(3, "fooReply", 0,
                               FakeModel("test3").avro_bytes),
            BrokerHandlerEntry(4, "fooReply", 0,
                               FakeModel("test4").avro_bytes),
        ]

        async def _fn():
            await self._insert_one(
                Message("fooReply", 0,
                        FakeModel("test1").avro_bytes))
            await self._insert_one(
                Message("fooReply", 0,
                        FakeModel("test2").avro_bytes))
            await sleep(0.5)
            await self._insert_one(
                Message("fooReply", 0,
                        FakeModel("test3").avro_bytes))
            await self._insert_one(
                Message("fooReply", 0,
                        FakeModel("test4").avro_bytes))

        observed, _ = await gather(
            self.handler.get_many(count=4, max_wait=0.1), _fn())

        self.assertEqual(len(expected), len(observed))
        for e, o in zip(expected, observed):
            self._assert_equal_entries(e, o)
Пример #2
0
 def setUp(self) -> None:
     self.topic = "FooCreated"
     self.data = [FakeModel("blue"), FakeModel("red")]
     self.identifier = uuid4()
     self.reply_topic = "AddOrderReply"
     self.status = BrokerMessageStatus.SUCCESS
     self.user = uuid4()
     self.strategy = BrokerMessageStrategy.MULTICAST
Пример #3
0
    async def test_if_commands_was_deleted(self):
        async with BrokerPublisher.from_config(config=self.config) as broker_publisher:
            await broker_publisher.send(FakeModel("Foo"), "TestDeleteReply")
            await broker_publisher.send(FakeModel("Foo"), "TestDeleteReply")

        await self.producer.dispatch()

        async with aiopg.connect(**self.broker_queue_db) as connection:
            async with connection.cursor() as cursor:
                await cursor.execute("SELECT COUNT(*) FROM producer_queue WHERE topic = '%s'" % "TestDeleteReply")
                self.assertEqual(0, (await cursor.fetchone())[0])
Пример #4
0
    async def test_get_one(self):
        expected = BrokerHandlerEntry(1, "fooReply", 0,
                                      FakeModel("test1").avro_bytes)
        await self._insert_one(
            Message("fooReply", 0,
                    FakeModel("test1").avro_bytes))
        await self._insert_one(
            Message("fooReply", 0,
                    FakeModel("test2").avro_bytes))

        observed = await self.handler.get_one()

        self._assert_equal_entries(expected, observed)
Пример #5
0
 async def _fn():
     await self._insert_one(
         Message("fooReply", 0,
                 FakeModel("test1").avro_bytes))
     await self._insert_one(
         Message("fooReply", 0,
                 FakeModel("test2").avro_bytes))
     await sleep(0.5)
     await self._insert_one(
         Message("fooReply", 0,
                 FakeModel("test3").avro_bytes))
     await self._insert_one(
         Message("fooReply", 0,
                 FakeModel("test4").avro_bytes))
Пример #6
0
    async def test_if_commands_retry_was_incremented(self):
        model = FakeModel("foo")
        identifier = uuid4()

        async with BrokerPublisher.from_config(config=self.config) as broker_publisher:
            await broker_publisher.send(
                model, "TestDeleteOrderReply", identifier=identifier, status=BrokerMessageStatus.SUCCESS
            )
            await broker_publisher.send(
                model, "TestDeleteOrderReply", identifier=identifier, status=BrokerMessageStatus.SUCCESS
            )

            self.producer.publish = AsyncMock(return_value=False)
            await self.producer.dispatch()

        async with aiopg.connect(**self.broker_queue_db) as connection:
            async with connection.cursor() as cursor:
                await cursor.execute("SELECT COUNT(*) FROM producer_queue WHERE topic = 'TestDeleteOrderReply'")
                self.assertEqual(2, (await cursor.fetchone())[0])

                await cursor.execute("SELECT retry FROM producer_queue WHERE id=1;")
                self.assertEqual(1, (await cursor.fetchone())[0])

                await cursor.execute("SELECT retry FROM producer_queue WHERE id=2;")
                self.assertEqual(1, (await cursor.fetchone())[0])
Пример #7
0
    async def test_concurrency_dispatcher(self):
        model = FakeModel("foo")
        identifier = uuid4()

        broker_publisher = BrokerPublisher.from_config(config=self.config)

        async with broker_publisher:
            for x in range(60):
                await broker_publisher.send(
                    model, "CommandBroker-Delete", identifier=identifier, reply_topic="TestDeleteReply"
                )

        async with aiopg.connect(**self.broker_queue_db) as connect:
            async with connect.cursor() as cur:
                await cur.execute("SELECT COUNT(*) FROM producer_queue")
                records = await cur.fetchone()

        assert records[0] == 60

        await asyncio.gather(*(self.producer.dispatch() for _ in range(6)))

        async with aiopg.connect(**self.broker_queue_db) as connect:
            async with connect.cursor() as cur:
                await cur.execute("SELECT COUNT(*) FROM producer_queue")
                records = await cur.fetchone()

        assert records[0] == 0
    async def test_avro_model(self):
        expected = FakeModel("foobar")

        raw = avro_mocked_request(expected.avro_data, expected.avro_schema)
        request = RestRequest(raw)
        observed = await request.content()

        self.assertEqual(expected, observed)
Пример #9
0
    def setUp(self) -> None:
        self.identifier = uuid4()
        self.user = uuid4()

        self.message = BrokerMessage(
            "AddOrder",
            FakeModel("foo"),
            identifier=self.identifier,
            user=self.user,
            reply_topic="UpdateTicket",
        )
Пример #10
0
    async def test_dispatch_wrong(self):
        instance_1 = namedtuple("FakeCommand",
                                ("topic", "avro_bytes"))("AddOrder",
                                                         bytes(b"Test"))
        instance_2 = BrokerMessage("NoActionTopic", FakeModel("Foo"))

        queue_id_1 = await self._insert_one(instance_1)
        queue_id_2 = await self._insert_one(instance_2)
        await self.handler.dispatch()
        self.assertFalse(await self._is_processed(queue_id_1))
        self.assertFalse(await self._is_processed(queue_id_2))
Пример #11
0
    async def test_dispatch_without_sorting(self):
        observed = list()

        async def _fn2(request):
            content = await request.content()
            observed.append(content)

        self.handler.get_action = MagicMock(return_value=_fn2)

        events = [
            BrokerMessage("TicketAdded", FakeModel("uuid1")),
            BrokerMessage("TicketAdded", FakeModel("uuid2")),
        ]

        for event in events:
            await self._insert_one(event)

        await self.handler.dispatch()

        expected = [FakeModel("uuid1"), FakeModel("uuid2")]
        self.assertEqual(expected, observed)
Пример #12
0
    def setUp(self) -> None:
        super().setUp()

        self.publisher = BrokerPublisher.from_config(self.config)
        self.handler = BrokerHandler.from_config(self.config,
                                                 publisher=self.publisher)

        self.identifier = uuid4()
        self.user = uuid4()

        self.message = BrokerMessage(
            "AddOrder",
            FakeModel("foo"),
            identifier=self.identifier,
            user=self.user,
            reply_topic="UpdateTicket",
            headers={"foo": "bar"},
        )
Пример #13
0
    async def test_dispatch_one(self):
        callback_mock = AsyncMock()
        lookup_mock = MagicMock(return_value=callback_mock)

        topic = "TicketAdded"
        event = BrokerMessage(topic, FakeModel("Foo"))
        entry = BrokerHandlerEntry(1,
                                   topic,
                                   0,
                                   event.avro_bytes,
                                   1,
                                   callback_lookup=lookup_mock)

        await self.handler.dispatch_one(entry)

        self.assertEqual(1, lookup_mock.call_count)
        self.assertEqual(call("TicketAdded"), lookup_mock.call_args)

        self.assertEqual(1, callback_mock.call_count)
        self.assertEqual(call(BrokerRequest(event)), callback_mock.call_args)
Пример #14
0
    async def test_dispatch_concurrent(self):
        from tests.utils import (
            FakeModel, )

        identifier = uuid4()

        instance = BrokerMessage("AddOrder", [FakeModel("foo")],
                                 identifier=identifier,
                                 reply_topic="UpdateTicket")
        instance_wrong = namedtuple("FakeCommand",
                                    ("topic", "avro_bytes"))("AddOrder",
                                                             bytes(b"Test"))

        for _ in range(10):
            await self._insert_one(instance)
            await self._insert_one(instance_wrong)

        self.assertEqual(20, await self._count())

        await gather(*(self.handler.dispatch() for _ in range(2)))
        self.assertEqual(10, await self._count())
Пример #15
0
    async def test_dispatch(self):
        callback_mock = AsyncMock(return_value=Response("add_order"))
        lookup_mock = MagicMock(return_value=callback_mock)
        entry = BrokerHandlerEntry(1,
                                   "AddOrder",
                                   0,
                                   self.message.avro_bytes,
                                   1,
                                   callback_lookup=lookup_mock)

        send_mock = AsyncMock()
        self.publisher.send = send_mock

        await self.handler.dispatch_one(entry)

        self.assertEqual(1, lookup_mock.call_count)
        self.assertEqual(call("AddOrder"), lookup_mock.call_args)

        self.assertEqual(
            [
                call(
                    "add_order",
                    topic="UpdateTicket",
                    identifier=self.message.identifier,
                    status=BrokerMessageStatus.SUCCESS,
                    user=self.user,
                    headers={"foo": "bar"},
                )
            ],
            send_mock.call_args_list,
        )

        self.assertEqual(1, callback_mock.call_count)
        observed = callback_mock.call_args[0][0]
        self.assertIsInstance(observed, BrokerRequest)
        self.assertEqual(FakeModel("foo"), await observed.content())
 def setUp(self) -> None:
     self.data = [FakeModel("blue"), FakeModel("red")]
Пример #17
0
 async def test_content_single(self):
     response = BrokerResponse(FakeModel("foo"))
     self.assertEqual(FakeModel("foo"), await response.content())
Пример #18
0
 async def test_content(self):
     response = BrokerResponse([FakeModel("foo"), FakeModel("bar")])
     self.assertEqual([FakeModel("foo"), FakeModel("bar")], await
                      response.content())
Пример #19
0
 def setUp(self) -> None:
     self.data = [FakeModel("foo"), FakeModel("bar")]
     self.identifier = uuid4()
     self.raw = BrokerMessage("FooCreated", self.data)
     self.request = BrokerRequest(self.raw)
 def setUp(self) -> None:
     self.models = [FakeModel("foo"), FakeModel("bar")]
Пример #21
0
 async def test_get_callback(self):
     fn = self.handler.get_callback(_Cls._fn)
     self.assertEqual((FakeModel("foo"), BrokerMessageStatus.SUCCESS, {
         "foo": "bar"
     }), await fn(self.message))