async def test_wrap_listeners_context(self) -> None: mock = Mock() with emitter.context() as ctx: listeners = ctx.wrap_listeners(Global) emitter.on("test", listeners, mock) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() self.assertTrue(await emitter.emit(None, listeners, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() self.assertTrue(emitter.remove("test", Global, mock, ctx)) self.assertFalse(await emitter.emit(None, Global, scope="test")) self.assertFalse(await emitter.emit(None, listeners, scope="test")) mock.assert_not_called() emitter.on("test", listeners, mock) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() self.assertTrue(await emitter.emit(None, listeners, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() self.assertTrue(emitter.remove("test", listeners, mock)) self.assertFalse(await emitter.emit(None, Global, scope="test")) self.assertFalse(await emitter.emit(None, listeners, scope="test")) mock.assert_not_called()
async def test_context(scope: str, time: T.Union[int, float]) -> None: mock = Mock() mock2 = Mock() with emitter.context() as context: @emitter.on(scope, Global) def main_listener(_: T.Any) -> None: emitter.on(scope, Global, mock) await emitter.emit(None, Global, scope=scope) mock.assert_not_called() mock2.assert_not_called() await emitter.emit(None, Global, scope=scope) mock.assert_called_once_with(None) mock2.assert_not_called() mock.reset_mock() @emitter.on(scope, Global) async def async_main_listener(_: T.Any) -> None: await asyncio.sleep(0) emitter.on(scope, Global, mock2) await asyncio.sleep(time) await emitter.emit(None, Global, scope=scope) mock.assert_called_once_with(None) mock2.assert_not_called() mock.reset_mock() await emitter.emit(None, Global, scope=scope) mock.assert_called_once_with(None) mock.reset_mock() mock2.assert_called_once_with(None) mock2.reset_mock() emitter.remove(None, Global, context=context) await emitter.emit(None, Global, scope=scope) mock.assert_not_called() mock2.assert_not_called()
async def test_context(self) -> None: e = Event("") mock = Mock() mock2 = Mock() self.assertFalse(await emitter.emit(e, Global)) with emitter.context() as context: self.assertFalse(await emitter.emit(e, Global)) self.assertFalse(await emitter.emit(None, Global, scope="test")) mock.assert_not_called() mock2.assert_not_called() @emitter.on(Event, Global) def main_listener(_: T.Any) -> None: emitter.on("test", Global, mock) self.assertTrue(await emitter.emit(e, Global)) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() @emitter.on(Event, Global) async def async_main_listener(_: T.Any) -> None: await asyncio.sleep(0) emitter.on("test", Global, mock2) self.assertTrue(await emitter.emit(e, Global)) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_called_once_with(None) mock.reset_mock() mock2.assert_called_once_with(None) mock2.reset_mock() emitter.remove(None, Global, context=context) self.assertFalse(await emitter.emit(e, Global)) self.assertFalse(await emitter.emit(None, Global, scope="test")) mock.assert_not_called() mock2.assert_not_called()
async def test_context_stack(self) -> None: mock = Mock() with emitter.context() as ctx0: emitter.on("test", Global, lambda x: mock(1)) @emitter.on("test", Global) async def async_main_listener0(_: T.Any) -> None: await asyncio.sleep(0) emitter.on("test", Global, lambda x: mock(2)) emitter.remove("test", Global, async_main_listener0) with emitter.context() as ctx1: emitter.on("test", Global, lambda x: mock(3)) with emitter.context() as ctx2: emitter.on("test", Global, lambda x: mock(4)) with emitter.context(): @emitter.on("test", Global) async def async_main_listener1(_: T.Any) -> None: await asyncio.sleep(0) emitter.on("test", Global, lambda x: mock(5)) emitter.remove("test", Global, async_main_listener1) with emitter.context(): emitter.on("test", Global, lambda x: mock(6)) with emitter.context() as ctx3: listener0 = ctx3.wrap_listeners(Global) with emitter.context(): emitter.on("test", listener0, lambda x: mock(7)) with emitter.context() as ctx5: listener1 = ctx5.wrap_listeners(Global) @emitter.on("test", Global) async def setup_remove(_: T.Any) -> None: await asyncio.sleep(0) emitter.remove("test", Global) emitter.on("test", listener1, lambda x: mock(8)) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_has_calls( [call(1), call(3), call(4), call(6), call(7), call(8)]) mock.reset_mock() self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_has_calls( [call(1), call(3), call(4), call(6), call(2), call(5)]) mock.reset_mock() emitter.remove(None, Global, context=ctx2) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_has_calls([call(1), call(3), call(6), call(2), call(5)]) mock.reset_mock() emitter.remove(None, Global, context=ctx0) self.assertTrue(await emitter.emit(None, Global, scope="test")) mock.assert_has_calls([call(3), call(6), call(5)]) mock.reset_mock() emitter.remove(None, Global, context=ctx1) self.assertFalse(await emitter.emit(None, Global, scope="test")) mock.assert_not_called()