コード例 #1
0
ファイル: test_validator.py プロジェクト: pamir-s/trinity
async def test_validator_handle_slot_tick(event_loop, event_bus, monkeypatch):
    alice = await get_validator(
        event_loop=event_loop, event_bus=event_bus, monkeypatch=monkeypatch, indices=[0]
    )

    event_first_tick_called = asyncio.Event()
    event_second_tick_called = asyncio.Event()
    event_third_tick_called = asyncio.Event()

    async def handle_first_tick(slot):
        event_first_tick_called.set()

    async def handle_second_tick(slot):
        event_second_tick_called.set()

    async def handle_third_tick(slot):
        event_third_tick_called.set()

    monkeypatch.setattr(alice, "handle_first_tick", handle_first_tick)
    monkeypatch.setattr(alice, "handle_second_tick", handle_second_tick)
    monkeypatch.setattr(alice, "handle_third_tick", handle_third_tick)

    # sleep for `event_bus` ready
    await asyncio.sleep(0.01)

    # First tick
    await event_bus.broadcast(
        SlotTickEvent(slot=1, elapsed_time=2, tick_type=TickType.SLOT_START),
        BroadcastConfig(internal=True),
    )
    await asyncio.wait_for(event_first_tick_called.wait(), timeout=2, loop=event_loop)
    assert event_first_tick_called.is_set()
    assert not event_second_tick_called.is_set()
    assert not event_third_tick_called.is_set()
    event_first_tick_called.clear()

    # Second tick
    await event_bus.broadcast(
        SlotTickEvent(slot=1, elapsed_time=2, tick_type=TickType.SLOT_ONE_THIRD),
        BroadcastConfig(internal=True),
    )
    await asyncio.wait_for(event_second_tick_called.wait(), timeout=2, loop=event_loop)
    assert not event_first_tick_called.is_set()
    assert event_second_tick_called.is_set()
    assert not event_third_tick_called.is_set()
    event_second_tick_called.clear()

    # Third tick
    await event_bus.broadcast(
        SlotTickEvent(slot=1, elapsed_time=2, tick_type=TickType.SLOT_TWO_THIRD),
        BroadcastConfig(internal=True),
    )
    await asyncio.wait_for(event_third_tick_called.wait(), timeout=2, loop=event_loop)
    assert not event_first_tick_called.is_set()
    assert not event_second_tick_called.is_set()
    assert event_third_tick_called.is_set()
コード例 #2
0
ファイル: test_validator.py プロジェクト: wschwab/trinity
async def test_validator_handle_slot_tick(event_loop, event_bus, monkeypatch):
    alice = await get_validator(event_loop=event_loop,
                                event_bus=event_bus,
                                indices=[0])

    event_first_tick_called = asyncio.Event()
    event_second_tick_called = asyncio.Event()

    async def handle_first_tick(slot):
        event_first_tick_called.set()

    async def handle_second_tick(slot):
        event_second_tick_called.set()

    monkeypatch.setattr(alice, 'handle_first_tick', handle_first_tick)
    monkeypatch.setattr(alice, 'handle_second_tick', handle_second_tick)

    # sleep for `event_bus` ready
    await asyncio.sleep(0.01)

    # First tick
    await event_bus.broadcast(
        SlotTickEvent(
            slot=1,
            elapsed_time=2,
            is_second_tick=False,
        ),
        BroadcastConfig(internal=True),
    )
    await asyncio.wait_for(
        event_first_tick_called.wait(),
        timeout=2,
        loop=event_loop,
    )
    assert not event_second_tick_called.is_set()
    event_first_tick_called.clear()

    # Second tick
    await event_bus.broadcast(
        SlotTickEvent(
            slot=1,
            elapsed_time=2,
            is_second_tick=True,
        ),
        BroadcastConfig(internal=True),
    )
    await asyncio.wait_for(
        event_second_tick_called.wait(),
        timeout=2,
        loop=event_loop,
    )
    assert not event_first_tick_called.is_set()