def test_configure_when_no_complex_event_definitions(self): setup = BoboSetup() with self.assertRaises(RuntimeError): setup.configure() self.assertFalse(setup.is_configured())
def test_action_from_producer_to_decider_not_recursive(self): setup = BoboSetup(recursive=False) setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_4)) setup.config_receiver(StrDictValidator()) setup.configure() decider = setup.get_decider() decider.setup() producer = setup.get_producer() producer.setup() a_event = NoAction().execute( CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={})) producer.on_action_attempt(a_event) producer.loop() handler = decider.get_all_handlers()[0] self.assertEqual(0, len(handler.get_all_recent()))
def test_multi_composite_from_decider_to_producer_recursive(self): setup = BoboSetup(recursive=True, max_recent=2) nfa_names = [NAME_NFA_A, NAME_NFA_B, NAME_NFA_C] for nfa_name in nfa_names: setup.add_complex_event(event_def=BoboComplexEvent( name=nfa_name, pattern=stub_pattern_1, action=NoAction())) setup.config_receiver(StrDictValidator()) setup.configure() decider = setup.get_decider() decider.setup() handlers = decider.get_all_handlers() self.assertEqual(3, len(handlers)) producer = setup.get_producer() producer.setup() decider.on_receiver_event(event_a) for _ in range(len(nfa_names)): decider.loop() producer.loop() for handler in handlers: self.assertEqual(2, len(handler.get_all_recent()))
def test_start_when_active(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.start() sleep(SLEEP_WAIT) with self.assertRaises(RuntimeError): setup.start() setup.cancel()
def test_configure_when_cancelled(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.cancel() with self.assertRaises(RuntimeError): setup.configure() self.assertFalse(setup.is_configured())
def test_get_distributed_configured_but_not_distributed(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.configure() with self.assertRaises(RuntimeError): setup.get_distributed()
def test_primitive_from_receiver_to_decider(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_4)) setup.config_receiver(StrDictValidator()) setup.configure() receiver = setup.get_receiver() receiver.setup() decider = setup.get_decider() decider.setup() receiver.add_data(DATA_DICT_A) receiver.loop() decider.loop() handlers = decider.get_all_handlers() self.assertEqual(NAME_NFA_A, handlers[0].nfa.name)
def test_composite_from_producer_to_forwarder(self): setup = BoboSetup(recursive=True) setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_4)) setup.config_receiver(StrDictValidator()) setup.configure() producer = setup.get_producer() producer.setup() forwarder = setup.get_forwarder() forwarder.setup() c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={}) producer.on_decider_complex_event(c_event) producer.loop() self.assertEqual(c_event, forwarder._event_queue.get_nowait())
def test_composite_from_decider_to_producer_not_recursive(self): setup = BoboSetup(recursive=False) setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_4)) setup.config_receiver(StrDictValidator()) setup.configure() decider = setup.get_decider() decider.setup() producer = setup.get_producer() producer.setup() for event in [event_a, event_b, event_c, event_d]: decider.on_receiver_event(event) decider.loop() self.assertFalse(producer._event_queue.empty()) producer.loop() handler = decider.get_all_handlers()[0] self.assertEqual(0, len(handler.get_all_recent()))
def test_subscribe_decider_composite(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.subscribe_decider(NAME_NFA_A, sub) setup.configure() decider = setup.get_decider() decider.setup() c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={}) decider.on_handler_final(NAME_NFA_A, RUN_ID_A, c_event) self.assertEqual(c_event, sub.decider_complex_event[0])
def test_subscribe_forward_composite_success(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.subscribe_forwarder(sub) setup.configure() forwarder = setup.get_forwarder() forwarder.setup() c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={}) forwarder.on_accepted_producer_event(c_event) forwarder.loop() self.assertEqual(c_event, sub.forwarder_success_event[0])
def test_subscribe_producer_action(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.subscribe_producer(NAME_NFA_A, sub) setup.configure() producer = setup.get_producer() producer.setup() a_event = NoAction().execute( CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={})) producer.on_action_attempt(a_event) producer.loop() self.assertEqual(a_event, sub.producer_action[0])
def test_config_arguments(self): setup = BoboSetup() event_def = BoboComplexEvent(NAME_NFA_A, stub_pattern_1) validator = StrDictValidator() action_producer = RateLimitAction() action_forwarder = NoAction() null_data = BoboNullDataStatic(DATA_DICT_A) setup.add_complex_event(event_def) setup.config_receiver(validator) setup.config_producer(action_producer) setup.config_forwarder(action_forwarder) setup.config_null_data(NULL_DATA_DELAY, null_data) setup.config_distributed(EXCHANGE_NAME, USER_NAME, parameters) setup.configure() receiver = setup.get_receiver() decider = setup.get_decider() producer = setup.get_producer() forwarder = setup.get_forwarder() nullgen = setup.get_null_data_generator() manager = setup.get_distributed() self.assertEqual(validator, receiver.get_validator()) self.assertEqual(NAME_NFA_A, decider.get_all_handlers()[0].nfa.name) self.assertEqual(action_producer, producer._action) self.assertEqual(action_forwarder, forwarder._action) self.assertEqual(null_data, nullgen.null_data) self.assertEqual(receiver, nullgen.receiver) self.assertEqual(manager.outgoing.decider, decider) self.assertEqual(manager.outgoing.exchange_name, EXCHANGE_NAME) self.assertTrue(manager.outgoing.user_id.find(USER_NAME) != -1) self.assertEqual(manager.outgoing.parameters, parameters) self.assertEqual(manager.incoming.decider, decider) self.assertEqual(manager.incoming.exchange_name, EXCHANGE_NAME) self.assertTrue(manager.incoming.user_id.find(USER_NAME) != -1) self.assertEqual(manager.incoming.parameters, parameters)
def test_on_sync(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_4, action=NoAction())) setup.config_null_data(delay_sec=NULL_DATA_DELAY, null_data=BoboNullDataStatic(DATA_DICT_A)) setup.config_distributed(exchange_name=EXCHANGE_NAME, user_name=USER_NAME, parameters=parameters) setup.configure() self.assertFalse(setup.get_receiver().is_active()) self.assertFalse(setup.get_decider().is_active()) self.assertFalse(setup.get_producer().is_active()) self.assertFalse(setup.get_forwarder().is_active()) self.assertFalse(setup.get_null_data_generator().is_active()) setup.on_sync() self.assertTrue(setup.get_receiver().is_active()) self.assertTrue(setup.get_decider().is_active()) self.assertTrue(setup.get_producer().is_active()) self.assertTrue(setup.get_forwarder().is_active()) self.assertTrue(setup.get_null_data_generator().is_active())
def test_access_before_configuration(self): setup = BoboSetup() with self.assertRaises(RuntimeError): setup.get_receiver() with self.assertRaises(RuntimeError): setup.get_decider() with self.assertRaises(RuntimeError): setup.get_producer() with self.assertRaises(RuntimeError): setup.get_forwarder() with self.assertRaises(RuntimeError): setup.get_distributed() with self.assertRaises(RuntimeError): setup.get_null_data_generator()
def test_subscribe_forward_action_failure(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) # this rejects all events passed to Forwarder setup.config_forwarder(NoAction(bool_return=False)) setup.subscribe_forwarder(sub) setup.configure() forwarder = setup.get_forwarder() forwarder.setup() a_event = NoAction().execute( CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={})) forwarder.on_producer_action(a_event) forwarder.loop() self.assertEqual(a_event, sub.forwarder_failure_event[0])
def test_setup_after_cancel(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.config_null_data(delay_sec=NULL_DATA_DELAY, null_data=BoboNullDataStatic(DATA_DICT_A)) setup.configure() setup.start() sleep(SLEEP_WAIT) setup.cancel() self.assertFalse(setup.is_ready()) self.assertFalse(setup.is_active()) self.assertFalse(setup.is_inactive()) self.assertTrue(setup.is_cancelled()) self.assertTrue(setup.is_configured()) self.assertFalse(setup.get_receiver().is_active()) self.assertFalse(setup.get_decider().is_active()) self.assertFalse(setup.get_producer().is_active()) self.assertFalse(setup.get_forwarder().is_active()) self.assertFalse(setup.get_null_data_generator().is_active())
def test_subscribe_receiver_valid_data(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.config_receiver(StrDictValidator()) setup.subscribe_receiver(sub) setup.configure() receiver = setup.get_receiver() receiver.setup() receiver.add_data(DATA_DICT_A) receiver.loop() self.assertIsInstance(sub.receiver_event[0], PrimitiveEvent)
def test_subscribe_receiver_invalid_data(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.config_receiver(StrDictValidator()) setup.subscribe_receiver(sub) setup.configure() receiver = setup.get_receiver() receiver.setup() receiver.add_data(VALUE_A) receiver.loop() self.assertEqual(VALUE_A, sub.invalid_data[0])
def test_subscribe_producer_composite_rejected(self): setup = BoboSetup() sub = StubSubscriberSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) # this rejects all events passed to Producer setup.config_producer(NoAction(bool_return=False)) setup.subscribe_producer(NAME_NFA_A, sub) setup.configure() producer = setup.get_producer() producer.setup() c_event = CompositeEvent(timestamp=EpochNSClock.generate_timestamp(), name=NAME_NFA_A, history=BoboHistory(), data={}) producer.on_decider_complex_event(c_event) producer.loop() self.assertEqual(c_event, sub.rejected_producer_event[0])
def test_setup_before_configure(self): setup = BoboSetup() setup.add_complex_event(event_def=BoboComplexEvent( name=NAME_NFA_A, pattern=stub_pattern_1, action=NoAction())) setup.config_null_data(delay_sec=NULL_DATA_DELAY, null_data=BoboNullDataStatic(DATA_DICT_A)) self.assertFalse(setup.is_ready()) self.assertFalse(setup.is_active()) self.assertTrue(setup.is_inactive()) self.assertFalse(setup.is_cancelled()) self.assertFalse(setup.is_configured())