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
Example #2
0
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_delete_entity():
    dp = DomainParticipant(0)

    assert dp.get_participant() == dp

    reference = dp._ref
    del dp

    with pytest.raises(DDSException):
        Entity(reference).get_participant()
Example #4
0
def test_find_topic():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message", Message)

    assert isgoodentity(tp)
    
    xtp = dp.find_topic("Message")

    assert xtp.typename == tp.typename
    assert xtp.name == tp.name
Example #5
0
def test_domain_id():
    dp = DomainParticipant(0)
    dp1 = DomainParticipant(33)

    time.sleep(0.5)

    data = run_ddsls(["--id", "33", "-t", "dcpsparticipant"])

    time.sleep(0.5)

    assert str(dp.guid) not in data["stdout"]
    assert str(dp1.guid) in data["stdout"]
def test_delete_entity():
    dp = DomainParticipant(0)

    assert dp.get_participant() == dp

    reference = dp._ref
    del dp

    with pytest.raises(DDSException) as exc:
        Entity(reference).get_participant()
        
    # This test is weird, it can fail if other tests are also failing.
    # So if this test fails fix other tests first before spending any time here.
    assert exc.value.code == DDSException.DDS_RETCODE_PRECONDITION_NOT_MET
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
Example #8
0
def test_initialize_writer():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message", Message)
    pub = Publisher(dp)
    dw = DataWriter(pub, tp)

    assert isgoodentity(dw)
Example #9
0
def main(sys_args):
    args = create_parser(sys_args)
    eqos = QosPerEntity(args.entityqos)
    if args.qos:
        qos = QosParser.parse(args.qos)
        eqos.entity_qos(qos, args.entityqos)

    dp = DomainParticipant(0)
    waitset = WaitSet(dp)
    manager = TopicManager(args, dp, eqos, waitset)
    if args.topic or args.dynamic:
        try:
            worker = Worker(make_work_function(manager, waitset, args))
            while True:
                txt = input("")
                worker.put_input(txt)
        except (KeyboardInterrupt, IOError, ValueError):
            pass
        except EOFError:
            # stdin closed, if runtime is set wait for that to expire, else exit
            if args.runtime:
                worker.work.join()
        finally:
            worker.stop()

    return 0
Example #10
0
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
Example #11
0
def test_reader_initialize():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    assert isgoodentity(dr)
Example #12
0
def test_reader_take():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message__DONOTPUBLISH", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    assert len(dr.take()) == 0
Example #13
0
def main():
    managers = []
    args = create_parser()
    dp = DomainParticipant(args.id)
    topics = parse_args(args)
    waitset = WaitSet(dp)

    for topic_type, topic in topics:
        managers.append(TopicManager(BuiltinDataReader(dp, topic), topic_type, args))
        managers[-1].add_to_waitset(waitset)
    if args.watch:
        try:
            while True:
                for manager in managers:
                    waitset.wait(duration(milliseconds=20))
                    manager.poll()
        except KeyboardInterrupt:
            pass
    else:
        for manager in managers:
            manager.poll()

    if args.filename:
        try:
            with open(args.filename, 'w') as f:
                Output.to_file(manager, managers, f)
                print("\nResults have been written to file", args.filename, "\n")
        except OSError:
            print("could not open file")
            return 1
    return 0
Example #14
0
def test_reader_waitforhistoricaldata():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message__DONOTPUBLISH", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    assert dr.wait_for_historical_data(duration(milliseconds=5))
Example #15
0
def main():
    qos_manager = Qosmanager()
    qos = None
    args = create_parser()
    if args.qos:
        args.qos = ' '.join(args.qos)
        myqos = qos_manager.construct_policy(args.qos)
        qos = Qos(myqos)

    dp = DomainParticipant(0)
    waitset = WaitSet(dp)
    manager = Topicmanger(args, dp, qos, waitset)
    if args.topic:
        try:
            while True:
                input = select.select([sys.stdin], [], [], 0)[0]
                if input:
                    for text in sys.stdin.readline().split():
                        try:
                            text = int(text)
                            manager.write(text)
                        except ValueError:
                            manager.write(text.rstrip("\n"))
                manager.read()
                waitset.wait(duration(microseconds=20))
        except KeyboardInterrupt:
            sys.exit(0)
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)
Example #17
0
def test_reader_resizebuffer():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message__DONOTPUBLISH", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    assert len(dr.read(N=100)) == 0
    assert len(dr.read(N=200)) == 0
    assert len(dr.read(N=100)) == 0
