Exemplo n.º 1
0
    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()))
Exemplo n.º 2
0
    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()))
Exemplo n.º 3
0
    def test_process_invalid_data(self):
        rec = BoboReceiver(StrDictValidator(), PrimitiveEventFormatter())
        recsub = ReceiverSubscriber()
        rec.subscribe(recsub)

        data_list = [None, "abc", 123]

        rec.setup()
        for data in data_list:
            rec.add_data(data)
            rec.loop()

        self.assertEqual(len(recsub.invalid), 3)
        self.assertEqual(recsub.invalid[0], data_list[0])
        self.assertEqual(recsub.invalid[1], data_list[1])
        self.assertEqual(recsub.invalid[2], data_list[2])
Exemplo n.º 4
0
    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])
Exemplo n.º 5
0
    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_cancel(self):
        receiver = BoboReceiver(validator=StrDictValidator(),
                                formatter=PrimitiveEventFormatter())
        null_data = StubBoboNullDictData(data=DATA_A)

        nullgen = BoboNullDataGenerator(receiver=receiver,
                                        null_data=null_data,
                                        active=True)

        nullgen.setup()
        nullgen.loop()
        self.assertEqual(1, receiver._data_queue.qsize())

        nullgen.cancel()
        self.assertTrue(nullgen._is_cancelled)

        with self.assertRaises(RuntimeError):
            nullgen.loop()
Exemplo n.º 7
0
    def test_process_valid_data(self):
        rec = BoboReceiver(StrDictValidator(), PrimitiveEventFormatter())
        recsub = ReceiverSubscriber()
        rec.subscribe(recsub)

        data_list = [{}, KEY_VAL_1]

        rec.setup()
        for data in data_list:
            rec.add_data(data)
            rec.loop()

        self.assertEqual(len(recsub.events), 2)

        for event in recsub.events:
            self.assertIsInstance(event, PrimitiveEvent)

        self.assertEqual(recsub.events[0].data, data_list[0])
        self.assertEqual(recsub.events[1].data, data_list[1])
Exemplo n.º 8
0
    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_activate_deactivate(self):
        receiver = BoboReceiver(validator=StrDictValidator(),
                                formatter=PrimitiveEventFormatter())
        null_data = StubBoboNullDictData(data=DATA_A)

        nullgen = BoboNullDataGenerator(receiver=receiver,
                                        null_data=null_data,
                                        active=False)

        nullgen.setup()
        nullgen.loop()
        self.assertEqual(0, receiver._data_queue.qsize())

        nullgen.activate()
        nullgen.loop()
        self.assertEqual(1, receiver._data_queue.qsize())

        nullgen.deactivate()
        nullgen.loop()
        self.assertEqual(1, receiver._data_queue.qsize())
Exemplo n.º 10
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)
Exemplo n.º 11
0
    def test_receiver_subscribe_unsubscribe(self):
        rec = BoboReceiver(StrDictValidator(), PrimitiveEventFormatter())
        recsub = ReceiverSubscriber()

        rec.subscribe(recsub)

        # first should be passed to subscriber
        rec.add_data(KEY_VAL_1)
        rec.setup()
        rec.loop()

        self.assertEqual(len(recsub.events), 1)
        self.assertEqual(recsub.events[0].data, KEY_VAL_1)

        rec.unsubscribe(recsub)

        # second should not be passed to former subscriber
        rec.add_data(KEY_VAL_2)
        rec.loop()

        self.assertEqual(len(recsub.events), 1)
        self.assertEqual(recsub.events[0].data, KEY_VAL_1)
Exemplo n.º 12
0
    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())
Exemplo n.º 13
0
    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()))
Exemplo n.º 14
0
    def test_invalid_min_1(self):
        validator = StrDictValidator(min_length=1)

        self.assertFalse(validator.validate({KEY_A: ""}))
        self.assertFalse(validator.validate({"": VAL_A}))
Exemplo n.º 15
0
    def test_invalid_min_0(self):
        validator = StrDictValidator(min_length=0)

        self.assertFalse(validator.validate(None))
        self.assertFalse(validator.validate(""))
        self.assertFalse(validator.validate(123))
        self.assertFalse(validator.validate(123.456))
        self.assertFalse(validator.validate(True))
        self.assertFalse(validator.validate(False))
        self.assertFalse(validator.validate(BoboHistory()))
        self.assertFalse(
            validator.validate(
                PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())))

        # as a dict
        self.assertFalse(validator.validate({KEY_A: None}))
        self.assertFalse(validator.validate({KEY_A: 123}))
        self.assertFalse(validator.validate({KEY_A: 123.456}))
        self.assertFalse(validator.validate({KEY_A: True}))
        self.assertFalse(validator.validate({KEY_A: False}))
        self.assertFalse(validator.validate({KEY_A: BoboHistory()}))
        self.assertFalse(
            validator.validate({
                KEY_A:
                PrimitiveEvent(timestamp=EpochNSClock.generate_timestamp())
            }))
Exemplo n.º 16
0
    def test_valid_min_0(self):
        validator = StrDictValidator(min_length=0)

        self.assertTrue(validator.validate({}))
        self.assertTrue(validator.validate({KEY_A: VAL_A}))