Ejemplo n.º 1
0
def test_zbeacon():
    ctx = zctx.new()

    #  Create a service socket and bind to an ephemeral port
    service = zsocket.new(ctx, zmq.PUSH)
    port_nbr = zsocket.bind(service, "inproc://foo")

    #  Create beacon to broadcast our service
    announcement = str(port_nbr)
    service_beacon = zbeacon.new(ctx, 9999)
    zbeacon.set_interval(service_beacon, 100)
    zbeacon.publish(service_beacon, announcement)

    #  Create beacon to lookup service
    client_beacon = zbeacon.new(ctx, 9999)
    zbeacon.subscribe(client_beacon, '')

    #  Wait for at most 1/2 second if there's no broadcast networking
    beacon_socket = zbeacon.socket(client_beacon)
    zsocket.set_rcvtimeo(beacon_socket, 500)
    ipaddress = zstr.recv(beacon_socket)
    content = zframe.recv(beacon_socket)
    received_port = int(zframe.data(content))
    assert received_port == port_nbr
    zframe.destroy(content)

    del service_beacon
    del ctx
Ejemplo n.º 2
0
def test_zpoller(verbose=False):

    ctx = zctx.new()

    # Create a few sockets
    vent = zsocket.new(ctx, zmq.PUSH);
    rc = zsocket.bind(vent, "tcp://*:9000")
    assert rc != -1, "vent bind error"
    sink = zsocket.new(ctx, zmq.PULL)
    rc = zsocket.connect(sink, "tcp://localhost:9000");
    assert rc != -1, "sink connect error"
    bowl = zsocket.new(ctx, zmq.PULL)
    dish = zsocket.new(ctx, zmq.PULL)

    # Set-up poller
    poller = zpoller.new(bowl, sink, dish)
    assert poller, "poller create error"

    zstr.send(vent, "Hello, World")

    # We expect a message only on the sink
    which = zpoller.wait(poller, 1000)

    assert which == sink, "expected message on sink only"
    assert not zpoller.expired(poller), "unexpected poll timer exipiry"
    assert not zpoller.terminated(poller), "unexpected poller termination (interrupted)"

    message = zstr.recv(which)
    assert message == "Hello, World", "unexpected message recevied"

    # Destroy poller and context
    del poller
    del ctx
Ejemplo n.º 3
0
def test_zbeacon():

    ctx = zctx.new()
    #  Create a service socket and bind to an ephemeral port
    service = zsocket.new(ctx, zmq.PUB)
    port_nbr = zsocket.bind(service, "tcp:#*:*")
    
    #  Create beacon to broadcast our service
    announcement = str(port_nbr)
    service_beacon = zbeacon.new(9999)
    zbeacon.set_interval(service_beacon, 100)
    zbeacon.publish(service_beacon, announcement)

    #  Create beacon to lookup service
    client_beacon = zbeacon.new (9999)
    zbeacon.subscribe(client_beacon, '')

    #  Wait for at most 1/2 second if there's no broadcast networking
    zsockopt.set_rcvtimeo(zbeacon.socket(client_beacon), 500)

    ipaddress = zstr.recv(zbeacon.socket(client_beacon))

    if ipaddress:
        content = zframe.recv(zbeacon.socket(client_beacon))
        received_port = int(zframe.data(content))
        assert received_port == port_nbr
        zframe.destroy(content)
Ejemplo n.º 4
0
def test_zsock(verbose=False):
    writer = zsock.new(zmq.PUSH)
    reader = zsock.new(zmq.PULL)
    assert zsock.type_str(writer) == 'PUSH'
    assert zsock.type_str(reader) == 'PULL'
    zsock.bind(writer, 'inproc://foo')
    zsock.connect(reader, 'inproc://foo')
    zstr.send(writer, "hello!")
    assert zstr.recv(reader) == 'hello!'
