def test_exception_bubbling(): # do propagate cfg['reactors']['propagate_exceptions'] = True brq = Baroque() brq.config = cfg def reaction_raising_error(evt): raise FileNotFoundError() r = Reactor(reaction_raising_error) brq.on(GenericEventType()).run(r) with pytest.raises(FileNotFoundError): brq.publish(Event(GenericEventType())) pytest.fail() # do not propagate cfg['reactors']['propagate_exceptions'] = False brq = Baroque() brq.config = cfg brq.on(GenericEventType()).run(r) try: brq.publish(Event(GenericEventType())) except FileNotFoundError: pytest.fail()
def test_reset(): brq = Baroque() class MyEventType1(EventType): def __init__(self, owner=None): EventType.__init__(self, '''{ "$schema": "http://json-schema.org/draft-04/schema#" }''', description='test', owner=owner) class MyEventType2(EventType): def __init__(self, owner=None): EventType.__init__(self, '''{ "$schema": "http://json-schema.org/draft-04/schema#" }''', description='test', owner=owner) eventtype1 = MyEventType1() eventtype2 = MyEventType2() brq.on(eventtype1).run(ReactorFactory.stdout()) brq.on(eventtype2).run(ReactorFactory.stdout()) brq.publish(Event(eventtype1)) brq.publish(Event(eventtype2)) assert brq.events.count_all() == 2 assert brq.eventtypes.count() == 2 + len( DEFAULT_CONFIG['eventtypes']['pre_registered']) assert len(brq.reactors.registered_types) == 2 brq.reset() assert brq.events.count_all() == 0 assert brq.eventtypes.count() == 0 assert len(brq.reactors.registered_types) == 0
def test_on(): brq = Baroque() eventtype = GenericEventType() result = brq.on(eventtype) assert isinstance(result, ReactorsBag) brq.reactors.registered_types[type(eventtype)] = ReactorsBag() result = brq.on(eventtype) assert isinstance(result, ReactorsBag)
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()
def __init__(self): self.old_value = None self.new_value = None # Instantiate objects observed = Observed('aaa') synced = Synced() # Create and bind the reactor def assign(event): synced.old_value = event.payload['meta']['old_value'] synced.new_value = event.payload['meta']['new_value'] reactor = Reactor(assign) brq.on(DataOperationEventType).run(reactor) # Change the observed instance observed.field = 'zzz' print('The observed property changed from {} to {}'.format( synced.old_value, synced.new_value)) observed.field = 123 print('The observed property changed from {} to {}'.format( synced.old_value, synced.new_value))
# instantiate the library brq = Baroque() # EventTypes mark categories of events eventtype = GenericEventType() # Events are simple objects with properties event = Event(eventtype) # Reactors are stateless functions # instantiate a predefined reactor that mirrors the triggering event on stdout reactor = ReactorFactory.stdout() # attach the reactor to any incoming event of type "eventtype" brq.on(eventtype).run(reactor) # same as: brq.on("foobar").trigger(reactor) # now fire the event brq.publish(event) # same as: brq.fire(event) # view all tracked event types print(brq.eventtypes) print(brq.eventtypes.count()) # remove a reactor for a given event type brq.reactors.to(eventtype).remove(reactor) # ... or all reactors for it brq.reactors.to(eventtype).remove_all() # ... or remove reactors for any kind of event type