Skip to content

Commit

Permalink
Merge pull request #536 from byllyfish/event_emitter
Browse files Browse the repository at this point in the history
Test some assumptions about event emitter implementation.
  • Loading branch information
byllyfish authored May 12, 2024
2 parents 3ec97e2 + aac9586 commit 8fb407f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 1 addition & 2 deletions examples/ngsdn/ngsdn/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class RouteManagerOneShot:

def __init__(self, switch: fy.Switch):
self.switch = switch

async def run(self):
self.switch.ee.add_listener(LinkEvent.LINK_READY, self._link_ready)

async def run(self):
await self._init_profiles()

await self.switch.write(
Expand Down
37 changes: 37 additions & 0 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,43 @@ async def test_switch4(p4rt_server_target: str):
await asyncio.sleep(0.01)


async def test_switch_ee(p4rt_server_target: str):
"Test switch's event emitter."

def _dump_ee(sw: Switch) -> dict[str, int]:
return {
event: len(
sw.ee.listeners( # pyright: ignore[reportUnknownArgumentType, reportUnknownMemberType]
event
)
)
for event in sw.ee.event_names()
}

options = SwitchOptions(
p4info=Path("tests/test_data/p4info/basic.p4.p4info.txt"),
)

sw1 = Switch("sw1", p4rt_server_target, options)

assert _dump_ee(sw1) == {}
async with sw1:
assert _dump_ee(sw1) == {}
assert _dump_ee(sw1) == {}

def _ignore(*_: object):
pass

sw1.ee.add_listener(SwitchEvent.CHANNEL_UP, _ignore)
sw1.ee.add_listener(SwitchEvent.CHANNEL_UP, _ignore) # dup coalesced
sw1.ee.add_listener(SwitchEvent.CHANNEL_DOWN, _ignore)

assert _dump_ee(sw1) == {"channel_up": 1, "channel_down": 1}
async with sw1:
assert _dump_ee(sw1) == {"channel_up": 1, "channel_down": 1}
assert _dump_ee(sw1) == {"channel_up": 1, "channel_down": 1}


def test_switch_options():
"Test the SwitchOptions class."
some_path = Path("/")
Expand Down

0 comments on commit 8fb407f

Please sign in to comment.