def test_not_matching_process(self): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(5) no_matching_calls = [] def no_matching_action(event): no_matching_calls.append(event) no_match_recording_filter = RecordingFalseFilter() ep.add_filter(no_match_recording_filter) ep.add_handler(no_matching_action, wait=True) event1 = Event(EventId(uuid.uuid4(), 0)) event2 = Event(EventId(uuid.uuid4(), 1)) event3 = Event(EventId(uuid.uuid4(), 2)) ep.handle(event1) ep.handle(event2) ep.handle(event3) # no Match listener must not have been called with no_match_recording_filter.condition: while len(no_match_recording_filter.events) < 3: no_match_recording_filter.condition.wait() assert len(no_match_recording_filter.events) == 3 assert event1 in no_match_recording_filter.events assert event2 in no_match_recording_filter.events assert event3 in no_match_recording_filter.events assert len(no_matching_calls) == 0 ep.remove_filter(no_match_recording_filter)
def testNotMatchingProcess(self): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(5) noMatchingCalls = [] def noMatchingAction(event): noMatchingCalls.append(event) noMatchRecordingFilter = RecordingFalseFilter() ep.addFilter(noMatchRecordingFilter) ep.addHandler(noMatchingAction, wait=True) event1 = Event(EventId(uuid.uuid4(), 0)) event2 = Event(EventId(uuid.uuid4(), 1)) event3 = Event(EventId(uuid.uuid4(), 2)) ep.handle(event1) ep.handle(event2) ep.handle(event3) # no Match listener must not have been called with noMatchRecordingFilter.condition: while len(noMatchRecordingFilter.events) < 3: noMatchRecordingFilter.condition.wait() self.assertEqual(3, len(noMatchRecordingFilter.events)) self.assertTrue(event1 in noMatchRecordingFilter.events) self.assertTrue(event2 in noMatchRecordingFilter.events) self.assertTrue(event3 in noMatchRecordingFilter.events) self.assertEqual(0, len(noMatchingCalls)) ep.removeFilter(noMatchRecordingFilter)
def testSendEventWrongScope(self): # Error: unrelated scope e = Event(scope=Scope("/blubb"), data='foo', type=self.informer.type) self.assertRaises(ValueError, self.informer.publishEvent, e) # OK: identical scope e = Event(scope=self.defaultScope, data='foo', type=self.informer.type) self.informer.publishEvent(e) # OK: sub-scope e = Event(scope=self.defaultScope.concat(Scope('/sub')), data='foo', type=self.informer.type) self.informer.publishEvent(e)
def testSequencing(self): goodScope = Scope("/good") inConnector = getConnector(goodScope, clazz=rsbspread.InPushConnector) outConnector = getConnector(goodScope, clazz=rsbspread.OutConnector) try: receiver = SettingReceiver(goodScope) inConnector.setObserverAction(receiver) # first an event that we do not want event = Event(EventId(uuid.uuid4(), 0)) event.scope = Scope("/notGood") event.data = "".join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(300502)) event.type = str event.metaData.senderId = uuid.uuid4() outConnector.handle(event) # and then a desired event event.scope = goodScope outConnector.handle(event) with receiver.resultCondition: receiver.resultCondition.wait(10) if receiver.resultEvent is None: self.fail("Did not receive an event") # self.assertEqual(receiver.resultEvent, event) finally: inConnector.deactivate() outConnector.deactivate()
def test_sequencing(self): good_scope = Scope("/good") in_connector = get_connector(good_scope, clazz=spread_transport.InConnector) out_connector = get_connector(good_scope, clazz=spread_transport.OutConnector) try: receiver = SettingReceiver(good_scope) in_connector.set_observer_action(receiver) # first an event that we do not want event = Event(EventId(uuid.uuid4(), 0)) event.scope = Scope("/notGood") event.data = ''.join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(300502)) event.data_type = str event.meta_data.sender_id = uuid.uuid4() out_connector.handle(event) # and then a desired event event.scope = good_scope out_connector.handle(event) with receiver.result_condition: receiver.result_condition.wait(10) assert receiver.result_event is not None # self.assertEqual(receiver.result_event, event) finally: in_connector.deactivate() out_connector.deactivate()
def testPullRoundtrip(self): goodScope = Scope("/good") try: inconnector = self._getInPullConnector(goodScope) except NotImplementedError: return outconnector = self._getOutConnector(goodScope) # first an event that we do not want event = Event(EventId(uuid.uuid4(), 0)) event.scope = Scope("/notGood") event.data = "dummy data" event.type = str event.metaData.senderId = uuid.uuid4() outconnector.handle(event) # and then a desired event event.scope = goodScope outconnector.handle(event) received = inconnector.raiseEvent(True) # ignore meta data here event.setMetaData(None) received.setMetaData(None) self.assertEqual(received, event) inconnector.deactivate() outconnector.deactivate()
def test_filtering(self): strategy = FullyParallelEventReceivingStrategy() false_filter = RecordingFalseFilter() strategy.add_filter(false_filter) handler = self.CollectingHandler() strategy.add_handler(handler, True) event = Event(event_id=42) strategy.handle(event) with false_filter.condition: while len(false_filter.events) == 0: false_filter.condition.wait(timeout=5) if len(false_filter.events) == 0: self.fail("Filter not called") time.sleep(1) with handler.condition: assert handler.event is None strategy.remove_filter(false_filter)
def test_roundtrip(self): good_scope = Scope("/good") inconnector = self._get_in_connector(good_scope) outconnector = self._get_out_connector(good_scope) receiver = SettingReceiver(good_scope) inconnector.set_observer_action(receiver) # first an event that we do not want event = Event(EventId(uuid.uuid4(), 0)) event.scope = Scope("/notGood") event.data = "x" * 600000 event.data_type = str event.meta_data.sender_id = uuid.uuid4() outconnector.handle(event) # and then a desired event event.scope = good_scope outconnector.handle(event) with receiver.result_condition: while receiver.result_event is None: receiver.result_condition.wait(10) assert receiver.result_event # ignore meta data here event.meta_data = None receiver.result_event.meta_data = None assert receiver.result_event == event outconnector.deactivate() inconnector.deactivate()
def testRoundtrip(self): goodScope = Scope("/good") inconnector = self._getInPushConnector(goodScope) outconnector = self._getOutConnector(goodScope) receiver = SettingReceiver(goodScope) inconnector.setObserverAction(receiver) # first an event that we do not want event = Event(EventId(uuid.uuid4(), 0)) event.scope = Scope("/notGood") event.data = "dummy data" event.type = str event.metaData.senderId = uuid.uuid4() outconnector.handle(event) # and then a desired event event.scope = goodScope outconnector.handle(event) with receiver.resultCondition: while receiver.resultEvent is None: receiver.resultCondition.wait(10) self.assertTrue(receiver.resultEvent) # ignore meta data here event.setMetaData(None) receiver.resultEvent.setMetaData(None) self.assertEqual(receiver.resultEvent, event) inconnector.deactivate() outconnector.deactivate()
def test_comparison(self): sid = uuid.uuid4() e1 = Event(EventId(sid, 0)) e2 = Event(EventId(sid, 0)) e2.meta_data.set_create_time(e1.meta_data.create_time) e1.meta_data.set_user_time("foo") assert e1 != e2 e2.meta_data.set_user_time("foo", e1.meta_data.user_times["foo"]) assert e1 == e2 cause = EventId(uuid4(), 42) e1.add_cause(cause) assert e1 != e2 e2.add_cause(cause) assert e1 == e2
def testComparison(self): sid = uuid.uuid4() e1 = Event(EventId(sid, 0)) e2 = Event(EventId(sid, 0)) e2.getMetaData().setCreateTime(e1.getMetaData().getCreateTime()) e1.metaData.setUserTime("foo") self.assertNotEquals(e1, e2) e2.metaData.setUserTime("foo", e1.getMetaData().getUserTimes()["foo"]) self.assertEquals(e1, e2) cause = EventId(uuid4(), 42) e1.addCause(cause) self.assertNotEqual(e1, e2) e2.addCause(cause) self.assertEqual(e1, e2)
def testAddRemove(self): for size in xrange(2, 10): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(size) h1 = lambda e: e h2 = lambda e: e ep.addHandler(h1, wait=True) ep.addHandler(h2, wait=True) ep.addHandler(h1, wait=True) ep.handle(Event(EventId(uuid.uuid4(), 0))) ep.handle(Event(EventId(uuid.uuid4(), 1))) ep.handle(Event(EventId(uuid.uuid4(), 2))) ep.removeHandler(h1, wait=True) ep.removeHandler(h2, wait=True) ep.removeHandler(h1, wait=True)
def test_user_roundtrip(self): scope = Scope("/test/it") in_connector = self._get_in_connector(scope, activate=False) out_connector = self._get_out_connector(scope, activate=False) in_configurator = rsb.eventprocessing.InRouteConfigurator( connectors=[in_connector]) out_configurator = rsb.eventprocessing.OutRouteConfigurator( connectors=[out_connector]) listener = create_listener(scope, configurator=in_configurator) publisher = create_informer(scope, data_type=str, configurator=out_configurator) receiver = SettingReceiver(scope) listener.add_handler(receiver) data1 = "a string to test" sent_event = Event(EventId(uuid.uuid4(), 0)) sent_event.data = data1 sent_event.data_type = str sent_event.scope = scope sent_event.meta_data.set_user_info("test", "it") sent_event.meta_data.set_user_info("test again", "it works?") sent_event.meta_data.set_user_time("blubb", 234234.0) sent_event.meta_data.set_user_time("bla", 3434343.45) sent_event.add_cause(EventId(uuid.uuid4(), 1323)) sent_event.add_cause(EventId(uuid.uuid4(), 42)) publisher.publish_event(sent_event) with receiver.result_condition: while receiver.result_event is None: receiver.result_condition.wait(10) if receiver.result_event is None: self.fail("Listener did not receive an event") assert receiver.result_event.meta_data.create_time <= \ receiver.result_event.meta_data.send_time assert receiver.result_event.meta_data.send_time <= \ receiver.result_event.meta_data.receive_time assert receiver.result_event.meta_data.receive_time <= \ receiver.result_event.meta_data.deliver_time sent_event.meta_data.receive_time = \ receiver.result_event.meta_data.receive_time sent_event.meta_data.deliver_time = \ receiver.result_event.meta_data.deliver_time # HACK: floating point precision leads to an imprecision here, # avoid this. sent_event.meta_data.send_time = \ receiver.result_event.meta_data.send_time sent_event.meta_data.create_time = \ receiver.result_event.meta_data.create_time assert sent_event == receiver.result_event publisher.deactivate() listener.deactivate()
def test_parallel_call_of_one_handler(self): class Counter: def __init__(self): self.value = 0 max_parallel_calls = Counter() current_calls = [] call_lock = Condition() class Receiver: def __init__(self, counter): self.counter = counter def __call__(self, message): with call_lock: current_calls.append(message) self.counter.value = max(self.counter.value, len(current_calls)) call_lock.notifyAll() time.sleep(2) with call_lock: current_calls.remove(message) call_lock.notifyAll() strategy = FullyParallelEventReceivingStrategy() strategy.add_handler(Receiver(max_parallel_calls), True) event = Event(event_id=42) strategy.handle(event) event = Event(event_id=43) strategy.handle(event) event = Event(event_id=44) strategy.handle(event) num_called = 0 with call_lock: while max_parallel_calls.value < 3 and num_called < 5: num_called = num_called + 1 call_lock.wait() if num_called == 5: self.fail("Impossible to be called in parallel again") else: assert max_parallel_calls.value == 3
def testSendEventWrongType(self): # Wrong type e = Event(scope=self.defaultScope, data=5) self.assertRaises(ValueError, self.informer.publishEvent, e) # Wrong type self.assertRaises(ValueError, self.informer.publishData, 5.0) # OK self.informer.publishData('bla')
def test_send_event_wrong_scope(self): # Error: unrelated scope e = Event(scope=Scope("/blubb"), data='foo', data_type=self.informer.data_type) with pytest.raises(ValueError): self.informer.publish_event(e) # OK: identical scope e = Event(scope=self.default_scope, data='foo', data_type=self.informer.data_type) self.informer.publish_event(e) # OK: sub-scope e = Event(scope=self.default_scope.concat(Scope('/sub')), data='foo', data_type=self.informer.data_type) self.informer.publish_event(e)
def testRoundtrip(self): self.maxDiff = None data = {} scope1 = Scope("/a/test") event1 = Event(id=EventId(uuid4(), 32), scope=scope1, method="foo", data=42, type=int, userTimes={"foo": 1231234.0}) event1.metaData.setSendTime() event1.metaData.setReceiveTime() event2 = Event(id=EventId(uuid4(), 1001), scope=scope1, method="fooasdas", data=422, type=int, userTimes={"bar": 1234.05}) event2.metaData.setSendTime() event2.metaData.setReceiveTime() data[scope1] = [event1, event2] converter = EventsByScopeMapConverter() roundtripped = converter.deserialize(*converter.serialize(data)) self.assertEqual(1, len(roundtripped)) self.assertTrue(scope1 in roundtripped) self.assertEqual(len(data[scope1]), len(roundtripped[scope1])) for orig, converted in zip(data[scope1], roundtripped[scope1]): self.assertEqual(orig.id, converted.id) self.assertEqual(orig.scope, converted.scope) # This test currently does not work correctly without a patch for # the converter selection for fundamental types # self.assertEqual(orig.type, converted.type) self.assertEqual(orig.data, converted.data) self.assertAlmostEqual(orig.metaData.createTime, converted.metaData.createTime) self.assertEqual(orig.causes, converted.causes)
def test_roundtrip(self): self.max_diff = None data = {} scope1 = Scope("/a/test") event1 = Event(event_id=EventId(uuid4(), 32), scope=scope1, method="foo", data=42, data_type=int, user_times={"foo": 1231234.0}) event1.meta_data.set_send_time() event1.meta_data.set_receive_time() event2 = Event(event_id=EventId(uuid4(), 1001), scope=scope1, method="fooasdas", data=422, data_type=int, user_times={"bar": 1234.05}) event2.meta_data.set_send_time() event2.meta_data.set_receive_time() data[scope1] = [event1, event2] converter = EventsByScopeMapConverter() roundtripped = converter.deserialize(*converter.serialize(data)) assert len(roundtripped) == 1 assert scope1 in roundtripped assert len(data[scope1]) == len(roundtripped[scope1]) for orig, converted in zip(data[scope1], roundtripped[scope1]): assert orig.event_id == converted.event_id assert orig.scope == converted.scope # This test currently does not work correctly without a patch for # the converter selection for fundamental types # self.assertEqual(orig.data_type, converted.data_type) assert orig.data == converted.data assert pytest.approx(orig.meta_data.create_time) == \ converted.meta_data.create_time assert pytest.approx(orig.causes) == converted.causes
def test_add_remove(self): for size in range(2, 10): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(size) def h1(e): return e def h2(e): return e ep.add_handler(h1, wait=True) ep.add_handler(h2, wait=True) ep.add_handler(h1, wait=True) ep.handle(Event(EventId(uuid.uuid4(), 0))) ep.handle(Event(EventId(uuid.uuid4(), 1))) ep.handle(Event(EventId(uuid.uuid4(), 2))) ep.remove_handler(h1, wait=True) ep.remove_handler(h2, wait=True) ep.remove_handler(h1, wait=True)
def test_send_event_wrong_type(self): # Wrong type e = Event(scope=self.default_scope, data=5) with pytest.raises(ValueError): self.informer.publish_event(e) # Wrong type with pytest.raises(ValueError): self.informer.publish_data(5.0) # OK self.informer.publish_data('bla')
def publish_cache(self): self.__logger.debug("[{}] Publishing cache... ({} total)".format( self.__authority, len(self.__send_cache_dynamic.keys()) + len(self.__send_cache_static.keys()))) for _, v in self.__send_cache_dynamic.iteritems(): event = Event() event.setData(v[0]) event.setType(type(v[0])) event.setScope(self.__rsb_informer_transform.getScope().concat( Scope(self.__scope_suffix_dynamic))) event.setMetaData(v[1]) self.__rsb_informer_transform.publishEvent(event) for _, v in self.__send_cache_static.iteritems(): event = Event() event.setData(v[0]) event.setType(type(v[0])) event.setScope(self.__rsb_informer_transform.getScope().concat( Scope(self.__scope_suffix_static))) event.setMetaData(v[1]) self.__rsb_informer_transform.publishEvent(event)
def testCauses(self): sid = uuid.uuid4() e = Event(EventId(sid, 32)) self.assertEqual(0, len(e.causes)) cause = EventId(uuid4(), 546345) e.addCause(cause) self.assertEqual(1, len(e.causes)) self.assertTrue(e.isCause(cause)) self.assertTrue(cause in e.causes) e.removeCause(cause) self.assertFalse(e.isCause(cause)) self.assertEqual(0, len(e.causes))
def test_causes(self): sid = uuid.uuid4() e = Event(EventId(sid, 32)) assert len(e.causes) == 0 cause = EventId(uuid4(), 546345) e.add_cause(cause) assert len(e.causes) == 1 assert e.is_cause(cause) assert cause in e.causes e.remove_cause(cause) assert not e.is_cause(cause) assert len(e.causes) == 0
def testUserRoundtrip(self): scope = Scope("/test/it") inConnector = self._getInPushConnector(scope, activate=False) outConnector = self._getOutConnector(scope, activate=False) outConfigurator = rsb.eventprocessing.OutRouteConfigurator( connectors=[outConnector]) inConfigurator = rsb.eventprocessing.InPushRouteConfigurator( connectors=[inConnector]) publisher = createInformer(scope, dataType=str, configurator=outConfigurator) listener = createListener(scope, configurator=inConfigurator) receiver = SettingReceiver(scope) listener.addHandler(receiver) data1 = "a string to test" sentEvent = Event(EventId(uuid.uuid4(), 0)) sentEvent.setData(data1) sentEvent.setType(str) sentEvent.setScope(scope) sentEvent.getMetaData().setUserInfo("test", "it") sentEvent.getMetaData().setUserInfo("test again", "it works?") sentEvent.getMetaData().setUserTime("blubb", 234234) sentEvent.getMetaData().setUserTime("bla", 3434343.45) sentEvent.addCause(EventId(uuid.uuid4(), 1323)) sentEvent.addCause(EventId(uuid.uuid4(), 42)) publisher.publishEvent(sentEvent) with receiver.resultCondition: while receiver.resultEvent is None: receiver.resultCondition.wait(10) if receiver.resultEvent is None: self.fail("Listener did not receive an event") self.assertTrue( receiver.resultEvent.metaData.createTime <= receiver. resultEvent.metaData.sendTime <= receiver.resultEvent.metaData. receiveTime <= receiver.resultEvent.metaData.deliverTime) sentEvent.metaData.receiveTime = \ receiver.resultEvent.metaData.receiveTime sentEvent.metaData.deliverTime = \ receiver.resultEvent.metaData.deliverTime self.assertEqual(sentEvent, receiver.resultEvent) listener.deactivate() publisher.deactivate()
def test_pass_to_action(self): scope = Scope("/lets/go") bus = Bus() connector = InConnector(bus=bus) connector.scope = scope connector.activate() action = StubSink(scope) connector.set_observer_action(action) e = Event() e.scope = scope bus.handle(e) assert len(action.events) == 1 assert e in action.events
def testPassToAction(self): scope = Scope("/lets/go") bus = Bus() connector = InPushConnector(bus=bus) connector.setScope(scope) connector.activate() action = StubSink(scope) connector.setObserverAction(action) e = Event() e.scope = scope bus.handle(e) self.assertEqual(1, len(action.events)) self.assertTrue(e in action.events)
def testHandle(self): bus = Bus() connector = OutConnector(bus=bus) scope = Scope("/a/test") sink = StubSink(scope) bus.addSink(sink) e = Event() e.scope = scope before = time.time() connector.handle(e) after = time.time() self.assertEqual(1, len(sink.events)) self.assertTrue(e in sink.events) self.assertTrue(e.metaData.sendTime >= before) self.assertTrue(e.metaData.sendTime <= after)
def test_handle(self): bus = Bus() connector = OutConnector(bus=bus) scope = Scope("/a/test") sink = StubSink(scope) bus.add_sink(sink) e = Event() e.scope = scope before = time.time() connector.handle(e) after = time.time() assert len(sink.events) == 1 assert e in sink.events assert e.meta_data.send_time >= before assert e.meta_data.send_time <= after
def testUserPullRoundtrip(self): scope = Scope("/test/it/pull") try: inConnector = self._getInPullConnector(scope, activate=False) except NotImplementedError: return outConnector = self._getOutConnector(scope, activate=False) outConfigurator = rsb.eventprocessing.OutRouteConfigurator( connectors=[outConnector]) inConfigurator = rsb.eventprocessing.InPullRouteConfigurator( connectors=[inConnector]) publisher = createInformer(scope, dataType=str, configurator=outConfigurator) reader = createReader(scope, configurator=inConfigurator) data1 = "a string to test" sentEvent = Event(EventId(uuid.uuid4(), 0)) sentEvent.setData(data1) sentEvent.setType(str) sentEvent.setScope(scope) sentEvent.getMetaData().setUserInfo("test", "it") sentEvent.getMetaData().setUserInfo("test again", "it works?") sentEvent.getMetaData().setUserTime("blubb", 234234) sentEvent.getMetaData().setUserTime("bla", 3434343.45) sentEvent.addCause(EventId(uuid.uuid4(), 1323)) sentEvent.addCause(EventId(uuid.uuid4(), 42)) publisher.publishEvent(sentEvent) resultEvent = reader.read(True) self.assertTrue(resultEvent.metaData.createTime <= resultEvent. metaData.sendTime <= resultEvent.metaData.receiveTime <= resultEvent.metaData.deliverTime) sentEvent.metaData.receiveTime = resultEvent.metaData.receiveTime sentEvent.metaData.deliverTime = resultEvent.metaData.deliverTime self.assertEqual(sentEvent, resultEvent) reader.deactivate() publisher.deactivate()
def test_send_time_adaption(self): scope = Scope("/notGood") connector = self._get_out_connector(scope) event = Event(EventId(uuid.uuid4(), 0)) event.scope = scope event.data = "".join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in list(range(300502))) event.data_type = str event.meta_data.sender_id = uuid.uuid4() before = time.time() connector.handle(event) after = time.time() assert event.meta_data.send_time >= before assert event.meta_data.send_time <= after connector.deactivate()