Search in sources :

Example 6 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ReqSpecTest method testSpecMessageFormat.

@Test
public void testSpecMessageFormat() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // The request and reply messages SHALL have this format on the wire:
        // * A delimiter, consisting of an empty frame, added by the REQ socket.
        // * One or more data frames, comprising the message visible to the
        // application.
        messageFormat(ctx, bindAddress, ZMQ.ZMQ_REQ, ZMQ.ZMQ_ROUTER);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 7 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ReqSpecTest method testSpecRoundRobinOut.

@Test
public void testSpecRoundRobinOut() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // SHALL route outgoing messages to connected peers using a round-robin
        // strategy.
        roundRobinOut(ctx, bindAddress, ZMQ.ZMQ_REQ, ZMQ.ZMQ_REP);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 8 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PushPullSpecTest method testSpecPushRoundRobinOut.

@Test
public void testSpecPushRoundRobinOut() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // PUSH: SHALL route outgoing messages to connected peers using a
        // round-robin strategy.
        roundRobinOut(ctx, bindAddress, ZMQ.ZMQ_PUSH, ZMQ.ZMQ_PULL);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 9 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PubSubHwmTest method assertHwmSubscription.

private void assertHwmSubscription(int subscribed, String endpoint) {
    int allSubscriptions = 30_000;
    int subscriptionNotSent = allSubscriptions - 1;
    Ctx ctx = ZMQ.createContext();
    SocketBase pub = ctx.createSocket(ZMQ.ZMQ_PUB);
    assertThat(pub, notNullValue());
    boolean rc = ZMQ.bind(pub, endpoint);
    assertThat(rc, is(true));
    String host = (String) ZMQ.getSocketOptionExt(pub, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    // Set up connect socket
    SocketBase sub = ctx.createSocket(ZMQ.ZMQ_SUB);
    assertThat(sub, notNullValue());
    rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_RCVTIMEO, 100);
    assertThat(rc, is(true));
    // send a lot of subscriptions, far beyond the HWM
    int idx = 0;
    for (; idx < allSubscriptions; ++idx) {
        rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_SUBSCRIBE, Wire.putUInt32(idx));
        // at some point, sending will trigger the HWM and subscription will be dropped
        assertThat(rc, is(true));
    }
    rc = ZMQ.connect(sub, host);
    assertThat(rc, is(true));
    ZMQ.msleep(500);
    // Send messages
    int sent = ZMQ.send(pub, Wire.putUInt32(1), 0);
    assertThat(sent, is(4));
    sent = ZMQ.send(pub, Wire.putUInt32(subscribed), 0);
    assertThat(sent, is(4));
    sent = ZMQ.send(pub, Wire.putUInt32(subscriptionNotSent), 0);
    assertThat(sent, is(4));
    ZMQ.msleep(500);
    Msg msg = ZMQ.recv(sub, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is(Wire.putUInt32(1)));
    msg = ZMQ.recv(sub, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is(Wire.putUInt32(subscribed)));
    msg = ZMQ.recv(sub, 0);
    assertThat(msg, nullValue());
    // Clean up
    ZMQ.close(sub);
    ZMQ.close(pub);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Example 10 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PubSubHwmTest method testBlocking.

private int testBlocking(int sendHwm, int msgCnt) {
    Ctx ctx = ZMQ.createContext();
    // Set up bind socket
    SocketBase pub = ctx.createSocket(ZMQ.ZMQ_PUB);
    boolean rc = ZMQ.bind(pub, "inproc://a");
    assertThat(rc, is(true));
    // Set up connect socket
    SocketBase sub = ctx.createSocket(ZMQ.ZMQ_SUB);
    rc = ZMQ.connect(sub, "inproc://a");
    assertThat(rc, is(true));
    // set a hwm on publisher
    rc = ZMQ.setSocketOption(pub, ZMQ.ZMQ_SNDHWM, sendHwm);
    assertThat(rc, is(true));
    rc = ZMQ.setSocketOption(pub, ZMQ.ZMQ_XPUB_NODROP, true);
    assertThat(rc, is(true));
    rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_SUBSCRIBE, new byte[0]);
    assertThat(rc, is(true));
    // Send until we block
    int sendCount = 0;
    int recvCount = 0;
    while (sendCount < msgCnt) {
        int ret = ZMQ.send(pub, "", ZMQ.ZMQ_DONTWAIT);
        if (ret == 0) {
            ++sendCount;
        } else if (ret == -1) {
            assertThat(pub.errno(), is(ZError.EAGAIN));
            recvCount += receive(sub);
            assertThat(sendCount, is(recvCount));
        }
    }
    recvCount += receive(sub);
    // Clean up
    ZMQ.close(sub);
    ZMQ.close(pub);
    ZMQ.term(ctx);
    return recvCount;
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Aggregations

Ctx (zmq.Ctx)81 Test (org.junit.Test)73 SocketBase (zmq.SocketBase)63 Msg (zmq.Msg)31 AbstractSpecTest (zmq.socket.AbstractSpecTest)16 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 Selector (java.nio.channels.Selector)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1