Beispiel #1
0
async def test_bus_iterate():
    queue = asyncio.Queue()
    bus = events.EventBus(registry=dummy_bus_event_registry, queue=queue)

    async def fake_task():
        pass

    bus._listen_task = asyncio.create_task(fake_task())
    await queue.put("item")
    assert await bus.__anext__() is "item"
Beispiel #2
0
async def test_bus__event_listener(caplog):
    queue = asyncio.Queue()
    bus = events.EventBus(registry=dummy_bus_event_registry, queue=queue)
    bus._react = CoroutineMock()

    async def fake_task():
        pass

    bus._listen_task = asyncio.create_task(fake_task())
    listen_task = asyncio.create_task(bus._event_listener())
    await asyncio.sleep(0)
    await queue.put("item")
    await asyncio.sleep(0)
    bus._react.assert_called_once_with("item")
Beispiel #3
0
async def test_bus__event_listener_cancel_and_shield_react(
        caplog, monkeypatch):
    caplog.set_level(logging.INFO)
    queue = asyncio.Queue()
    bus = events.EventBus(registry=dummy_bus_event_registry, queue=queue)

    async def fake_task():
        pass

    watch_value = "abcdef.123456"
    side_effect_duration = 0.1

    async def mockreact(message):
        await asyncio.sleep(side_effect_duration)
        logging.info(message)

    bus._react = mockreact
    bus._listen_task = asyncio.create_task(fake_task())
    listen_task = asyncio.create_task(bus._event_listener())
    await asyncio.sleep(0)

    # Trigger a side-effect by putting an item in the queue and waiting for the
    # listener to react
    await queue.put(watch_value)
    await asyncio.sleep(0)

    # Cancel the listening task
    listen_task.cancel()
    await listen_task

    # Side effect has not completed yet...
    assert watch_value not in caplog.text
    await asyncio.sleep(side_effect_duration * 2)

    # Side effect should complete and log the watched value now, because of the
    # cancellation shielding!
    assert watch_value in caplog.text
Beispiel #4
0
    print(aircraft.global_id, "landing in", airport)
    return Landed(airport=airport)


async def think_reactor(aggregate_id, event, context):
    await asyncio.sleep(1)
    print("Did we forget to pack something important?")


async def main(_aircrafts):
    async with aggregates.execute_transaction(_aircrafts) as aircraft:
        aircraft.execute(schedule, "DAILY2018")
        aircraft.execute(takeoff)
    print("Aircraft status:", aircraft)
    await asyncio.sleep(2)
    async with aggregates.execute_transaction(_aircrafts,
                                              "DAILY2018") as aircraft:
        aircraft.execute(land, "Paris CDG")
    print("Aircraft status:", aircraft)


if __name__ == "__main__":
    air_traffic_bus = events.EventBus(registry=FlightEvent.registry)
    air_traffic_bus.subscribe(think_reactor, TakenOff.topic)
    aircrafts = AircraftRepository(events.DictEventStore(),
                                   bus=air_traffic_bus)
    air_traffic_bus.listen()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main(aircrafts))
    air_traffic_bus.close(timeout=5)
Beispiel #5
0
def dummy_bus(dummy_queue):
    bus = events.EventBus(registry=dummy_bus_event_registry, queue=dummy_queue)
    bus._event_listener = CoroutineMock()
    return bus