def test_on_any_event_run(): brq = Baroque() assert len(brq.reactors.jolly_bag) == 0 r = ReactorFactory.stdout() result = brq.on_any_event_run(r) assert isinstance(result, Reactor) assert r in brq.reactors.jolly_bag
def test_fire(): # using a databox to keep state and make assertions box = Box() r1 = ReactorFactory.call_function(box, 'test_eventtype') r2 = ReactorFactory.call_function(box, 'any_eventtype') brq = Baroque() eventtype = GenericEventType() brq.on(eventtype).run(r1) brq.on_any_event_run(r2) evt = Event(eventtype) assert not box.reacted_on_test_eventtype assert not box.reacted_on_any_eventtype assert brq.events.count_all() == 0 brq.fire(evt) assert box.reacted_on_test_eventtype assert box.reacted_on_any_eventtype assert brq.events.count_all() == 1
def test_publish(): # using a databox to keep state and make assertions box = Box() r1 = ReactorFactory.call_function(box, 'test_eventtype') r2 = ReactorFactory.call_function(box, 'any_eventtype') brq = Baroque() eventtype = GenericEventType() brq.on(eventtype).run(r1) brq.on_any_event_run(r2) evt = Event(eventtype) assert not box.reacted_on_test_eventtype assert not box.reacted_on_any_eventtype assert brq.events.count_all() == 0 brq.publish(evt) assert box.reacted_on_test_eventtype assert box.reacted_on_any_eventtype assert brq.events.count_all() == 1 assert evt.status == EventStatus.PUBLISHED with pytest.raises(AssertionError): brq.publish('not-an-event') pytest.fail()
# ... or all reactors for it brq.reactors.to(eventtype).remove_all() # ... or remove reactors for any kind of event type brq.reactors.remove_all() # try again but with your custom reactors def randomize(event): from random import randint print(randint(1, 10)) my_reactor = Reactor(randomize) # run a reactor upon any incoming event brq.on_any_event_run(my_reactor) event = Event(eventtype) brq.publish(event) # view all attached reactors, or all reactors for a specific event type print(brq.reactors) print(brq.reactors.to(eventtype)) print(brq.reactors.to(eventtype).count()) print(brq.reactors.to_any_event()) print(brq.reactors.to_any_event().count()) brq.reactors.to_any_event().remove_all() brq.reactors.remove_all() # reactors can run only if specific conditions are met def condition(event):