def test_wrong_publisher_type(): publisher = object() subscriber = BasicSubscriber() loop = asyncio.get_event_loop() with pytest.raises(TypeError): server = Dispatcher(publisher, subscriber, loop=loop) # A publisher without the publish method publisher = BasicPublisher() publisher.produce = None with pytest.raises(TypeError): server = Dispatcher(publisher, subscriber, loop=loop)
def test_custom_handler(): publisher = BasicPublisher() subscriber = BasicSubscriber2() loop = asyncio.get_event_loop() server = Dispatcher(publisher, [subscriber], loop=loop) loop.run_until_complete(server.start(max_events=5))
def test_basic_dispatch(): publisher = BasicPublisher() subscriber = BasicSubscriber() loop = asyncio.get_event_loop() server = Dispatcher(publisher, subscriber, loop=loop) loop.run_until_complete(server.start(max_events=5))
def test_wrong_max_events(): loop = asyncio.get_event_loop() server = Dispatcher(loop=loop) with pytest.raises(ValueError): loop.run_until_complete(server.start(max_events=-1)) with pytest.raises(ValueError): loop.run_until_complete(server.start(max_events="1"))
def test_bad_event(): publisher = BadPublisher() subscriber = BasicSubscriber2() loop = asyncio.get_event_loop() with pytest.raises(TypeError): server = Dispatcher(publisher, subscriber, loop=loop) loop.run_until_complete(server.start(max_events=1))
def test_not_implemented(): publisher = PublisherInterface() subscriber = SubscriberInterface() loop = asyncio.get_event_loop() with pytest.raises(NotImplementedError): server = Dispatcher(publisher, subscriber, loop=loop) loop.run_until_complete(server.start(max_events=1))
def test_add_remove_handler(): server = Dispatcher() handler = lambda x: x server.add_handler(handler, 'test') server.remove_handler(handler, 'test') assert len(server.get_events()) == 0 with pytest.raises(TypeError): server.add_handler('test', 'not_a_handler') with pytest.raises(TypeError): server.remove_handler('test', 'not_a_handler')
def __init__(self): self.total = 0 async def consume(self, event): event_name, timestamp, payload = event payload = json.loads(payload) self.total += int(payload['value']) def get_total(self): return self.total if __name__ == '__main__': publisher = BasicPublisher() subscriber = BasicSubscriber() loop = asyncio.get_event_loop() server = Dispatcher(publisher, subscriber, loop=loop) loop.run_until_complete(server.start(max_events=5)) total = subscriber.get_total() print("Total is %d" % total) assert total == 15
def test_wrong_subscriber_type(): server = Dispatcher() with pytest.raises(TypeError): server.add_subscriber(object())