Example #18
0
def test_participant_json_reported():
    dp = DomainParticipant(0)
    time.sleep(0.5)

    data = run_ddsls(["-t", "dcpsparticipant", "--json"])

    time.sleep(0.5)

    assert str(dp.guid) in data["stdout"]
Example #19
0
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)
Example #20
0
def main(sys_args):
    JsonWriter.reset()
    managers = []
    args = create_parser(sys_args)
    dp = DomainParticipant(args.id)
    topics = parse_args(args)
    waitset = WaitSet(dp)

    for topic_type, topic in topics:
        # Create TopicManager for each topic
        managers.append(
            TopicManager(BuiltinDataReader(dp, topic), topic_type, args))
        managers[-1].add_to_waitset(waitset)

    if not args.filename and args.json:
        print("[")

    # Watchmode
    if args.watch:
        try:
            time_start = datetime.datetime.now()
            v = True
            while v:
                for manager in managers:
                    waitset.wait(duration(milliseconds=20))
                    manager.poll()
                if args.runtime:
                    v = datetime.datetime.now(
                    ) < time_start + datetime.timedelta(seconds=args.runtime)
        except KeyboardInterrupt:
            pass
    # Non-watchmode
    else:
        time_start = datetime.datetime.now()
        runtime = args.runtime or 1
        while datetime.datetime.now() < time_start + datetime.timedelta(
                seconds=runtime):
            for manager in managers:
                manager.poll()

    if not args.filename and args.json:
        print("]")

    # Write to file
    if args.filename:
        try:
            with open(args.filename, 'w') as f:
                data = {
                    manager.topic_type: manager.as_dict()
                    for manager in managers
                }
                json.dump(data, f, indent=4)
                print(f"\nResults have been written to file {args.filename}\n")
        except OSError:
            raise Exception(f"Could not open file {args.filename}")
    return 0
Example #21
0
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))
Example #22
0
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
Example #23
0
def test_reader_invalid():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message__DONOTPUBLISH", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    with pytest.raises(TypeError):
        dr.read(-1)

    with pytest.raises(TypeError):
        dr.take(-1)
Example #24
0
def test_suspension():
    dp = DomainParticipant(0)
    pub = Publisher(dp)

    with pytest.raises(DDSException) as exc:
        pub.suspend()
    assert exc.value.code == DDSException.DDS_RETCODE_UNSUPPORTED

    with pytest.raises(DDSException) as exc:
        pub.resume()
    assert exc.value.code == DDSException.DDS_RETCODE_UNSUPPORTED
Example #25
0
    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")
Example #26
0
    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 = None
        self._pub = None
        self._sub = None
        self._dw = None
        self._dr = None
        self.msg = Message(message="hi")
        self.msg2 = Message(message="hi2")
Example #27
0
def test_subscriber_wrong_usage_errors():
    dp = DomainParticipant(0)

    with pytest.raises(TypeError):
        Subscriber(False)

    with pytest.raises(TypeError):
        Subscriber(dp, qos=False)

    with pytest.raises(TypeError):
        Subscriber(dp, listener=False)
Example #28
0
def test_builtin_dcps_participant():
    dp = DomainParticipant(0)
    sub = Subscriber(dp)
    dr1 = BuiltinDataReader(sub, BuiltinTopicDcpsParticipant)
    dr2 = BuiltinDataReader(sub, BuiltinTopicDcpsSubscription)

    assert isgoodentity(dr1)
    assert isgoodentity(dr2)
    assert dr1.take_next().key == dp.guid
    msg = dr2.take(N=2)
    assert [msg[0].key, msg[1].key] == [dr1.guid, dr2.guid] or \
           [msg[0].key, msg[1].key] == [dr2.guid, dr1.guid]
Example #29
0
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_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