def test_avro_serialization(self): message = BrokerMessage( self.topic, self.data, identifier=self.identifier, reply_topic=self.reply_topic, user=self.user, status=self.status, strategy=self.strategy, ) observed = BrokerMessage.from_avro_bytes(message.avro_bytes) self.assertEqual(message, observed)
def test_ok(self): self.assertTrue( BrokerMessage(self.topic, self.data, status=BrokerMessageStatus.SUCCESS).ok) self.assertFalse( BrokerMessage(self.topic, self.data, status=BrokerMessageStatus.ERROR).ok) self.assertFalse( BrokerMessage(self.topic, self.data, status=BrokerMessageStatus.SYSTEM_ERROR).ok)
def test_constructor_simple(self): message = BrokerMessage(self.topic, self.data) self.assertEqual(self.topic, message.topic) self.assertEqual(self.data, message.data) self.assertIsInstance(message.identifier, UUID) self.assertEqual(None, message.reply_topic) self.assertEqual(None, message.user) self.assertEqual(BrokerMessageStatus.SUCCESS, message.status) self.assertEqual(BrokerMessageStrategy.UNICAST, message.strategy)
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", )
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))
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)
async def test_dispatch_with_order(self): observed = defaultdict(list) async def _fn2(request): content = await request.content() observed[content[0]].append(content[1]) self.handler.get_action = MagicMock(return_value=_fn2) events = list() for i in range(1, 6): events.extend([ BrokerMessage("TicketAdded", ["uuid1", i]), BrokerMessage("TicketAdded", ["uuid2", i]) ]) shuffle(events) for event in events: await self._insert_one(event) await self.handler.dispatch() expected = {"uuid1": list(range(1, 6)), "uuid2": list(range(1, 6))} self.assertEqual(expected, observed)
def test_constructor(self): message = BrokerMessage( self.topic, self.data, identifier=self.identifier, reply_topic=self.reply_topic, user=self.user, status=self.status, strategy=self.strategy, ) self.assertEqual(self.topic, message.topic) self.assertEqual(self.data, message.data) self.assertEqual(self.identifier, message.identifier) self.assertEqual(self.reply_topic, message.reply_topic) self.assertEqual(self.user, message.user) self.assertEqual(self.status, message.status) self.assertEqual(self.strategy, message.strategy)
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"}, )
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)
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())
def test_sort(self): unsorted = [ BrokerHandlerEntry(1, "", 0, BrokerMessage("", "foo").avro_bytes, 1), BrokerHandlerEntry(1, "", 0, BrokerMessage("", 4).avro_bytes, 1), BrokerHandlerEntry(1, "", 0, BrokerMessage("", 2).avro_bytes, 1), BrokerHandlerEntry(1, "", 0, BrokerMessage("", 3).avro_bytes, 1), BrokerHandlerEntry(1, "", 0, BrokerMessage("", 1).avro_bytes, 1), BrokerHandlerEntry(1, "", 0, BrokerMessage("", "bar").avro_bytes, 1), ] expected = [ unsorted[0], unsorted[4], unsorted[2], unsorted[3], unsorted[1], unsorted[5] ] observed = sorted(unsorted) self.assertEqual(expected, observed)
def test_user(self): raw = BrokerMessage("FooCreated", self.data, user=uuid4()) request = BrokerRequest(raw) self.assertEqual(raw.user, request.user)
def test_eq_false(self): self.assertNotEqual( self.request, BrokerRequest(BrokerMessage("FooUpdated", self.data)))
def setUp(self) -> None: self.data = [FakeModel("foo"), FakeModel("bar")] self.identifier = uuid4() self.raw = BrokerMessage("FooCreated", self.data) self.request = BrokerRequest(self.raw)