Search in sources :

Example 36 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class RepSpecTest method fairQueueIn.

private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
    // Server socket will accept connections
    SocketBase rep = ZMQ.socket(ctx, bindType);
    assertThat(rep, notNullValue());
    int timeout = 250;
    boolean rc = ZMQ.setSocketOption(rep, ZMQ.ZMQ_RCVTIMEO, timeout);
    assertThat(rc, is(true));
    rc = ZMQ.bind(rep, address);
    assertThat(rc, is(true));
    int services = 5;
    List<SocketBase> reqs = new ArrayList<>();
    for (int peer = 0; peer < services; ++peer) {
        SocketBase sender = ZMQ.socket(ctx, connectType);
        assertThat(sender, notNullValue());
        reqs.add(sender);
        rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_RCVTIMEO, timeout);
        assertThat(rc, is(true));
        String host = (String) ZMQ.getSocketOptionExt(rep, ZMQ.ZMQ_LAST_ENDPOINT);
        assertThat(host, notNullValue());
        rc = ZMQ.connect(sender, host);
        assertThat(rc, is(true));
    }
    rc = sendSeq(reqs.get(0), "A");
    assertThat(rc, is(true));
    recvSeq(rep, "A");
    rc = sendSeq(rep, "A");
    assertThat(rc, is(true));
    recvSeq(reqs.get(0), "A");
    rc = sendSeq(reqs.get(0), "A");
    assertThat(rc, is(true));
    recvSeq(rep, "A");
    rc = sendSeq(rep, "A");
    assertThat(rc, is(true));
    recvSeq(reqs.get(0), "A");
    boolean someoneFixThis = false;
    // TODO V4 review this test (breaking in libzmq): there is no guarantee about the order of the replies.
    if (someoneFixThis) {
        // send N requests
        for (int peer = 0; peer < services; ++peer) {
            sendSeq(reqs.get(peer), "B" + peer);
        }
        Set<String> replies = new HashSet<>();
        // handle N requests
        for (int peer = 0; peer < services; ++peer) {
            Msg msg = ZMQ.recv(rep, 0);
            assertThat(msg, notNullValue());
            String reply = new String(msg.data(), ZMQ.CHARSET);
            replies.add(reply);
            sendSeq(rep, reply);
        }
        for (int peer = 0; peer < services; ++peer) {
            Msg msg = ZMQ.recv(reqs.get(peer), 0);
            assertThat(msg, notNullValue());
            String reply = new String(msg.data(), ZMQ.CHARSET);
            replies.remove(reply);
        }
        assertThat(replies.size(), is(0));
    }
    ZMQ.closeZeroLinger(rep);
    for (SocketBase sender : reqs) {
        ZMQ.closeZeroLinger(sender);
    }
    // Wait for disconnects.
    ZMQ.msleep(100);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 37 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class PushPullSpecTest method fairQueueIn.

private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
    // Server socket will accept connections
    SocketBase pull = ZMQ.socket(ctx, bindType);
    assertThat(pull, notNullValue());
    boolean rc = ZMQ.bind(pull, address);
    assertThat(rc, is(true));
    int services = 5;
    List<SocketBase> senders = new ArrayList<>();
    for (int peer = 0; peer < services; ++peer) {
        SocketBase sender = ZMQ.socket(ctx, connectType);
        assertThat(sender, notNullValue());
        senders.add(sender);
        String host = (String) ZMQ.getSocketOptionExt(pull, ZMQ.ZMQ_LAST_ENDPOINT);
        assertThat(host, notNullValue());
        rc = ZMQ.connect(sender, host);
        assertThat(rc, is(true));
    }
    // Wait for connections.
    ZMQ.msleep(100);
    Set<String> firstHalf = new HashSet<>();
    Set<String> secondHalf = new HashSet<>();
    // Send 2N messages
    for (int peer = 0; peer < services; ++peer) {
        sendSeq(senders.get(peer), "A" + peer);
        firstHalf.add("A" + peer);
        sendSeq(senders.get(peer), "B" + peer);
        secondHalf.add("B" + peer);
    }
    // Wait for data.
    ZMQ.msleep(100);
    // Expect to pull one from each first
    for (int peer = 0; peer < services; ++peer) {
        Msg msg = ZMQ.recv(pull, 0);
        assertThat(msg, notNullValue());
        assertThat(msg.size(), is(2));
        firstHalf.remove(new String(msg.data(), ZMQ.CHARSET));
    }
    assertThat(firstHalf.size(), is(0));
    // And then get the second batch
    for (int peer = 0; peer < services; ++peer) {
        Msg msg = ZMQ.recv(pull, 0);
        assertThat(msg, notNullValue());
        assertThat(msg.size(), is(2));
        secondHalf.remove(new String(msg.data(), ZMQ.CHARSET));
    }
    assertThat(secondHalf.size(), is(0));
    ZMQ.closeZeroLinger(pull);
    for (SocketBase sender : senders) {
        ZMQ.closeZeroLinger(sender);
    }
    // Wait for disconnects.
    ZMQ.msleep(100);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 38 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class DistTest method testMatch.

@Test
public void testMatch() {
    dist.attach(second);
    assertThat(dist.matching(), is(0));
    // simulate HWM
    second.write(new Msg());
    // mark the pipe as not active and not eligible
    dist.sendToAll(new Msg());
    dist.match(first);
    assertThat(dist.matching(), is(1));
    dist.match(second);
    // second pipe is not a matching one
    assertThat(dist.matching(), is(1));
}
Also used : Msg(zmq.Msg) Test(org.junit.Test)

Example 39 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class DistTest method testActivatedAfterReachingHWM.

@Test
public void testActivatedAfterReachingHWM() {
    dist.attach(second);
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(2));
    // simulate HWM
    second.write(new Msg());
    // mark the pipe as not active and not eligible
    dist.sendToAll(new Msg());
    // now, second pipe has been put out of the active and eligible ones
    assertThat(dist.eligible(), is(1));
    assertThat(dist.active(), is(1));
    // reactivate pipe having reached HWM
    dist.activated(second);
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(2));
}
Also used : Msg(zmq.Msg) Test(org.junit.Test)

Example 40 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class DistTest method testAttachedWhenSendingMultipartMessage.

@Test
public void testAttachedWhenSendingMultipartMessage() {
    Msg msg = new Msg();
    msg.setFlags(Msg.MORE);
    dist.sendToAll(msg);
    // no change in eligible/active states
    assertThat(dist.eligible(), is(1));
    assertThat(dist.active(), is(1));
    dist.attach(second);
    assertThat(dist.eligible(), is(2));
    // active should not have changed, as we are in the middle a sending a multi-part message
    assertThat(dist.active(), is(1));
    // no change in eligible/active states
    dist.activated(second);
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(1));
    dist.sendToAll(new Msg());
    // end of multi-part message
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(2));
}
Also used : Msg(zmq.Msg) Test(org.junit.Test)

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1