示例#1
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())
示例#2
0
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
示例#3
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)
示例#4
0
def test_reader_initialize():
    dp = DomainParticipant(0)
    tp = Topic(dp, "Message", Message)
    sub = Subscriber(dp)
    dr = DataReader(sub, tp)

    assert isgoodentity(dr)
示例#5
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
示例#6
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))
示例#7
0
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
示例#8
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
示例#9
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")
示例#10
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)
示例#11
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]
示例#12
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)

    for msg in dr1.read_iter(timeout=duration(milliseconds=10)):
        assert msg.key == dp.guid

    for msg in dr2.take_iter(timeout=duration(milliseconds=10)):
        msg.key in [dr1.guid, dr2.guid]
示例#13
0
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
示例#14
0
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
示例#15
0
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
示例#16
0
    def __init__(self, args, dp, eqos, waitset):
        self.dp = dp

        if args.topic:
            self.topic_name = args.topic
            self.dynamic = False
        else:
            self.topic_name = args.dynamic
            self.dynamic = True

        self.seq = -1  # Sequence number counter
        self.eqos = eqos  # Entity qos
        self.entities = {}  # Store writers and readers
        self.file = args.filename  # Write to file or not
        self.track_samples = {}  # Track read samples if needs to write to file
        try:
            self.listener = QosListener()
            self.pub = Publisher(dp, qos=self.eqos.publisher_qos)
            self.sub = Subscriber(dp,
                                  qos=self.eqos.subscriber_qos,
                                  listener=self.listener)

            if self.dynamic:
                ds, tmap = discover_datatype(self.dp, self.topic_name)
                self.entity = self.create_entities(ds, self.topic_name)
                self.type_map = tmap
                print(f"Discovered datatype dynamically:{ds}")
            else:
                for type in datatypes:
                    self.entities[type] = self.create_entities(
                        type, self.topic_name + type.postfix())

        except DDSException:
            raise Exception(
                "The arguments inputted are considered invalid for cyclonedds."
            )

        self.read_cond = ReadCondition(
            (self.entities[Integer]
             if not self.dynamic else self.entity).reader,
            ViewState.Any | InstanceState.Alive | SampleState.NotRead)
        waitset.attach(self.read_cond)
示例#17
0
 * v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
"""

from cyclonedds.domain import DomainParticipant
from cyclonedds.pub import Publisher, DataWriter
from cyclonedds.sub import Subscriber, DataReader
from cyclonedds.topic import Topic

from pycdr import cdr


@cdr
class HelloWorld:
    data: str


dp = DomainParticipant()
tp = Topic(dp, "Hello", HelloWorld)

pub = Publisher(dp)
dw = DataWriter(pub, tp)

sub = Subscriber(dp)
dr = DataReader(sub, tp)

dw.write(HelloWorld(data='Hello, World!'))
sample = dr.read()[0]
print(sample.data)
示例#18
0
def test_subscriber_notify_readers():
    """This will fail, notify_readers is not yet implemented on the C side."""
    dp = DomainParticipant(0)
    sub = Subscriber(dp)
    sub.notify_readers()
示例#19
0
def test_subscriber_initialize():
    dp = DomainParticipant(0)
    sub = Subscriber(dp)

    assert isgoodentity(sub)
示例#20
0
def test_pub_matched_cross_participant(hitpoint_factory):
    hpf = hitpoint_factory

    class MyListener(Listener):
        def __init__(self):
            super().__init__()
            self.hitpoint_data_available = hpf()
            self.hitpoint_pub_matched = hpf()
            self.hitpoint_sub_matched = hpf()

        def on_data_available(self, reader):
            self.hitpoint_data_available.hit()

        def on_publication_matched(self, writer,status):
            self.hitpoint_pub_matched.hit()

        def on_subscription_matched(self, reader, status):
            self.hitpoint_sub_matched.hit()

    domain1_participant_listener = MyListener()
    domain2_participant_listener = MyListener()
    dp1 = DomainParticipant(listener=domain1_participant_listener)
    dp2 = DomainParticipant(listener=domain2_participant_listener)

    tp1 = Topic(dp1, "Message", Message)
    tp2 = Topic(dp2, "Message", Message)

    publisher_listener = MyListener()
    pub = Publisher(dp1, listener=publisher_listener)

    subscriber_listener = MyListener()
    sub = Subscriber(dp2, listener=subscriber_listener)

    datawriter_listener = MyListener()
    datawriter = DataWriter(pub, tp1, listener=datawriter_listener)

    datareader_listener = MyListener()
    datareader = DataReader(sub, tp2, listener=datareader_listener)

    datawriter.write(Message("hi!"))

    #  Assertions, _only_ datawriter should publication match,
    # _only_ datareader should subscriber match and receive data

    assert datawriter_listener.hitpoint_pub_matched.was_hit()
    assert datareader_listener.hitpoint_sub_matched.was_hit()
    assert datareader_listener.hitpoint_data_available.was_hit()

    assert domain1_participant_listener.hitpoint_pub_matched.was_not_hit()
    assert domain1_participant_listener.hitpoint_sub_matched.was_not_hit()
    assert domain1_participant_listener.hitpoint_data_available.was_not_hit()
    assert domain2_participant_listener.hitpoint_pub_matched.was_not_hit()
    assert domain2_participant_listener.hitpoint_sub_matched.was_not_hit()
    assert domain2_participant_listener.hitpoint_data_available.was_not_hit()

    assert publisher_listener.hitpoint_pub_matched.was_not_hit()
    assert publisher_listener.hitpoint_sub_matched.was_not_hit()
    assert publisher_listener.hitpoint_data_available.was_not_hit()

    assert subscriber_listener.hitpoint_pub_matched.was_not_hit()
    assert subscriber_listener.hitpoint_sub_matched.was_not_hit()
    assert subscriber_listener.hitpoint_data_available.was_not_hit()

    assert datawriter_listener.hitpoint_sub_matched.was_not_hit()
    assert datawriter_listener.hitpoint_data_available.was_not_hit()

    assert datareader_listener.hitpoint_pub_matched.was_not_hit()
示例#21
0
 def sub(self, qos=None, listener=None):
     self._sub = Subscriber(self.dp, qos=qos, listener=listener)
     return self._sub
def _make_reader_without_saving_deps():
    tp = Topic(DomainParticipant(0), "Message", Message)
    return DataReader(Subscriber(tp.participant), tp)
示例#23
0
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
"""

from cyclonedds.core import Listener, WaitSet, ReadCondition, ViewState, SampleState, InstanceState, Qos, Policy
from cyclonedds.domain import DomainParticipant
from cyclonedds.topic import Topic
from cyclonedds.sub import Subscriber, DataReader
from cyclonedds.util import duration

from vehicles import Vehicle


class MyListener(Listener):
    def on_liveliness_changed(self, reader, status):
        print(">> Liveliness event")


listener = MyListener()
qos = Qos(Policy.Reliability.BestEffort(duration(seconds=1)),
          Policy.Deadline(duration(microseconds=10)),
          Policy.Durability.Transient, Policy.History.KeepLast(10))

domain_participant = DomainParticipant(0)
topic = Topic(domain_participant, 'Vehicle', Vehicle, qos=qos)
subscriber = Subscriber(domain_participant)
reader = DataReader(domain_participant, topic, listener=listener)

for sample in reader.take_iter(timeout=duration(seconds=2)):
    print(sample)