def test_scoped_qos(common_setup): with pytest.raises(TypeError): DomainParticipant(qos=Qos().topic()) DomainParticipant(qos=Qos().domain_participant()) with pytest.raises(TypeError): Topic(common_setup.dp, 'Message', Message, qos=Qos().domain_participant()) Topic(common_setup.dp, 'Message', Message, qos=Qos().topic()) with pytest.raises(TypeError): Publisher(common_setup.dp, qos=Qos().subscriber()) Publisher(common_setup.dp, qos=Qos().publisher()) with pytest.raises(TypeError): Subscriber(common_setup.dp, qos=Qos().publisher()) Subscriber(common_setup.dp, qos=Qos().subscriber()) with pytest.raises(TypeError): DataWriter(common_setup.pub, common_setup.tp, qos=Qos().datareader()) DataWriter(common_setup.pub, common_setup.tp, qos=Qos().datawriter()) with pytest.raises(TypeError): DataReader(common_setup.sub, common_setup.tp, qos=Qos().datawriter()) DataReader(common_setup.sub, common_setup.tp, qos=Qos().datareader())
def test_get_participant(): dp = DomainParticipant(0) assert dp.participant == dp.get_participant() == dp tp = Topic(dp, "Message", Message) assert isgoodentity(tp) assert tp.participant == tp.get_participant() == dp sub = Subscriber(dp) assert isgoodentity(sub) assert sub.participant == sub.get_participant() == dp pub = Publisher(dp) assert isgoodentity(pub) assert pub.participant == pub.get_participant() == dp dr = DataReader(sub, tp) assert isgoodentity(dr) assert dr.participant == dr.get_participant() == dp dw = DataWriter(pub, tp) assert isgoodentity(dw) assert dw.participant == dw.get_participant() == dp
def test_all_entities_json_reported(): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) dr = DataReader(dp, tp) time.sleep(1) data = run_ddsls(["--json", "-a"]) json_data = json.loads(data["stdout"]) assert str(dw.guid) in data["stdout"] assert str(dr.guid) in data["stdout"] assert str(dp.guid) in data["stdout"] assert tp.name in data["stdout"] assert tp.typename in data["stdout"] writer_check, reader_check = False, False for sample in json_data: for val in sample["value"]: if val["key"] == str(dw.guid): assert (dw.get_qos()).asdict() == val["qos"] writer_check = True if val["key"] == str(dr.guid): assert (dr.get_qos()).asdict() == val["qos"] reader_check = True assert reader_check and writer_check
class Topicmanger: def __init__(self, args, dp, qos, waitset): self.topic_name = args.topic self.seq = -1 try: int_topic = Topic(dp, self.topic_name + "int", Integer, qos=qos) str_topic = Topic(dp, self.topic_name, String, qos=qos) self.int_writer = DataWriter(dp, int_topic, qos=qos) self.int_reader = DataReader(dp, int_topic, qos=qos) self.str_writer = DataWriter(dp, str_topic, qos=qos) self.str_reader = DataReader(dp, str_topic, qos=qos) except DDSException: raise SystemExit("Error: The arguments inputted are considered invalid for cyclonedds.") self.read_cond = ReadCondition(self.int_reader, ViewState.Any | InstanceState.Alive | SampleState.NotRead) waitset.attach(self.read_cond) def write(self, input): self.seq += 1 if type(input) is int: msg = Integer(self.seq, input) self.int_writer.write(msg) else: msg = String(self.seq, input) self.str_writer.write(msg) def read(self): for sample in self.int_reader.take(N=100): print("Subscribed:", sample) for sample in self.str_reader.take(N=100): print("Subscribed:", sample)
def test_writer_writedispose(): dp = DomainParticipant(0) tp = Topic(dp, "MessageKeyed", MessageKeyed) pub = Publisher(dp) dw = DataWriter(pub, tp) msg = MessageKeyed(user_id=1, message="Hello") dw.write_dispose(msg)
def test_writeto_writer(): dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) pub = Publisher(dp) dw = DataWriter(pub, tp) msg = Message(message="TestMessage") dw.write(msg) assert dw.wait_for_acks(duration(seconds=1))
def test_data_representation_writer_v2_v0_unmatch(): qosv0 = Qos(Policy.DataRepresentation(use_cdrv0_representation=True)) qosv2 = Qos(Policy.DataRepresentation(use_xcdrv2_representation=True)) dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) dr = DataReader(dp, tp, qos=qosv2) dw = DataWriter(dp, tp, qos=qosv0) msg = Message("Hello") dw.write(msg) assert dr.read_next() == None
def test_keyed_type_alignment(): dp = DomainParticipant() tp = Topic(dp, "Test", KeyedArrayType) dw = DataWriter(dp, tp) dr = DataReader(dp, tp) samp1 = KeyedArrayType(['u' * 17, 's' * 13, 'b' * 23], ['u' * 3, 's' * 5, 'b' * 7], [-1] * 3, [-1] * 3, [-1] * 3, [-1] * 3) dw.write(samp1) samp2 = dr.read()[0] assert KeyedArrayType.cdr.key(samp1) == KeyedArrayType.cdr.key(samp2)
def test_data_representation_writer_v2_match(): qos = Qos(Policy.DataRepresentation(use_xcdrv2_representation=True)) dp = DomainParticipant(0) tp = Topic(dp, "XMessage", XMessage) dr = DataReader(dp, tp, qos=qos) dw = DataWriter(dp, tp, qos=qos) assert dw._use_version_2 == True msg = XMessage("Hello") dw.write(msg) assert dr.read_next() == msg
def test_publication_reported(): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) time.sleep(1) data = run_ddsls(["-t", "dcpspublication"]) assert str(dw.guid) in data["stdout"] assert str(dp.guid) in data["stdout"] assert tp.name in data["stdout"] assert tp.typename in data["stdout"] assert str(dw.get_qos()) in data["stdout"]
def __init__(self, args, dp, qos, waitset): self.topic_name = args.topic self.seq = -1 try: int_topic = Topic(dp, self.topic_name + "int", Integer, qos=qos) str_topic = Topic(dp, self.topic_name, String, qos=qos) self.int_writer = DataWriter(dp, int_topic, qos=qos) self.int_reader = DataReader(dp, int_topic, qos=qos) self.str_writer = DataWriter(dp, str_topic, qos=qos) self.str_reader = DataReader(dp, str_topic, qos=qos) except DDSException: raise SystemExit("Error: The arguments inputted are considered invalid for cyclonedds.") self.read_cond = ReadCondition(self.int_reader, ViewState.Any | InstanceState.Alive | SampleState.NotRead) waitset.attach(self.read_cond)
def test_writer_lookup(): dp = DomainParticipant(0) tp = Topic(dp, "MessageKeyed", MessageKeyed) pub = Publisher(dp) dw = DataWriter(pub, tp) keymsg1 = MessageKeyed(user_id=1000, message="Hello!") keymsg2 = MessageKeyed(user_id=2000, message="Hello!") assert None == dw.lookup_instance(keymsg1) assert None == dw.lookup_instance(keymsg2) handle1 = dw.register_instance(keymsg1) handle2 = dw.register_instance(keymsg2) assert handle1 > 0 and handle2 > 0 and handle1 != handle2 assert handle1 == dw.lookup_instance(keymsg1) assert handle2 == dw.lookup_instance(keymsg2)
def test_get_pubsub(): dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) sub = Subscriber(dp) pub = Publisher(dp) dr = DataReader(sub, tp) dw = DataWriter(pub, tp) assert dr.subscriber == dr.get_subscriber() == sub assert dw.publisher == dw.get_publisher() == pub with pytest.raises(DDSException) as exc: dp.get_subscriber() assert exc.value.code == DDSException.DDS_RETCODE_ILLEGAL_OPERATION
def test_initialize_writer(): dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) pub = Publisher(dp) dw = DataWriter(pub, tp) assert isgoodentity(dw)
def test_dynamic_publish_complex(): dp = DomainParticipant() tp = Topic(dp, 'DynTest', XStruct) rd = DataReader(dp, tp) type_id = XStruct.__idl__.get_type_id() datatype, tmap = get_types_for_typeid(dp, type_id, duration(seconds=1)) assert datatype assert datatype.__idl__.get_type_id() == XStruct.__idl__.get_type_id() tp = Topic(dp, 'DynTest', datatype) wr = DataWriter(dp, tp) wr.write(datatype(A=tmap['XUnion'](A=tmap['XEnum'].V1), k=1)) assert rd.read()[0].k == 1
def test_dynamic_subscribe_complex(): dp = DomainParticipant() tp = Topic(dp, 'DynTest', XStruct) wr = DataWriter(dp, tp) type_id = XStruct.__idl__.get_type_id() datatype, tmap = get_types_for_typeid(dp, type_id, duration(seconds=1)) assert datatype assert datatype.__idl__.get_type_id() == XStruct.__idl__.get_type_id() tp = Topic(dp, 'DynTest', datatype) dr = DataReader(dp, tp) wr.write(XStruct(A=XUnion(A=XEnum.V1), k=1)) assert dr.read()[0].k == 1
def test_all_entities_reported(): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) dr = DataReader(dp, tp) time.sleep(1) data = run_ddsls(["-a"]) assert str(dw.guid) in data["stdout"] assert str(dr.guid) in data["stdout"] assert str(dp.guid) in data["stdout"] assert tp.name in data["stdout"] assert tp.typename in data["stdout"] assert str(dw.get_qos()) in data["stdout"] assert str(dr.get_qos()) in data["stdout"]
def test_reader_keepalive_parents(): dr = _make_reader_without_saving_deps() msg = Message("Hello") dw = DataWriter(dr.participant, dr.topic).write(msg) assert dr.read_next() == msg
def test_data_representation_writer_error_invalid_v0(): qos = Qos(Policy.DataRepresentation(use_cdrv0_representation=True)) dp = DomainParticipant(0) tp = Topic(dp, "XMessage", XMessage) with pytest.raises(DDSException): DataWriter(dp, tp, qos=qos)
def test_reader_readiter(): dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) sub = Subscriber(dp) pub = Publisher(dp) dr = DataReader(sub, tp) dw = DataWriter(pub, tp) msg = Message("Hello") dw.write(msg) read = False for msgr in dr.read_iter(timeout=duration(milliseconds=10)): assert not read assert msg == msgr read = True
async def test_inner(): qos = Qos(Policy.OwnershipStrength(10), Policy.Userdata("Old".encode())) dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp, qos=qos) await asyncio.sleep(0.5) old_qos = dw.get_qos() await asyncio.sleep(0.5) new_qos = Qos(Policy.OwnershipStrength(20), Policy.Userdata("New".encode())) dw.set_qos(new_qos) await asyncio.sleep(0.5) return dp, tp, dw, old_qos, new_qos
def __init__(self, domain_id=0): self.qos = Qos(Policy.Reliability.Reliable(duration(seconds=2)), Policy.History.KeepLast(10)) self.dp = DomainParticipant(domain_id) self.tp = Topic(self.dp, 'Message', Message) self.pub = Publisher(self.dp) self.sub = Subscriber(self.dp) self.dw = DataWriter(self.pub, self.tp, qos=self.qos) self.dr = DataReader(self.sub, self.tp, qos=self.qos) self.msg = Message(message="hi") self.msg2 = Message(message="hi2")
def test_ddsls_publication_json_reported(): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) time.sleep(1) data = run_ddsls(["--json", "-t", "dcpspublication"]) json_data = json.loads(data["stdout"]) assert str(dw.guid) in data["stdout"] assert str(dp.guid) in data["stdout"] assert tp.name in data["stdout"] assert tp.typename in data["stdout"] writer_check = False for sample in json_data: for val in sample["value"]: if val["key"] == str(dw.guid): assert (dw.get_qos()).asdict() == val["qos"] writer_check = True assert writer_check
def check_enforced_non_communication(log: Stream, ctx: FullContext, typename: str) -> bool: datatype_regular = ctx.get_datatype(typename) if datatype_regular.__idl__.keyless: return True narrow_ctx = ctx.narrow_context_of(typename) new_scope = deepcopy(narrow_ctx.scope) non_valid_mutation(new_scope, typename) mutated_ctx = FullContext(new_scope) mutated_datatype = mutated_ctx.get_datatype(typename) normal_without_header_idl = "\n".join(narrow_ctx.idl_file.splitlines()[1:]) mutated_without_header_idl = "\n".join( mutated_ctx.idl_file.splitlines()[1:]) if normal_without_header_idl == mutated_without_header_idl: # No mutation took place (only unions) just assume it is good return True dp = DomainParticipant() try: tp = Topic(dp, typename, mutated_datatype) except DDSException: # Sometimes the type gets so mangled (like empty structs/unions) # that it is not a valid topic type anymore. We'll consider this a # successful test. return True dw = DataWriter( dp, tp, qos=Qos(Policy.DataRepresentation(use_xcdrv2_representation=True), Policy.Reliability.Reliable(duration(seconds=2)), Policy.DestinationOrder.BySourceTimestamp)) dw.set_status_mask(DDSStatus.PublicationMatched) dw.take_status() ctx.c_app.run(typename, 1) now = time.time() while (dw.take_status() & DDSStatus.PublicationMatched) == 0: if time.time() - now > 0.5: ctx.c_app.process.kill() return True time.sleep(0.001) ctx.c_app.process.kill() log << f"C-app agreed to communicate with non-valid mutation" << log.endl << log.indent log << log.dedent << "[Mutated IDL]:" << log.indent << log.endl log << mutated_ctx.idl_file << log.endl log << log.dedent return False
def test_writer_instance_handle(): dp = DomainParticipant(0) tp = Topic(dp, "MessageKeyed", MessageKeyed) pub = Publisher(dp) dw = DataWriter(pub, tp) msg = MessageKeyed(user_id=1, message="Hello") handle = dw.register_instance(msg) assert handle > 0 dw.write(msg) dw.unregister_instance_handle(handle)
def test_get_children(): dp = DomainParticipant(0) assert len(dp.children) == len(dp.get_children()) == 0 tp = Topic(dp, "Message", Message) assert isgoodentity(tp) assert len(dp.children) == len(dp.get_children()) == 1 assert dp.children[0] == dp.get_children()[0] == tp assert len(tp.children) == len(tp.get_children()) == 0 sub = Subscriber(dp) assert isgoodentity(sub) assert len(dp.children) == len(dp.get_children()) == 2 assert set(dp.children) == set([sub, tp]) assert len(sub.children) == len(sub.get_children()) == 0 pub = Publisher(dp) assert isgoodentity(pub) assert len(dp.children) == len(dp.get_children()) == 3 assert set(dp.children) == set([pub, sub, tp]) assert len(pub.children) == len(pub.get_children()) == 0 dr = DataReader(sub, tp) assert isgoodentity(dr) assert set(dp.children) == set([pub, sub, tp]) assert len(sub.children) == 1 assert sub.children[0] == dr dw = DataWriter(pub, tp) assert isgoodentity(dw) assert set(dp.children) == set([pub, sub, tp]) assert len(pub.children) == 1 assert pub.children[0] == dw del dw del dr del pub del sub del tp assert len(dp.children) == len(dp.get_children()) == 0
def test_data_representation_writer_dualreader_match(): qosv0 = Qos(Policy.DataRepresentation(use_cdrv0_representation=True)) qosv2 = Qos(Policy.DataRepresentation(use_xcdrv2_representation=True)) qosv0v2 = Qos( Policy.DataRepresentation(use_cdrv0_representation=True, use_xcdrv2_representation=True)) dp = DomainParticipant(0) tp = Topic(dp, "Message", Message) dr = DataReader(dp, tp, qos=qosv0v2) dwv0 = DataWriter(dp, tp, qos=qosv0) dwv2 = DataWriter(dp, tp, qos=qosv2) msg1 = Message("Hello") dwv0.write(msg1) assert dr.read_next() == msg1 msg2 = Message("Hi!") dwv2.write(msg2) assert dr.read_next() == msg2
async def test_inner(): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) dr = DataReader(dp, tp) disposed_data = { "dp.guid": str(dp.guid), "tp.name": tp.name, "tp.typename": tp.typename, "dw.guid": str(dw.guid), "dr.guid": str(dr.guid) } await asyncio.sleep(1) del dp, tp, dw, dr gc.collect() await asyncio.sleep(1) return disposed_data
def test_reader_readnext_takenext(): dp = DomainParticipant(0) tp = Topic(dp, "Message__DONOTPUBLISH", Message) sub = Subscriber(dp) pub = Publisher(dp) dr = DataReader(sub, tp) dw = DataWriter(pub, tp) msg = Message("Hello") dw.write(msg) assert dr.read_next() == msg assert dr.read_next() is None dw.write(msg) assert dr.take_next() == msg assert dr.take_next() is None
def test_ddsls_write_to_file(tmp_path): dp = DomainParticipant(0) tp = Topic(dp, "MessageTopic", Message) dw = DataWriter(dp, tp) dr = DataReader(dp, tp) time.sleep(0.5) run_ddsls(["--json", "-a", "--filename", str(tmp_path / "test.json")]) time.sleep(0.5) with open(tmp_path / "test.json") as f: data = json.load(f) assert str(dw.guid) in data["PUBLICATION"]["New"] assert str(dr.guid) in data["SUBSCRIPTION"]["New"] assert str(dp.guid) in data["PARTICIPANT"]["New"] assert tp.name in data["PUBLICATION"]["New"][str(dw.guid)]["topic_name"] assert tp.typename in data["PUBLICATION"]["New"][str(dw.guid)]["type_name"] assert tp.name in data["SUBSCRIPTION"]["New"][str(dr.guid)]["topic_name"] assert tp.typename in data["SUBSCRIPTION"]["New"][str(dr.guid)]["type_name"]