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 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_hashing(self): id1 = EventId(uuid.uuid4(), 23) id2 = EventId(id1.participant_id, 23) id3 = EventId(uuid.uuid4(), 32) id4 = EventId(id3.participant_id, 33) assert hash(id1) == hash(id2) assert hash(id1) != hash(id3) assert hash(id1) != hash(id4) assert hash(id3) != hash(id4)
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 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 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 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 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 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 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 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_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 testHashing(self): id1 = EventId(uuid.uuid4(), 23) id2 = EventId(id1.getParticipantId(), 23) id3 = EventId(uuid.uuid4(), 32) id4 = EventId(id3.getParticipantId(), 33) self.assertEqual(hash(id1), hash(id2)) self.assertNotEqual(hash(id1), hash(id3)) self.assertNotEqual(hash(id1), hash(id4)) self.assertNotEqual(hash(id3), hash(id4))
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 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 testSendTimeAdaption(self): scope = Scope("/notGood") connector = self._getOutConnector(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 range(300502)) event.type = str event.metaData.senderId = uuid.uuid4() before = time.time() connector.handle(event) after = time.time() self.assertTrue(event.getMetaData().getSendTime() >= before) self.assertTrue(event.getMetaData().getSendTime() <= after) connector.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()
def test_get_as_uuid(self): id1 = EventId(uuid.uuid4(), 23) id2 = EventId(id1.participant_id, 23) id3 = EventId(id1.participant_id, 24) id4 = EventId(uuid.uuid4(), 24) assert id1.get_as_uuid() == id2.get_as_uuid() assert id1.get_as_uuid() != id3.get_as_uuid() assert id1.get_as_uuid() != id4.get_as_uuid() assert id3.get_as_uuid() != id4.get_as_uuid()
def testMatchingProcess(self): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(5) mc1Cond = Condition() matchingCalls1 = [] mc2Cond = Condition() matchingCalls2 = [] def matchingAction1(event): with mc1Cond: matchingCalls1.append(event) mc1Cond.notifyAll() def matchingAction2(event): with mc2Cond: matchingCalls2.append(event) mc2Cond.notifyAll() matchingRecordingFilter1 = RecordingTrueFilter() matchingRecordingFilter2 = RecordingTrueFilter() ep.addFilter(matchingRecordingFilter1) ep.addFilter(matchingRecordingFilter2) ep.addHandler(matchingAction1, wait=True) ep.addHandler(matchingAction2, wait=True) event1 = Event(EventId(uuid.uuid4(), 0)) event2 = Event(EventId(uuid.uuid4(), 1)) ep.handle(event1) ep.handle(event2) # both filters must have been called with matchingRecordingFilter1.condition: while len(matchingRecordingFilter1.events) < 4: matchingRecordingFilter1.condition.wait() self.assertEqual(4, len(matchingRecordingFilter1.events)) self.assertTrue(event1 in matchingRecordingFilter1.events) self.assertTrue(event2 in matchingRecordingFilter1.events) with matchingRecordingFilter2.condition: while len(matchingRecordingFilter2.events) < 4: matchingRecordingFilter2.condition.wait() self.assertEqual(4, len(matchingRecordingFilter2.events)) self.assertTrue(event1 in matchingRecordingFilter2.events) self.assertTrue(event2 in matchingRecordingFilter2.events) # both actions must have been called with mc1Cond: while len(matchingCalls1) < 2: mc1Cond.wait() self.assertEqual(2, len(matchingCalls1)) self.assertTrue(event1 in matchingCalls1) self.assertTrue(event2 in matchingCalls1) with mc2Cond: while len(matchingCalls2) < 2: mc2Cond.wait() self.assertEqual(2, len(matchingCalls2)) self.assertTrue(event1 in matchingCalls2) self.assertTrue(event2 in matchingCalls2) ep.removeFilter(matchingRecordingFilter2) ep.removeFilter(matchingRecordingFilter1)
def test_matching_process(self): ep = rsb.eventprocessing.ParallelEventReceivingStrategy(5) mc1_cond = Condition() matching_calls1 = [] mc2_cond = Condition() matching_calls2 = [] def matching_action1(event): with mc1_cond: matching_calls1.append(event) mc1_cond.notifyAll() def matching_action2(event): with mc2_cond: matching_calls2.append(event) mc2_cond.notifyAll() matching_recording_filter_1 = RecordingTrueFilter() matching_recording_filter_2 = RecordingTrueFilter() ep.add_filter(matching_recording_filter_1) ep.add_filter(matching_recording_filter_2) ep.add_handler(matching_action1, wait=True) ep.add_handler(matching_action2, wait=True) event1 = Event(EventId(uuid.uuid4(), 0)) event2 = Event(EventId(uuid.uuid4(), 1)) ep.handle(event1) ep.handle(event2) # both filters must have been called with matching_recording_filter_1.condition: while len(matching_recording_filter_1.events) < 4: matching_recording_filter_1.condition.wait() assert len(matching_recording_filter_1.events) == 4 assert event1 in matching_recording_filter_1.events assert event2 in matching_recording_filter_1.events with matching_recording_filter_2.condition: while len(matching_recording_filter_2.events) < 4: matching_recording_filter_2.condition.wait() assert len(matching_recording_filter_2.events) == 4 assert event1 in matching_recording_filter_2.events assert event2 in matching_recording_filter_2.events # both actions must have been called with mc1_cond: while len(matching_calls1) < 2: mc1_cond.wait() assert len(matching_calls1) == 2 assert event1 in matching_calls1 assert event2 in matching_calls1 with mc2_cond: while len(matching_calls2) < 2: mc2_cond.wait() assert len(matching_calls2) == 2 assert event1 in matching_calls2 assert event2 in matching_calls2 ep.remove_filter(matching_recording_filter_2) ep.remove_filter(matching_recording_filter_1)
def testGetAsUUID(self): id1 = EventId(uuid.uuid4(), 23) id2 = EventId(id1.participantId, 23) id3 = EventId(id1.participantId, 24) id4 = EventId(uuid.uuid4(), 24) self.assertEqual(id1.getAsUUID(), id2.getAsUUID()) self.assertNotEqual(id1.getAsUUID(), id3.getAsUUID()) self.assertNotEqual(id1.getAsUUID(), id4.getAsUUID()) self.assertNotEqual(id3.getAsUUID(), id4.getAsUUID())