Esempio n. 1
0
def send_burst(mrc, fmt, mtype=1, num=13, counter=0):
    """
        Internal function to support test_rcv_all.
        Send a burst of messages optionally giving the type, payload
        and number to send.
    """
    sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE)  # seed message buffer

    for i in range(num):
        payload = bytes(fmt % counter, "UTF-8")
        counter += 1

        rmr.set_payload_and_length(payload, sbuf_send)
        sbuf_send.contents.mtype = mtype

        max_retries = 5
        while max_retries > 0:
            sbuf_send = rmr.rmr_send_msg(mrc, sbuf_send)
            ms = rmr.message_summary(sbuf_send)
            if ms[rmr.RMR_MS_MSG_STATE] != rmr.RMR_ERR_RETRY:
                break
            max_retries -= 1
            time.sleep(0.75)

        assert ms[rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
        assert max_retries > 0
Esempio n. 2
0
def test_rmr_set_get():
    """
    test set functions
    """
    sbuf = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
    _assert_new_sbuf(sbuf)

    # test payload
    pay = b"\x01\x00\x80"
    rmr.set_payload_and_length(pay, sbuf)
    summary = rmr.message_summary(sbuf)
    assert summary[rmr.RMR_MS_PAYLOAD] == pay
    assert summary[rmr.RMR_MS_PAYLOAD_LEN] == 3

    # test transid (note we cant test payload because it's randomly gen)
    assert summary[rmr.RMR_MS_TRN_ID] == b""
    assert len(summary[rmr.RMR_MS_TRN_ID]) == 0
    rmr.generate_and_set_transaction_id(sbuf)
    summary = rmr.message_summary(sbuf)
    assert summary[rmr.RMR_MS_TRN_ID] != b""
    assert len(summary[rmr.RMR_MS_TRN_ID]) == 32

    # test meid
    assert rmr.rmr_get_meid(sbuf) == summary[rmr.RMR_MS_MEID] == b""
    rmr.rmr_set_meid(sbuf, b"666\x01\x00\x01")
    summary = rmr.message_summary(sbuf)
    assert rmr.rmr_get_meid(sbuf) == summary[rmr.RMR_MS_MEID] == b"666\x01"
    assert (len(summary[rmr.RMR_MS_MEID])) == 4

    # exercise the logging setter; cannot test the result
    rmr.rmr_set_vlevel(0)
Esempio n. 3
0
def test_wh():
    """test the ability to send a message directly, without routing, via a wormhole"""
    state = rmr.rmr_wh_state(MRC_SEND, 1)
    assert state != rmr.RMR_OK
    whid = rmr.rmr_wh_open(MRC_SEND, b"127.0.0.1:3563")
    assert whid >= 0
    state = rmr.rmr_wh_state(MRC_SEND, whid)
    assert state == rmr.RMR_OK

    sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
    _assert_new_sbuf(sbuf_send)
    mtype = 1
    sbuf_send.contents.mtype = mtype
    payload = b"Birds like worms"
    rmr.set_payload_and_length(payload, sbuf_send)
    send_summary = rmr.message_summary(sbuf_send)

    # send via call, but don't wait long for a response
    rmr.rmr_wh_call(MRC_SEND, whid, sbuf_send, 1, 100)

    # receive message in other context
    time.sleep(0.5)
    sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
    sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
    rcv_summary = rmr.message_summary(sbuf_rcv)

    # asserts
    assert send_summary[rmr.RMR_MS_MSG_STATE] == rcv_summary[
        rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
    assert send_summary[rmr.RMR_MS_MSG_TYPE] == rcv_summary[
        rmr.RMR_MS_MSG_TYPE] == mtype
    assert send_summary[rmr.RMR_MS_PAYLOAD] == rcv_summary[
        rmr.RMR_MS_PAYLOAD] == payload

    sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
    _assert_new_sbuf(sbuf_send)
    mtype = 1
    sbuf_send.contents.mtype = mtype
    payload = b"Birds like worms"
    rmr.set_payload_and_length(payload, sbuf_send)

    # send without waiting for a response
    rmr.rmr_wh_send_msg(MRC_SEND, whid, sbuf_send)

    # receive message in other context
    time.sleep(0.5)
    sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
    rcv_summary = rmr.message_summary(sbuf_rcv)

    # asserts
    assert send_summary[rmr.RMR_MS_MSG_STATE] == rcv_summary[
        rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
    assert send_summary[rmr.RMR_MS_MSG_TYPE] == rcv_summary[
        rmr.RMR_MS_MSG_TYPE] == mtype
    assert send_summary[rmr.RMR_MS_PAYLOAD] == rcv_summary[
        rmr.RMR_MS_PAYLOAD] == payload

    rmr.rmr_wh_close(MRC_SEND, whid)
Esempio n. 4
0
def test_send_rcv():
    """
    test send and receive
    """
    pay = b"\x01\x00\x80"

    # send a message
    sbuf_send = rmr.rmr_alloc_msg(MRC_SEND, SIZE)
    _assert_new_sbuf(sbuf_send)
    rmr.set_payload_and_length(pay, sbuf_send)
    sbuf_send.contents.mtype = 0
    sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
    send_summary = rmr.message_summary(sbuf_send)
    assert send_summary[
        rmr.
        RMR_MS_MSG_STATE] == rmr.RMR_OK  # if send fails don't attempt receive
    assert send_summary[rmr.RMR_MS_MSG_STATUS] == "RMR_OK"
    time.sleep(0.5)

    # receive it in other context
    sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
    sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv, 2000)
    rcv_summary = rmr.message_summary(sbuf_rcv)
    assert rcv_summary[rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
    assert rcv_summary[rmr.RMR_MS_MSG_STATUS] == "RMR_OK"
    assert rcv_summary[rmr.RMR_MS_MSG_TYPE] == 0
    assert rcv_summary[rmr.RMR_MS_PAYLOAD] == pay

    # send an ACK back
    ack_pay = b"message received"
    sbuf_rcv = rmr.rmr_rts_msg(MRC_RCV, sbuf_rcv, payload=ack_pay, mtype=6666)
    rcv_ack_summary = rmr.message_summary(sbuf_rcv)

    # have the sender receive it
    sbuf_send = rmr.rmr_torcv_msg(MRC_SEND, sbuf_send, 2000)
    send_ack_summary = rmr.message_summary(sbuf_send)

    assert send_ack_summary[rmr.RMR_MS_MSG_STATE] == rcv_ack_summary[
        rmr.RMR_MS_MSG_STATE] == rmr.RMR_OK
    assert send_ack_summary[rmr.RMR_MS_MSG_STATUS] == rcv_ack_summary[
        rmr.RMR_MS_MSG_STATUS] == "RMR_OK"
    assert send_ack_summary[rmr.RMR_MS_PAYLOAD] == ack_pay
    assert send_ack_summary[rmr.RMR_MS_MSG_TYPE] == 6666
def test_send_mock(monkeypatch):
    """
    tests the send mock
    """
    monkeypatch.setattr("ricxappframe.rmr.rmr.rmr_send_msg",
                        rmr_mocks.send_mock_generator(12))
    rmr_mocks.patch_rmr(monkeypatch)
    sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
    rmr.set_payload_and_length("testttt".encode("utf8"), sbuf)

    expected = {
        rmr.RMR_MS_MEID: None,
        rmr.RMR_MS_MSG_SOURCE: "localtest:80",
        rmr.RMR_MS_MSG_STATE: rmr.RMR_OK,
        rmr.RMR_MS_MSG_STATUS: "RMR_OK",
        rmr.RMR_MS_MSG_TYPE: 0,
        rmr.RMR_MS_PAYLOAD: b"testttt",
        rmr.RMR_MS_PAYLOAD_LEN: 7,
        rmr.RMR_MS_PAYLOAD_MAX: 4096,
        rmr.RMR_MS_SUB_ID: 0,
    }
    _partial_dict_comparison(expected, rmr.message_summary(sbuf))

    # set the mtype
    sbuf.contents.mtype = 666

    # send it (the fake send sets the state, and touches nothing else)
    sbuf = rmr.rmr_send_msg(MRC, sbuf)

    expected = {
        rmr.RMR_MS_MEID: None,
        rmr.RMR_MS_MSG_SOURCE: "localtest:80",
        rmr.RMR_MS_MSG_STATE: rmr.RMR_ERR_TIMEOUT,
        rmr.RMR_MS_MSG_STATUS: "RMR_ERR_TIMEOUT",
        rmr.RMR_MS_MSG_TYPE: 666,
        rmr.RMR_MS_PAYLOAD: None,
        rmr.RMR_MS_PAYLOAD_LEN: 7,
        rmr.RMR_MS_PAYLOAD_MAX: 4096,
        rmr.RMR_MS_SUB_ID: 0,
    }
    _partial_dict_comparison(expected, rmr.message_summary(sbuf))
Esempio n. 6
0
def test_resize_payload():
    """test the ability to insert a larger payload into an existing message"""
    mtype = 99
    subid = 100

    mbuf = rmr.rmr_alloc_msg(MRC_SEND,
                             25)  # allocate buffer with small payload
    mbuf.contents.mtype = mtype  # type and sub-id should not change
    mbuf.contents.sub_id = subid

    long_payload = b"This is a long payload that should force the message buffer to be reallocated"
    rmr.set_payload_and_length(long_payload, mbuf)
    summary = rmr.message_summary(mbuf)
    assert summary[rmr.RMR_MS_PAYLOAD_MAX] >= len(
        long_payload)  # RMR may allocate a larger payload space
    assert summary[rmr.RMR_MS_PAYLOAD_LEN] == len(
        long_payload)  # however, the length must be exactly the same
    assert summary[
        rmr.
        RMR_MS_MSG_TYPE] == mtype  # both mtype and sub-id should be preserved in new
    assert summary[rmr.RMR_MS_SUB_ID] == subid
Esempio n. 7
0
rmr.rmr_set_stimeout(mrc, 2)
sbuf = rmr.rmr_alloc_msg(mrc, 256)

# capture ctrl-c
signal.signal(signal.SIGINT, signal_handler)

while True:
    # generate a random value between 1 and 256 bytes, then gen some random  bytes with several nulls thrown in
    for val in [
            "".join([
                random.choice(string.ascii_letters + string.digits)
                for n in range(random.randint(1, 256))
            ]).encode("utf8"),
            b"\x00" + os.urandom(4) + b"\x00" + os.urandom(4) + b"\x00",
    ]:
        rmr.set_payload_and_length(val, sbuf)
        rmr.generate_and_set_transaction_id(sbuf)
        sbuf.contents.state = 0
        sbuf.contents.mtype = 0
        print("Pre send summary: {}".format(rmr.message_summary(sbuf)))
        sbuf = rmr.rmr_send_msg(mrc, sbuf)
        print("Post send summary: {}".format(rmr.message_summary(sbuf)))
        print("Waiting for return, will timeout after 2000ms")
        sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 2000)
        summary = rmr.message_summary(sbuf)
        if summary[rmr.RMR_MS_MSG_STATE] == 12:
            print("Nothing received yet")
        else:
            print("Ack Message received!: {}".format(summary))

    time.sleep(1)