Пример #1
0
def test_send_rcv_subid_good():
    """
    test send and receive where subid is used for routing
    """
    pay = b"\x01\x00\x80"
    test_mtype = 46656
    test_subid = 777

    # send a message
    sbuf_send = rmr.rmr_alloc_msg(MRC_SEND,
                                  3,
                                  pay,
                                  mtype=test_mtype,
                                  sub_id=test_subid)
    pre_send_summary = rmr.message_summary(sbuf_send)
    sbuf_send = rmr.rmr_send_msg(MRC_SEND, sbuf_send)
    send_summary = rmr.message_summary(sbuf_send)

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

    # asserts
    assert send_summary["message state"] == rcv_summary["message state"] == 0
    assert send_summary["message status"] == rcv_summary[
        "message status"] == "RMR_OK"
    assert pre_send_summary["payload"] == rcv_summary["payload"] == pay
    assert pre_send_summary["message type"] == rcv_summary[
        "message type"] == test_mtype
    assert pre_send_summary["subscription id"] == rcv_summary[
        "subscription id"] == test_subid
Пример #2
0
def rmr_rcvall_msgs_raw(mrc, pass_filter=[]):
    """
    Same as rmr_rcvall_msgs, but the raw sbuf is also returned.
    Useful, for example, if rts is to be used.

    Parameters
    ----------
        mrc: ctypes c_void_p
            Pointer to the RMR context

        pass_filter: list (optional)
            The message type(s) to capture.

    Returns
    -------
    list of tuple:$
       List of tuples [(S, sbuf),...] where S is a message summary and sbuf is the raw message$
       the caller is responsible for calling rmr.rmr_free_msg(sbuf) for each sbuf afterwards to prevent memory leaks.
    """

    new_messages = []

    while True:
        mbuf = rmr.rmr_alloc_msg(mrc, 4096)  # allocate buffer to have something for a return status
        mbuf = rmr.rmr_torcv_msg(mrc, mbuf, 0)  # set the timeout to 0 so this doesn't block!!
        summary = rmr.message_summary(mbuf)
        if summary["message status"] != "RMR_OK":
            break

        if len(pass_filter) == 0 or mbuf.contents.mtype in pass_filter:  # no filter, or passes; capture it
            new_messages.append((summary, mbuf))
        else:
            rmr.rmr_free_msg(mbuf)

    return new_messages
Пример #3
0
def test_rcv_mock(monkeypatch):
    """
    tests the rmr recieve mocking generator
    """
    rmr_mocks.patch_rmr(monkeypatch)
    sbuf = rmr.rmr_alloc_msg(MRC, SIZE)

    # test rcv
    monkeypatch.setattr(
        "rmr.rmr.rmr_rcv_msg",
        rmr_mocks.rcv_mock_generator({"foo": "bar"}, 666, 0, True))
    sbuf = rmr.rmr_rcv_msg(MRC, sbuf)
    assert rmr.get_payload(sbuf) == b'{"foo": "bar"}'
    assert sbuf.contents.mtype == 666
    assert sbuf.contents.state == 0
    assert sbuf.contents.len == 14

    # test torcv, although the timeout portion is not currently mocked or tested
    monkeypatch.setattr(
        "rmr.rmr.rmr_torcv_msg",
        rmr_mocks.rcv_mock_generator({"foo": "bar"}, 666, 0, True, 50))
    sbuf = rmr.rmr_torcv_msg(MRC, sbuf, 5)
    assert rmr.get_payload(sbuf) == b'{"foo": "bar"}'
    assert sbuf.contents.mtype == 666
    assert sbuf.contents.state == 0
    assert sbuf.contents.len == 14
