async def test_subscribe_multi_times(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method1, event1) eventbus.subscribe(obj.method1, event1) await eventbus.send(event1) assert obj.count1 == 1
async def test_send_unknown_event(): eventbus = EventBus() obj = FooClass() await eventbus.send(event1) eventbus.subscribe(obj.method1, event1) await eventbus.send(event2) assert obj.count1 == 0
async def test_subscribe(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method1, event1) eventbus.subscribe(obj.method2, event2) await eventbus.send(event1) assert obj.count1 == 1 and obj.count2 == 0 await eventbus.send(event2) assert obj.count1 == 1 and obj.count2 == 1
async def test_del_weak_ref(): eventbus = EventBus() obj1 = FooClass() obj2 = FooClass() eventbus.subscribe(obj1.method1, event1) eventbus.subscribe(obj2.method1, event1) await eventbus.send(event1) assert obj1.count1 == 1 and obj2.count1 == 1 del obj2 assert len(eventbus._refs[event1]) == 2 await eventbus.send(event1) assert len(eventbus._refs[event1]) == 1 assert obj1.count1 == 2
def test_raise_value_error(): def func(): """ function """ eventbus = EventBus() with pytest.raises(ValueError): eventbus.subscribe(func, event1) with pytest.raises(ValueError): eventbus.subscribe(FooClass().staticmethod, event1) eventbus.subscribe(FooClass().method, event1)
async def test_unsubscribe(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method1, event1) await eventbus.send(event1) assert obj.count1 == 1 eventbus.unsubscribe(obj.method1, event1) await eventbus.send(event1) assert obj.count1 == 1
def __init__(self, **kwargs): self.event_bus = EventBus() self.config = Config(DEFAULT_CONFIG, **kwargs)
async def test_send_with_parameters(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method_set_value, event1) await eventbus.send(event1, value=-1) assert obj.value == -1
async def test_unsubscribe_multi_times(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method1, event1) eventbus.unsubscribe(obj.method1, event1) eventbus.unsubscribe(obj.method1, event1)
async def test_raise_error_in_callback(): eventbus = EventBus() obj = FooClass() eventbus.subscribe(obj.method_raise_error, event1) await eventbus.send(event1)