def test_regression_write_read_take(common_setup):
    """
        This popped up in early testing, if the refcounting in the C backend is not done right
        the count can become zero between read and take and python frees it.
    """

    common_setup.dw.write(Message(message="Hi!"))
    assert common_setup.dr.read()[0] == Message(message="Hi!")
    assert common_setup.dr.take()[0] == Message(message="Hi!")
示例#2
0
def test_communication_order(common_setup):
    msg1 = Message(message="Hi1!")
    msg2 = Message(message="Hi2!")
    common_setup.dw.write(msg1)
    common_setup.dw.write(msg2)
    result = common_setup.dr.read(N=2)

    assert len(result) == 2
    assert result[0] == msg1
    assert result[1] == msg2
示例#3
0
def test_communication_basic_read(common_setup):
    msg = Message(message="Hi!")
    common_setup.dw.write(msg)
    result = common_setup.dr.read()

    assert len(result) == 1
    assert result[0] == msg
示例#4
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))
示例#5
0
def test_communication_take_destroys(common_setup):
    msg = Message(message="Hi!")
    common_setup.dw.write(msg)
    result1 = common_setup.dr.read()
    result2 = common_setup.dr.take()
    result3 = common_setup.dr.read()

    assert len(result1) == 1
    assert len(result2) == 1
    assert len(result3) == 0
    assert result1[0] == result2[0] == msg
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
def test_querycondition_read(common_setup):
    qc = QueryCondition(common_setup.dr,
                        SampleState.Read | ViewState.Any | InstanceState.Any,
                        lambda msg: msg.message.startswith("Goodbye"))

    assert not qc.triggered

    messages = [Message(message=f"Hi {i}!")
                for i in range(5)] + [Message(message="Goodbye")]
    for m in messages:
        common_setup.dw.write(m)

    common_setup.dr.read(N=5)
    assert not qc.triggered

    common_setup.dr.read(N=6)
    assert qc.triggered

    received = common_setup.dr.read(condition=qc)

    assert len(received) == 1 and received[0] == messages[5]
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_data_representation_writer_v0_match():
    qos = Qos(Policy.DataRepresentation(use_cdrv0_representation=True))

    dp = DomainParticipant(0)
    tp = Topic(dp, "Message", Message)
    dr = DataReader(dp, tp, qos=qos)
    dw = DataWriter(dp, tp, qos=qos)

    assert dw._use_version_2 == False

    msg = Message("Hello")
    dw.write(msg)
    assert dr.read_next() == msg
示例#10
0
def test_waitset_wait(common_setup):
    ws = WaitSet(common_setup.dp)

    rc1 = ReadCondition(common_setup.dr,
                        ViewState.Any | InstanceState.Any | SampleState.Any)
    ws.attach(rc1)

    assert ws.wait(duration(milliseconds=5)) == 0

    common_setup.dw.write(Message(message="Hi!"))

    assert ws.wait(duration(seconds=1)) == 1

    rc2 = ReadCondition(
        common_setup.dr,
        ViewState.Any | InstanceState.Any | SampleState.NotRead)
    ws.attach(rc2)

    assert ws.wait(duration(seconds=1)) == 2
示例#11
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()