Skip to content

Instantly share code, notes, and snippets.

@vortec
Last active December 30, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vortec/54a52acaea08e77023d3d670d7d753ee to your computer and use it in GitHub Desktop.
Save vortec/54a52acaea08e77023d3d670d7d753ee to your computer and use it in GitHub Desktop.
class EventDispatcher:
def __init__(self, loop, input_handler, output_handler):
self.loop = loop
self.input_handler = input_handler
self.output_handler = output_handler
async def __call__(self, event):
# Generic stuff (logging, benchmarking, ...)
message = await self.input_handler(event)
await self.output_handler(message)
# Current and only way to test if the input handler gets called:
@pytest.mark.asyncio
async def test_calls_input_handler(loop):
async def input_handler(event):
input_handler.called = True
async def output_handler(_):
pass
dispatcher = EventDispatcher(input_handler, output_handler)
await dispatcher('abc')
assert input_handler.called is True
# What I would prefer:
@pytest.mark.asyncio
async def test_calls_input_handler(loop):
from unittest.mock import AsyncMock
input_handler = AsyncMock()
dispatcher = EventDispatcher(input_handler, AsyncMock())
await dispatcher('abc')
assert input_handler.called is True
# Or even:
@pytest.mark.asyncio
async def test_calls_input_handler(loop):
async def input_handler(event):
input_handler.called = True
output_handler = async lambda: None
dispatcher = EventDispatcher(input_handler, output_handler)
await dispatcher('abc')
assert input_handler.called is True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment