async def test_notify_async(loop): def topic_a(a: int, b: str): pass def topic_b(x: int): pass calls = [] def observer_a1(a: int, b: str): calls.append(('a1', a, b)) def observer_a2(a: int, b: str): calls.append(('a2', a, b)) def observer_b1(x: int): calls.append(('b1', x)) def generic_1(*args, **kwargs): calls.append((('generic1', ) + args + (repr(kwargs), ))) def generic_2(*args, **kwargs): calls.append((('generic2', ) + args + (repr(kwargs), ))) notifier = Notifier(loop=loop) notifier.add_observer(topic_a, observer_a1) # add an observer notifier.add_observer( topic_a, observer_a1 ) # adding the same observer multiple times should affect nothing notifier.add_generic_observer(generic_1) # add a generic observer # An attempt to add the same observer with different `synchronous` option value should raise an error with pytest.raises(NotifierError, match=r'^Cannot register the same observer ' r'with a different value of `synchronous` option$'): notifier.add_observer(topic_a, observer_a1, synchronous=True) with pytest.raises(TypeError): notifier[topic_a](123) assert calls == [] notifier[topic_a](1, 'aaa') await asyncio.sleep(0.1) assert set(calls) == {('generic1', topic_a, 1, 'aaa', '{}'), ('a1', 1, 'aaa')} calls.clear() notifier.add_observer( topic_a, observer_a2) # add a second observer to the same topic notifier.add_observer(topic_b, observer_b1) # observer to a different topic notifier.add_generic_observer(generic_2) # a second generic observer notifier[topic_a](2, 'bbb') await asyncio.sleep(0.1) assert set(calls) == {('generic1', topic_a, 2, 'bbb', '{}'), ('generic2', topic_a, 2, 'bbb', '{}'), ('a1', 2, 'bbb'), ('a2', 2, 'bbb')} calls.clear() notifier[topic_b](x=111) await asyncio.sleep(0.1) assert set(calls) == {('generic1', topic_b, "{'x': 111}"), ('generic2', topic_b, "{'x': 111}"), ('b1', 111)} calls.clear() notifier.remove_observer(topic_b, observer_b1) notifier.remove_generic_observer(generic_1) notifier[topic_b](222) await asyncio.sleep(0.1) assert set(calls) == {('generic2', topic_b, 222, '{}')}
def test_notify(): def topic_a(a: int, b: str): pass def topic_b(x: int): pass calls = [] def observer_a1(a: int, b: str): calls.append(('a1', a, b)) def observer_a2(a: int, b: str): calls.append(('a2', a, b)) def observer_b1(x: int): calls.append(('b1', x)) def generic_1(*args, **kwargs): calls.append((('generic1', ) + args + (repr(kwargs), ))) def generic_2(*args, **kwargs): calls.append((('generic2', ) + args + (repr(kwargs), ))) notifier = Notifier() notifier.add_observer(topic_a, observer_a1) # add an observer notifier.add_observer( topic_a, observer_a1 ) # adding the same observer multiple times should affect nothing notifier.add_generic_observer(generic_1) # add a generic observer with pytest.raises(TypeError): notifier[topic_a](123) assert calls == [] notifier[topic_a](1, 'aaa') assert calls == [('generic1', topic_a, 1, 'aaa', '{}'), ('a1', 1, 'aaa')] calls.clear() notifier.add_observer( topic_a, observer_a2) # add a second observer to the same topic notifier.add_observer(topic_b, observer_b1) # observer to a different topic notifier.add_generic_observer(generic_2) # a second generic observer notifier[topic_a](2, 'bbb') assert calls == [('generic1', topic_a, 2, 'bbb', '{}'), ('generic2', topic_a, 2, 'bbb', '{}'), ('a1', 2, 'bbb'), ('a2', 2, 'bbb')] calls.clear() notifier[topic_b](x=111) assert calls == [('generic1', topic_b, "{'x': 111}"), ('generic2', topic_b, "{'x': 111}"), ('b1', 111)] calls.clear() notifier.logger.warning = MagicMock() notifier.notify_by_topic_name('non_existent_topic', x=1, y=2) notifier.logger.warning.assert_called_once_with( 'Topic with name `non_existent_topic` not found') notifier.notify_by_topic_name('topic_b', x=111) assert calls == [('generic1', topic_b, "{'x': 111}"), ('generic2', topic_b, "{'x': 111}"), ('b1', 111)] calls.clear() notifier.remove_observer(topic_b, observer_b1) notifier.remove_generic_observer(generic_1) notifier[topic_b](222) assert calls == [('generic2', topic_b, 222, '{}')]