Search in sources :

Example 56 with SocketBase

use of zmq.SocketBase 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 57 with SocketBase

use of zmq.SocketBase 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 58 with SocketBase

use of zmq.SocketBase 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)

Example 59 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class XPubTest method testSetNoDrop.

@Test
public void testSetNoDrop() {
    Ctx ctx = ZMQ.createContext();
    SocketBase pub = null;
    try {
        pub = ctx.createSocket(ZMQ.ZMQ_XPUB);
        boolean rc = pub.setSocketOpt(ZMQ.ZMQ_XPUB_NODROP, 0);
        assertThat(rc, is(true));
    } finally {
        ZMQ.close(pub);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 60 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class DealerDealerTest method testIssue131.

@Test
public void testIssue131() throws IOException {
    Ctx ctx = ZMQ.createContext();
    assertThat(ctx, notNullValue());
    SocketBase sender = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(sender, notNullValue());
    final int port = Utils.findOpenPort();
    final String addr = "tcp://localhost:" + port;
    boolean rc = ZMQ.connect(sender, addr);
    assertThat(rc, is(true));
    byte[] sbuf = msg(255);
    int sent = ZMQ.send(sender, sbuf, 0);
    assertThat(sent, is(255));
    byte[] quit = { 'q' };
    sent = ZMQ.send(sender, quit, 0);
    assertThat(sent, is(1));
    ZMQ.close(sender);
    SocketBase receiver = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(receiver, notNullValue());
    rc = ZMQ.bind(receiver, addr);
    assertThat(rc, is(true));
    int nbytes = 0;
    do {
        Msg msg = ZMQ.recv(receiver, 0);
        nbytes = msg.size();
        System.out.println(msg);
    } while (nbytes != 1);
    ZMQ.close(receiver);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Aggregations

SocketBase (zmq.SocketBase)80 Ctx (zmq.Ctx)63 Test (org.junit.Test)55 Msg (zmq.Msg)37 ArrayList (java.util.ArrayList)10 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ZMQ (zmq.ZMQ)1 Event (zmq.ZMQ.Event)1