示例#1
0
def test_message():
    ctx = Context()

    test_endpoint = "inproc://zmsg.test"
    output_s = ctx.socket(zmq.PAIR)
    rc = output_s.bind(test_endpoint)
    assert rc >= 0
    input_s = ctx.socket(zmq.PAIR)
    rc = input_s.connect(test_endpoint)
    assert rc == 0

    msg = Message()

    foo = Frame('Hello')
    msg.push(foo)
    assert len(msg) == 1
    assert msg.content_size() == 5

    rc = output_s.send_msg(msg)
    assert rc == 0

    msg = input_s.recv_msg()
    assert len(msg) == 1
    assert msg.content_size() == 5
    del msg

    msg = Message()
    for i in range(0, 10):
        rc = msg.append("Frame{0}".format(i))
        assert rc == 0

    copy = msg.dup()
    rc = output_s.send_msg(copy)
    assert rc == 0
    rc = output_s.send_msg(msg)
    assert rc == 0

    copy = input_s.recv_msg()
    assert len(copy) == 10
    assert copy.content_size() == 60
    del copy

    msg = input_s.recv_msg()
    assert len(msg) == 10
    assert msg.content_size() == 60

    for i in range(0, 10):
        assert msg.next() == "Frame{0}".format(i)
    del msg

    #TODO: continue adding remaining class function tests

    del ctx
示例#2
0
def test_frame():
    ctx = Context()
    test_endpoint = 'inproc://zframe.test'
    output_s = ctx.socket(zmq.PAIR)
    rc = output_s.bind(test_endpoint)
    assert rc >= 0
    input_s = ctx.socket(zmq.PAIR)
    rc = input_s.connect(test_endpoint)
    assert rc == 0

    # Send five different frames, test ZFRAME_MORE
    for i in range(0, 5):
        frame = Frame("Hello")
        rc = output_s.send_frame(frame, Frame.MORE)
        assert rc == 0, "error sending frame rc={0}, {1}".format(rc, zmq.strerror(zmq.errno()))

    # Send same frame five times, test ZFRAME_REUSE
    frame = Frame("Hello")
    for i in range(0, 5):
        rc = output_s.send_frame(frame, Frame.MORE + Frame.REUSE)
        assert rc == 0, "error sending reused frame rc={0}, {1}".format(rc, zmq.strerror(zmq.errno()))

    copy = frame.dup()
    assert frame == copy

    frame.reset("")
    assert frame != copy
    assert len(copy) == 5
    del frame
    del copy

    # Test zframe_new_empty
    frame = Frame()
    assert len(frame) == 0
    del frame

    # Send END frame
    frame = Frame("NOT")
    frame.reset("END")
    hex_string = frame.strhex()
    assert hex_string == "454E44"

    frame_bytes = frame.bytes()
    assert frame_bytes == "END"

    string = str(frame)
    assert string == "END"
    rc = output_s.send_frame(frame)
    assert rc == 0

    # Read and count until we receive END
    frame_nbr = 0
    while True:
        frame = input_s.recv_frame()
        frame_nbr += 1
        if frame == "END":
            del frame
            break
        else:
            assert len(frame) == 5
            assert frame.bytes() == "Hello"
        assert frame.more()
        frame.set_more(0)
        assert frame.more() == 0
        del frame
    assert frame_nbr == 11
    frame = input_s.recv_frame_nowait()
    assert frame is None

    del ctx
示例#3
0
def test_socket():
    ctx = Context()

    # Create a detached thread, let it run
    interf = "*"
    domain = "localhost"
    service = 5560

    writer = ctx.socket(zmq.PUSH)
    assert writer
    reader = ctx.socket(zmq.PULL)
    assert reader
    assert writer.type() == "PUSH"
    assert reader.type() == "PULL"
    rc = writer.bind("tcp://{0}:{1}".format(interf, service))
    assert rc == service


    # Check unbind
    rc = writer.unbind("tcp://{0}:{1}".format(interf, service))
    assert rc == 0

    # In some cases and especially when running under Valgrind, doing
    # a bind immediately after an unbind causes an EADDRINUSE error.
    # Even a short sleep allows the OS to release the port for reuse.
    time.sleep(0.1)

    # Bind again
    rc = writer.bind("tcp://{0}:{1}".format(interf, service))
    assert rc == service


    rc = reader.connect("tcp://{0}:{1}".format(domain, service))
    assert rc == 0
    writer.send("HELLO")
    message = reader.recv()
    assert message
    assert message == "HELLO"

    # Test binding to ports
    port = writer.bind("tcp://{0}:*".format(interf))
    assert (port >= zsocket.DYNFROM and port <= zsocket.DYNTO)

    assert writer.poll(100) == False

    rc = reader.connect("txp://{0}:{1}".format(domain, service))
    assert rc == -1

    # Test sending frames to socket
    frame = Frame("ABC")
    rc = writer.send_frame(frame, Frame.MORE)
    assert rc == 0
    frame = Frame("DEFG")
    rc = writer.send_frame(frame)
    assert rc == 0

    frame = reader.recv_frame()
    assert frame == "ABC"
    assert frame.more()
    del frame

    frame = reader.recv_frame()
    assert frame == "DEFG"
    assert not frame.more()
    del frame

    del writer
    del reader
    del ctx