Пример #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[
        "message state"] == 0  # if send fails don't attempt receive
    assert send_summary["message 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["message state"] == 0
    assert rcv_summary["message status"] == "RMR_OK"
    assert rcv_summary["message type"] == 0
    assert rcv_summary["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["message state"] == rcv_ack_summary[
        "message state"] == 0
    assert send_ack_summary["message status"] == rcv_ack_summary[
        "message status"] == "RMR_OK"
    assert send_ack_summary["payload"] == ack_pay
    assert send_ack_summary["message type"] == 6666
Пример #5
0
def test_rcv_timeout():
    """
    test torcv; this is a scary test because if it fails... it doesn't fail, it will run forever!
    We receive a message (though nothing has been sent) and make sure the function doesn't block forever.

    There is no unit test for rmr_rcv_msg; too dangerous, that is a blocking call that may never return.
    """
    sbuf_rcv = rmr.rmr_alloc_msg(MRC_RCV, SIZE)
    sbuf_rcv = rmr.rmr_torcv_msg(MRC_RCV, sbuf_rcv,
                                 50)  # should time out after 50ms
    summary = rmr.message_summary(sbuf_rcv)
    assert summary["message state"] == 12
    assert summary["message status"] == "RMR_ERR_TIMEOUT"
Пример #6
0
def rmr_rcvall_msgs(mrc, pass_filter=[]):
    """
    Assemble an array of all messages which can be received without
    blocking.  Effectively draining the message queue if RMR is started
    in mt-call mode, or draining any waiting TCP buffers.  If the
    pass_filter parameter is supplied it is treated as one or more message
    types to accept (pass through). Using the default, an empty list, results
    in messages with any type being captured.

    Parameters
    ----------
        mrc: ctypes c_void_p
            Pointer to the RMR context

        pass_filter: list (optional)
            The message type(s) to capture.

    Returns
    -------
        list of dict
        List of message summaries, one for each message captured.
    """

    new_messages = []
    mbuf = rmr.rmr_alloc_msg(mrc, 4096)  # allocate buffer to have something for a return status

    while True:
        mbuf = rmr.rmr_torcv_msg(mrc, mbuf, 0)  # set the timeout to 0 so this doesn't block!!

        summary = rmr.message_summary(mbuf)
        if summary["message status"] != "RMR_OK":  # ok indicates msg received, stop on all other states
            break

        if len(pass_filter) == 0 or summary["message type"] in pass_filter:  # no filter, or passes; capture it
            new_messages.append(summary)

    rmr.rmr_free_msg(mbuf)  # must free message to avoid leak
    return new_messages
Пример #7
0
    sbuf_send = rmr.rmr_send_msg(mrc, sbuf_send)
    post_send_summary = rmr.message_summary(sbuf_send)

    if not (post_send_summary["message state"] == 0 and post_send_summary["message status"] == "RMR_OK"):
        print("was unable to send query to a1!")
        time.sleep(1)
    else:
        # query worked, wait 2 seconds, then receive everything we have
        time.sleep(1)
        print("reading messages")

        # this is a hacked up version of rmr_rcvall_msgs in the rmr package
        # we need the actual messages, not the summaries, to use rts
        sbuf_rcv = rmr.rmr_alloc_msg(mrc, 4096)  # allocate buffer to have something for a return status
        while True:
            sbuf_rcv = rmr.rmr_torcv_msg(mrc, sbuf_rcv, 0)  # set the timeout to 0 so this doesn't block!!

            summary = rmr.message_summary(sbuf_rcv)
            if summary["message status"] != "RMR_OK":  # ok indicates msg received, stop on all other states
                print("no more instances received. will try again in 1s")
                break

            print("Received: {0}".format(summary))

            received_payload = json.loads(summary["payload"])
            assert received_payload["policy_type_id"] == test_type
            assert summary["message type"] == 20010

            payload = {
                "policy_type_id": received_payload["policy_type_id"],
                "policy_instance_id": received_payload["policy_instance_id"],
Пример #8
0
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['message state'] == 12:
            print("Nothing received yet")
        else:
            print("Ack Message received!: {}".format(summary))

    time.sleep(1)