def test_dispatcher_topic_doesnt_contain_hook(capsys): disp = EventDispatcher() evt = Event(name='emit signal', context='test') evt.enabled = True evt.topics.append({}) disp.dispatch(evt, 'hook', 1234) captured = capsys.readouterr() assert captured.out == ""
def test_dispatcher_single_topic(capsys): disp = EventDispatcher() evt = Event(name='emit signal', context='test') evt.enabled = True def handler(context, data): print(context.event.metadata, context.hook, data) evt.topics.append({'hook': [handler]}) disp.dispatch(evt, 'hook', 1234) captured = capsys.readouterr() assert captured.out == """\
def test_call_event_remove_dispatcher_while_processed(env, capsys): evt = Event(name='cross red light', context='test') evt.dispatcher = EventDispatcher() evt.enabled = True def before(context, data): print(context.hook, context.event.metadata, data.value) evt.dispatcher = None def callbacks(context, data): print(context.hook, context.event.metadata, data.value) def after(context, data): print(context.hook, context.event.metadata, data.value) topic = { 'before': [before, before], 'callbacks': [callbacks], 'after': [after], } evt.topics.append(topic) evt(env.timeout(1, 'main street')) env.run() captured = capsys.readouterr() assert captured.out == """\
def test_call_event_remove_dispatcher_before_processed(env, capsys): evt = Event(name='cross red light', context='test') evt.dispatcher = EventDispatcher() evt.enabled = True def handler(context, data): print(context.hook, context.event.metadata, data.value) topic = { 'before': [handler], 'callbacks': [handler], 'after': [handler], } evt.topics.append(topic) evt(env.timeout(2, 'main street')) def process(): yield env.timeout(1) evt.dispatcher = None env.process(process()) env.run() captured = capsys.readouterr() assert captured.out == ""
def test_call_event_reuse_simpyevent_preserve_order(env, capsys): evt = Event(name='cross red light', context='test') evt.enabled = True evt2 = Event(name='caught on camera', context='test') evt2.enabled = True dispatcher = EventDispatcher() evt.dispatcher = dispatcher evt2.dispatcher = dispatcher def handler(context, data): print(context.hook, context.event.metadata, data.value) evt.topics.append({ 'before': [handler], 'callbacks': [handler], 'after': [handler], }) evt2.topics.append({ 'before': [handler], 'callbacks': [handler], 'after': [handler], }) event = evt(env.timeout(1, 'main street')) evt2(event) env.run() captured = capsys.readouterr() assert captured.out == """\
def test_call_event_dispatcher_no_topic(env, capsys): evt = Event(name='cross red light', context='test') evt.enabled = True evt.dispatcher = EventDispatcher() event = env.timeout(1, 'main street') assert evt(event) is event env.run() captured = capsys.readouterr() assert captured.out == ""
def test_event_dispatch_default_data(capsys): evt = Event(name='cross red light', context='test') evt.dispatcher = EventDispatcher() evt.enabled = True def handler(context, data): print(context.hook, context.event.metadata, data) evt.topics.append({ 'init': [handler], }) evt.dispatch(hook='init') captured = capsys.readouterr() assert captured.out == """\
def test_event_disable(env, capsys): evt = Event(name='cross red light', context='test') evt.dispatcher = EventDispatcher() def handler(context, data): print(context.hook, context.event.metadata, data.value) evt.topics.append({ 'before': [handler], 'callbacks': [handler], 'after': [handler], }) evt(env.timeout(1, 'main street')) env.run() captured = capsys.readouterr() assert captured.out == ""
def test_event_dispatch_enable(capsys): evt = Event(name='cross red light', context='test') evt.dispatcher = EventDispatcher() def handler(context, data): print(context.hook, context.event.metadata, data) evt.topics.append({ 'enable': [handler], 'init': [handler], }) evt.enabled = True evt.enabled = True # check does not dispatch if no change evt.dispatch(hook='init', data='handle me') captured = capsys.readouterr() assert captured.out == """\
def test_dispatcher_alter_event_topic_content_while_dispatch(capsys): disp = EventDispatcher() evt = Event(name='emit signal', context='test') evt.enabled = True def handler(context, data): print(context.event.metadata, context.hook, data) t['hook'].clear() evt.topics.append({'hook': [handler]}) t = {'hook': [handler]} evt.topics.append(t) disp.dispatch(evt, 'hook', 1234) print('#2') disp.dispatch(evt, 'hook', 1234) captured = capsys.readouterr() assert captured.out == """\