Search in sources :

Example 51 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PushPullSpecTest method testSpecPushBlockOnSendNoPeers.

@Test
public void testSpecPushBlockOnSendNoPeers() 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 block on sending, or return a suitable error, when it has no
        // available peers.
        blockOnSendNoPeers(ctx, bindAddress, ZMQ.ZMQ_PUSH);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 52 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PushPullSpecTest method testSpecDestroyQueueOnDisconnect.

@Test
@Ignore
public void testSpecDestroyQueueOnDisconnect() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
    // PUSH and PULL: SHALL create this queue when a peer connects to it. If
    // this peer disconnects, the socket SHALL destroy its queue and SHALL
    // discard any messages it contains.
    // *** Test disabled until libzmq does this properly ***
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 53 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PubSubHwmTest method testDefaults.

private int testDefaults(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(sub, ZMQ.ZMQ_SUBSCRIBE, new byte[0]);
    assertThat(rc, is(true));
    // Send until we block
    int sendCount = 0;
    while (sendCount < msgCnt && ZMQ.send(pub, "", ZMQ.ZMQ_DONTWAIT) == 0) {
        ++sendCount;
    }
    // Now receive all sent messages
    int recvCount = 0;
    while (null != ZMQ.recv(sub, ZMQ.ZMQ_DONTWAIT)) {
        ++recvCount;
    }
    assertThat(sendCount, is(recvCount));
    // Clean up
    ZMQ.close(sub);
    ZMQ.close(pub);
    ZMQ.term(ctx);
    return recvCount;
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Example 54 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PubSubHwmTest method testResetHwm.

@Test
public void testResetHwm() {
    // hwm should apply to the messages that have already been received
    // with hwm 11024: send 9999 msg, receive 9999, send 1100, receive 1100
    int firstCount = 9999;
    int secondCount = 1100;
    int hwm = 11024;
    Ctx ctx = ZMQ.createContext();
    // Set up bind socket
    SocketBase pub = ctx.createSocket(ZMQ.ZMQ_PUB);
    boolean rc = ZMQ.setSocketOption(pub, ZMQ.ZMQ_SNDHWM, hwm);
    assertThat(rc, is(true));
    rc = ZMQ.bind(pub, "tcp://localhost:*");
    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);
    rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_RCVHWM, hwm);
    assertThat(rc, is(true));
    rc = ZMQ.connect(sub, host);
    assertThat(rc, is(true));
    rc = ZMQ.setSocketOption(sub, ZMQ.ZMQ_SUBSCRIBE, new byte[0]);
    assertThat(rc, is(true));
    ZMQ.sleep(1);
    // Send messages
    int sendCount = 0;
    while (sendCount < firstCount && ZMQ.send(pub, "1", ZMQ.ZMQ_DONTWAIT) == 1) {
        ++sendCount;
    }
    assertThat(sendCount, is(firstCount));
    ZMQ.msleep(500);
    // Now receive all sent messages
    int recvCount = 0;
    while (null != ZMQ.recv(sub, ZMQ.ZMQ_DONTWAIT)) {
        ++recvCount;
    }
    assertThat(recvCount, is(firstCount));
    ZMQ.msleep(100);
    sendCount = 0;
    while (sendCount < secondCount && ZMQ.send(pub, "2", ZMQ.ZMQ_DONTWAIT) == 1) {
        ++sendCount;
    }
    assertThat(sendCount, is(secondCount));
    ZMQ.msleep(200);
    // Now receive all sent messages
    recvCount = 0;
    while (null != ZMQ.recv(sub, ZMQ.ZMQ_DONTWAIT)) {
        ++recvCount;
    }
    assertThat(recvCount, is(secondCount));
    // Clean up
    ZMQ.close(sub);
    ZMQ.close(pub);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 55 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class SubTest method testSetNullOption.

@Test
public void testSetNullOption() {
    Ctx ctx = ZMQ.createContext();
    SocketBase pub = null;
    try {
        pub = ctx.createSocket(ZMQ.ZMQ_SUB);
        boolean rc = pub.setSocketOpt(ZMQ.ZMQ_SUBSCRIBE, null);
        assertThat(rc, is(false));
    } catch (IllegalArgumentException e) {
        assertThat(pub.errno.get(), is(ZError.EINVAL));
    } finally {
        ZMQ.close(pub);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

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