Ejemplo n.º 1
0
def test_get_event_names(ee: EventEmitter,
                         arg_swallower: Callable[..., None]) -> None:
    events = {"event", "event1", "event2", "event3"}
    for event in events:
        ee.on(event, arg_swallower)
    assert all(registered_event_name in events
               for registered_event_name in ee.event_names())
Ejemplo n.º 2
0
def test_on_does_not_emits_error_when_not_listening_for_errors(
        ee: EventEmitter, error_helper: "EEExceptionHelper") -> None:
    ee.on("event", error_helper.error_raiser)
    assert ee.listener_count("event") == 1
    assert ee.listener_count("error") == 0
    assert ee.emit("event", 1, data=2)
    error_helper.assert_error_was_not_emitted()
Ejemplo n.º 3
0
 def addEventListener(
     self, emitter: EventEmitter, eventName: str, handler: Callable
 ) -> None:
     emitter.on(eventName, handler)
     self.listeners.append(
         dict(emitter=emitter, eventName=eventName, handler=handler)
     )
Ejemplo n.º 4
0
def test_on_decorator(ee: EventEmitter, mock: Mock) -> None:
    @ee.on("event")
    def handler(*args, **kwargs) -> None:
        mock.method(*args, **kwargs)

    assert ee.listener_count("event") == 1
    assert handler is ee.listeners("event")[0]
    assert ee.emit("event", 1, data=2)
    mock.method.assert_called_with(1, data=2)
    assert ee.listener_count("event") == 1
    assert handler is ee.listeners("event")[0]
Ejemplo n.º 5
0
async def test_coroutine_listener_does_not_emits_error_when_not_listening_for_errors(
    ee_with_event_loop: EventEmitter,
    error_helper: "EEExceptionHelper",
    event_loop: AbstractEventLoop,
) -> None:
    error_helper.with_deferred(event_loop)
    ee_with_event_loop.on("event", error_helper.error_raiser_async)
    assert ee_with_event_loop.listener_count("event") == 1
    assert ee_with_event_loop.listener_count("error") == 0
    assert ee_with_event_loop.emit("event", 1, data=2)
    await error_helper.assert_error_was_not_emitted_async()
Ejemplo n.º 6
0
def test_once_decorator_does_not_emits_error_when_not_listening_for_errors(
        ee: EventEmitter, error_helper: "EEExceptionHelper") -> None:
    @ee.once("event")
    def handler(*args, **kwargs) -> None:
        error_helper.error_raiser(*args, **kwargs)

    assert ee.listener_count("event") == 1
    assert ee.listener_count("error") == 0
    assert ee.emit("event", 1, data=2)
    error_helper.assert_error_was_not_emitted()
    assert ee.listener_count("event") == 0
    assert ee.listener_count("error") == 0
Ejemplo n.º 7
0
async def test_coroutine_listener_decorator(ee_with_event_loop: EventEmitter,
                                            mock: Mock,
                                            deferred: Future) -> None:
    @ee_with_event_loop.on("event")
    async def handler(*args, **kwargs) -> None:
        mock.method(*args, **kwargs)
        deferred.set_result(True)

    assert ee_with_event_loop.listener_count("event") == 1
    assert handler is ee_with_event_loop.listeners("event")[0]
    assert ee_with_event_loop.emit("event", 1, data=2)
    assert await deferred
    mock.method.assert_called_with(1, data=2)
    assert ee_with_event_loop.listener_count("event") == 1
    assert handler is ee_with_event_loop.listeners("event")[0]
Ejemplo n.º 8
0
    async def waitEvent(
        emitter: EventEmitter,
        eventName: str,
        predicate: Optional[Callable[[Any], bool]] = None,
        loop: Optional[AbstractEventLoop] = None,
    ) -> Future:
        _loop = loop if loop is not None else aio_get_event_loop()
        _predicate = predicate if predicate is not None else dummy_predicate
        promise = _loop.create_future()

        def listener(event: Any = None) -> None:
            if _predicate(event) and not promise.done():
                emitter.remove_listener(eventName, listener)
                promise.set_result(event)

        emitter.on(eventName, listener)
        return promise
Ejemplo n.º 9
0
def test_once(ee: EventEmitter, mock: Mock) -> None:
    ee.once("event", mock.method)

    assert ee.listener_count("event") == 1
    assert mock.method is ee.listeners("event")[0]
    assert ee.emit("event", 1, data=2)
    assert not ee.emit("event", 1, data=2)
    mock.method.assert_called_once_with(1, data=2)
    assert ee.listener_count("event") == 0
    assert mock.method not in ee.listeners("event")
Ejemplo n.º 10
0
def test_remove_all_listeners(ee: EventEmitter,
                              arg_swallower: Callable[..., None]) -> None:
    ee.on("event", arg_swallower)
    assert ee.listener_count("event") == 1
    assert arg_swallower is ee.listeners("event")[0]
    ee.remove_all_listeners()
    assert ee.listener_count("event") == 0
Ejemplo n.º 11
0
async def test_coroutine_listener_once_decorator_emits_error_when_listening_for_errors(
    ee_with_event_loop: EventEmitter,
    error_helper: "EEExceptionHelper",
    event_loop: AbstractEventLoop,
) -> None:
    error_helper.with_deferred(event_loop)

    @ee_with_event_loop.once("event")
    async def error_raiser(*args, **kwargs) -> None:
        await error_helper.error_raiser_async(*args, **kwargs)

    @ee_with_event_loop.once("error")
    async def error_listener(*args, **kwargs) -> None:
        await error_helper.error_listener_async(*args, **kwargs)

    assert ee_with_event_loop.listener_count("event") == 1
    assert ee_with_event_loop.listener_count("error") == 1
    assert ee_with_event_loop.emit("event", 1, data=2)
    await error_helper.assert_error_was_emitted_async()
    assert ee_with_event_loop.listener_count("event") == 0
    assert ee_with_event_loop.listener_count("error") == 0
Ejemplo n.º 12
0
def ee_with_event_loop(event_loop: AbstractEventLoop) -> EventEmitter:
    return EventEmitter(loop=event_loop)
Ejemplo n.º 13
0
def ee() -> EventEmitter:
    return EventEmitter()
Ejemplo n.º 14
0
def test_get_listeners_registered_for_event(
        ee: EventEmitter, arg_swallower: Callable[..., None]) -> None:
    ee.on("event", arg_swallower)
    ee_listeners = ee.listeners("event")
    assert len(ee_listeners) == 1
    assert ee_listeners[0] is arg_swallower