Ejemplo n.º 5
0
def test_zsocket():
    ctx = zctx.new()
    push = zsocket.new(ctx, zmq.PUSH)
    pull = zsocket.new(ctx, zmq.PULL)
    zsocket.bind(push, 'inproc://test')
    zsocket.connect(pull, 'inproc://test')
    zstr.send(push, 'foo')
    assert zstr.recv(pull) == 'foo'
    zstr.send(push, 'foo')
    zsocket.poll(pull, 1)
    assert zstr.recv_nowait(pull) == 'foo'
Ejemplo n.º 6
0
def test_zstr():
    ctx = zctx.new()
    test_endpoint = 'inproc://zstr.test'
    output_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, test_endpoint)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.connect(input_s, test_endpoint)

    # Send ten strings, five strings with MORE flag and then END
    for i in range(0, 10):
        rc = zstr.send(output_s, "this is string {0}".format(i))
        assert (rc == 0)
    zstr.sendx(output_s, "This", "is", "almost", "the", "very", "END")

    # Read and count until we receive END
    string_nbr = 0
    while True:
        string = zstr.recv(input_s)
        if string == "END":
            break
        string_nbr += 1
    assert (string_nbr == 15)
Ejemplo n.º 7
0
def test_zstr():
    ctx = zctx.new()
    test_endpoint = 'inproc://zstr.test'
    output_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.bind(output_s, test_endpoint)
    input_s = zsocket.new(ctx, zmq.PAIR)
    zsocket.connect(input_s, test_endpoint)

    # Send ten strings, five strings with MORE flag and then END
    for i in range(0, 10):
        rc = zstr.send(output_s, "this is string {}".format(i))
        assert(rc == 0)
    zstr.sendx(output_s, "This", "is", "almost", "the", "very", "END")

    # Read and count until we receive END
    string_nbr = 0
    while True:
        string = zstr.recv(input_s)
        if string == "END":
            break
        string_nbr += 1
    assert(string_nbr == 15)
Ejemplo n.º 8
0
 def on_socket_event(loop, item, arg):
     assert zstr.recv(item.socket) == "PING"
     assert arg == 3
     return -1
Ejemplo n.º 9
0
def test_zsocket():

    ctx = zctx.new()

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

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

    # Check unbind
    rc = zsocket.unbind(writer, "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 = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service


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

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

    assert zsocket.poll(writer, 100) == False

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

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

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "ABC")
    assert zframe.more(frame)
    zframe.destroy(frame)

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "DEFG")
    assert not zframe.more(frame)
    zframe.destroy(frame)

    del writer
    del ctx
Ejemplo n.º 10
0
 def recv(self):
     return zstr.recv(self.sock)
Ejemplo n.º 11
0
 def item_handler(loop, item, arg):
     assert zstr.recv(item.socket) == 'PING'
     assert ffi.from_handle(arg) == 3
     return -1
Ejemplo n.º 12
0
 def on_socket_event(loop, item, arg):
     assert zstr.recv(item.socket) == 'PING'
     assert arg == 3
     return -1
Ejemplo n.º 13
0
 def recv(self):
     return zstr.recv(self.sock)
Ejemplo n.º 14
0
def test_zactor(verbose=False):
    actor = zactor.new(echo_actor, "Hello, World")
    zstr.sendx(actor, "ECHO", "This is a string")
    assert zstr.recv(actor) == "This is a string"
Ejemplo n.º 15
0
def test_zsocket():

    ctx = zctx.new()

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

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

    # Check unbind
    rc = zsocket.unbind(writer, "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 = zsocket.bind(writer, "tcp://{0}:{1}".format(interf, service))
    assert rc == service

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

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

    assert zsocket.poll(writer, 100) == False

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

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

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "ABC")
    assert zframe.more(frame)
    zframe.destroy(frame)

    frame = zframe.recv(reader)
    assert zframe.streq(frame, "DEFG")
    assert not zframe.more(frame)
    zframe.destroy(frame)

    del writer
    del ctx