Skip to content

Commit

Permalink
Renamed EventSource.dispatch() to EventSource.dispatch_event()
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Apr 17, 2016
1 parent 5ef6eed commit c831bca
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
6 changes: 3 additions & 3 deletions asphalt/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.dispatch('finished', exc_val)
await self.dispatch_event('finished', exc_val)

async def _publish_resource(self, value, alias: str, context_attr: str,
types: Iterable[Union[str, type]],
Expand Down Expand Up @@ -197,7 +197,7 @@ async def _publish_resource(self, value, alias: str, context_attr: str,
if creator is None and resource.context_attr:
setattr(self, context_attr, value)

await self.dispatch('resource_published', resource)
await self.dispatch_event('resource_published', resource)
return resource

async def publish_resource(
Expand Down Expand Up @@ -273,7 +273,7 @@ async def remove_resource(self, resource: Resource):
if resource.context_attr and resource.context_attr in self.__dict__:
delattr(self, resource.context_attr)

await self.dispatch('resource_removed', resource)
await self.dispatch_event('resource_removed', resource)

async def request_resource(self, type: Union[str, type], alias: str='default', *,
timeout: Union[int, float, None]=None, optional: bool=False):
Expand Down
2 changes: 1 addition & 1 deletion asphalt/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def remove_listener(self, handle: EventListener):
except (KeyError, ValueError):
raise LookupError('listener not found') from None

async def dispatch(self, event: Union[str, Event], *args, **kwargs) -> None:
async def dispatch_event(self, event: Union[str, Event], *args, **kwargs) -> None:
"""
Dispatch an event, optionally constructing one first.
Expand Down
2 changes: 1 addition & 1 deletion asphalt/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def run_application(component: Component, *, max_threads: int=None,
except (KeyboardInterrupt, SystemExit):
pass
finally:
event_loop.run_until_complete(context.dispatch('finished', exception))
event_loop.run_until_complete(context.dispatch_event('finished', exception))

event_loop.close()
logger.info('Application stopped')
Expand Down
4 changes: 2 additions & 2 deletions docs/userguide/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The event listener can be either a regular function or a coroutine function::
context.add_listener('finished', asynclistener)

# As we dispatch the "finished" event, the event object is printed twice
await context.dispatch('finished', None)
await context.dispatch_event('finished', None)

Waiting for a single event
--------------------------
Expand Down Expand Up @@ -100,5 +100,5 @@ the topic(s)::

And to dispatch a single ``MyCustomEvent`` from your new event source::

await MyEventSource().dispatch('sometopic', 'foo_value', bar='bar_value')
await MyEventSource().dispatch_event('sometopic', 'foo_value', bar='bar_value')

2 changes: 1 addition & 1 deletion docs/userguide/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ You could test it using `py.test`_ like this::
yield ctx

# This is run at test teardown
event_loop.run_until_complete(ctx.dispatch('finished', None))
event_loop.run_until_complete(ctx.dispatch_event('finished', None))


@pytest.fixture
Expand Down
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.
- *BACKWARD INCOMPATIBLE* Modified event dispatch logic in ``EventSource`` to always run all
event listeners even if some listeners raise exceptions. A uniform exception is then raised
that contains all the exceptions and the listeners who raised them.
- *BACKWARD INCOMPATIBLE* Renamed the ``EventSource.dispatch()`` method to ``dispatch_event``
to disambiguate the operation and to prevent name clashes with subclasses
- *BACKWARD INCOMPATIBLE* Event topic registrations for ``EventSource`` subclasses are now done
using the ``@register_topic`` class decorator instead of the ``_register_topic()`` method
- Added the ability to listen to multiple topics in an EventSource with a single listener
Expand Down
34 changes: 17 additions & 17 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async def test_topic_override(self):
events = []
source = target_class()
source.add_listener('some_event', events.append)
await source.dispatch('some_event')
await source.dispatch_event('some_event')
assert isinstance(events[0], event_subclass)


Expand All @@ -96,7 +96,7 @@ async def callback(event: Event):

events = []
source.add_listener('event_a', callback)
await source.dispatch('event_a', 'x', 'y', a=1, b=2)
await source.dispatch_event('event_a', 'x', 'y', a=1, b=2)

assert len(events) == 1
assert events[0].args == ('x', 'y')
Expand All @@ -114,9 +114,9 @@ async def dispatch_event_construct(self, source: EventSource, construct_first):
source.add_listener('event_a', events.append)
if construct_first:
event = DummyEvent(source, 'event_a', 'x', 'y', a=1, b=2)
await source.dispatch(event)
await source.dispatch_event(event)
else:
await source.dispatch('event_a', 'x', 'y', a=1, b=2)
await source.dispatch_event('event_a', 'x', 'y', a=1, b=2)

assert len(events) == 1
assert events[0].args == ('x', 'y')
Expand All @@ -128,7 +128,7 @@ async def dispatch_event_listener_arguments(self, source: EventSource):
arguments = []
source.add_listener(['event_a'], lambda *args, **kwargs: arguments.append((args, kwargs)),
[1, 2], {'x': 6, 'y': 8})
await source.dispatch('event_a')
await source.dispatch_event('event_a')

assert len(arguments) == 1
assert arguments[0][0] == (1, 2)
Expand All @@ -139,8 +139,8 @@ async def dispatch_event_multiple_topic(self, source: EventSource):
"""Test that a one add_listen() call can be made to subscribe to multiple topics."""
events = []
source.add_listener(['event_a', 'event_b'], events.append)
await source.dispatch('event_a', 'x', 'y', a=1, b=2)
await source.dispatch('event_b', 'c', 'd', g=7, h=8)
await source.dispatch_event('event_a', 'x', 'y', a=1, b=2)
await source.dispatch_event('event_b', 'c', 'd', g=7, h=8)

assert len(events) == 2
assert events[0].args == ('x', 'y')
Expand All @@ -166,7 +166,7 @@ async def async_error(event):
async_listener = source.add_listener('event_a', async_error)
event = DummyEvent(source, 'event_a')
with pytest.raises(EventDispatchError) as exc:
await source.dispatch(event)
await source.dispatch_event(event)

assert exc.value.event is event
assert exc.value.exceptions == [
Expand All @@ -181,13 +181,13 @@ async def test_remove_listener(self, source, from_handle):
"""Test that an event listener no longer receives events after it's been removed."""
events = []
handle = source.add_listener('event_a', events.append)
await source.dispatch('event_a', 1)
await source.dispatch_event('event_a', 1)
if from_handle:
handle.remove()
else:
source.remove_listener(handle)

await source.dispatch('event_a', 2)
await source.dispatch_event('event_a', 2)

assert len(events) == 1
assert events[0].args == (1,)
Expand All @@ -203,28 +203,28 @@ def test_remove_nonexistent_listener(self, source, topic):
@pytest.mark.asyncio
async def test_dispatch_nonexistent_topic(self, source):
with pytest.raises(LookupError) as exc:
await source.dispatch('blah')
await source.dispatch_event('blah')
assert str(exc.value) == 'no such topic registered: blah'

@pytest.mark.asyncio
async def test_dispatch_pointless_args(self, source):
"""Test that passing variable arguments with an Event instance raises an AssertionError."""
with pytest.raises(AssertionError) as exc:
await source.dispatch(DummyEvent(source, 'event_a'), 6)
await source.dispatch_event(DummyEvent(source, 'event_a'), 6)
assert str(exc.value) == 'passing extra arguments makes no sense here'

@pytest.mark.asyncio
async def test_dispatch_event_class_mismatch(self, source):
"""Test that passing an event of the wrong type raises an AssertionError."""
with pytest.raises(AssertionError) as exc:
await source.dispatch(Event(source, 'event_a'))
await source.dispatch_event(Event(source, 'event_a'))
assert str(exc.value) == 'event class mismatch'


@pytest.mark.asyncio
async def test_wait_event(source, event_loop):
event = DummyEvent(source, 'event_a')
event_loop.create_task(source.dispatch(event))
event_loop.create_task(source.dispatch_event(event))
received_event = await wait_event(source, 'event_a')
assert received_event is event

Expand All @@ -233,11 +233,11 @@ async def test_wait_event(source, event_loop):
async def test_stream_events(source, event_loop):
async def generate_events():
await asyncio.sleep(0.2)
await source.dispatch('event_a', 1)
await source.dispatch_event('event_a', 1)
await asyncio.sleep(0.2)
await source.dispatch('event_a', 2)
await source.dispatch_event('event_a', 2)
await asyncio.sleep(0.2)
await source.dispatch('event_a', 3)
await source.dispatch_event('event_a', 3)

event_loop.create_task(generate_events())
last_number = 0
Expand Down

0 comments on commit c831bca

Please sign in to comment.