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_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_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))
示例#6
0
    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