Search in sources :

Example 26 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class PubTest method testRecv.

@Test
public void testRecv() {
    Ctx ctx = ZMQ.createContext();
    SocketBase pub = null;
    try {
        pub = ctx.createSocket(ZMQ.ZMQ_PUB);
        pub.recv(ZMQ.ZMQ_DONTWAIT);
        Assert.fail("Pub cannot receive message");
    } catch (UnsupportedOperationException e) {
        assertThat(ctx.errno().get(), is(ZError.ENOTSUP));
    } finally {
        ZMQ.close(pub);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 27 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class PubTest method testHasIn.

@Test
public void testHasIn() {
    Ctx ctx = ZMQ.createContext();
    SocketBase pub = null;
    try {
        pub = ctx.createSocket(ZMQ.ZMQ_PUB);
        int events = pub.getSocketOpt(ZMQ.ZMQ_EVENTS);
        assertThat(events, is(2));
    } finally {
        ZMQ.close(pub);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 28 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class RouterMandatoryTest method testRouterMandatoryHwm.

@Test
public void testRouterMandatoryHwm() throws Exception {
    boolean rc;
    System.out.print("Starting router mandatory HWM test");
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_MANDATORY, true);
    ZMQ.setSocketOption(router, ZMQ.ZMQ_SNDHWM, 1);
    ZMQ.setSocketOption(router, ZMQ.ZMQ_LINGER, 1);
    rc = ZMQ.bind(router, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    // Create dealer called "X" and connect it to our router, configure HWM
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    ZMQ.setSocketOption(dealer, ZMQ.ZMQ_RCVHWM, 1);
    ZMQ.setSocketOption(dealer, ZMQ.ZMQ_IDENTITY, "X");
    String host = (String) ZMQ.getSocketOptionExt(router, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    rc = ZMQ.connect(dealer, host);
    assertThat(rc, is(true));
    System.out.print(".");
    // Get message from dealer to know when connection is ready
    int ret = ZMQ.send(dealer, "Hello", 0);
    assertThat(ret, is(5));
    System.out.print(".");
    Msg msg = ZMQ.recv(router, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(msg.data()[0], is((byte) 'X'));
    int i = 0;
    for (; i < 100000; ++i) {
        ret = ZMQ.send(router, "X", ZMQ.ZMQ_DONTWAIT | ZMQ.ZMQ_SNDMORE);
        if (ret == -1 && router.errno() == ZError.EAGAIN) {
            break;
        }
        assertThat(ret, is(1));
        ret = ZMQ.send(router, new byte[BUF_SIZE], BUF_SIZE, ZMQ.ZMQ_DONTWAIT);
        assertThat(ret, is(BUF_SIZE));
    }
    System.out.print(".");
    // This should fail after one message but kernel buffering could
    // skew results
    assertThat(i < 10, is(true));
    ZMQ.sleep(1);
    // Send second batch of messages
    for (; i < 100000; ++i) {
        ret = ZMQ.send(router, "X", ZMQ.ZMQ_DONTWAIT | ZMQ.ZMQ_SNDMORE);
        if (ret == -1 && router.errno() == ZError.EAGAIN) {
            break;
        }
        assertThat(ret, is(1));
        ret = ZMQ.send(router, new byte[BUF_SIZE], BUF_SIZE, ZMQ.ZMQ_DONTWAIT);
        assertThat(ret, is(BUF_SIZE));
    }
    System.out.print(".");
    // This should fail after two messages but kernel buffering could
    // skew results
    assertThat(i < 20, is(true));
    System.out.println("Done sending messages.");
    // Clean up.
    ZMQ.close(router);
    ZMQ.close(dealer);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 29 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestReqrepIpc method testReqrepIpc.

@Test
public void testReqrepIpc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.bind(sb, "ipc:///tmp/tester2");
    assertThat(brc, is(true));
    SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(sc, notNullValue());
    brc = ZMQ.connect(sc, "ipc:///tmp/tester2");
    assertThat(brc, is(true));
    Helper.bounce(sb, sc);
    // Tear down the wiring.
    ZMQ.close(sb);
    ZMQ.close(sc);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 30 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestRouterMandatory method testRouterMandatory.

@Test
public void testRouterMandatory() throws Exception {
    int rc;
    boolean brc;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sa = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    ZMQ.setSocketOption(sa, ZMQ.ZMQ_SNDHWM, 1);
    assertThat(sa, notNullValue());
    brc = ZMQ.bind(sa, "tcp://127.0.0.1:*");
    assertThat(brc, is(true));
    // Sending a message to an unknown peer with the default setting
    rc = ZMQ.send(sa, "UNKNOWN", ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(7));
    rc = ZMQ.send(sa, "DATA", 0);
    assertThat(rc, is(4));
    int mandatory = 1;
    // Set mandatory routing on socket
    ZMQ.setSocketOption(sa, ZMQ.ZMQ_ROUTER_MANDATORY, mandatory);
    // Send a message and check that it fails
    rc = ZMQ.send(sa, "UNKNOWN", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
    assertThat(rc, is(-1));
    assertThat(sa.errno(), is(ZError.EHOSTUNREACH));
    // Create a valid socket
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(sb, notNullValue());
    ZMQ.setSocketOption(sb, ZMQ.ZMQ_RCVHWM, 1);
    ZMQ.setSocketOption(sb, ZMQ.ZMQ_IDENTITY, "X");
    String host = (String) ZMQ.getSocketOptionExt(sa, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    brc = ZMQ.connect(sb, host);
    // wait until connect
    Thread.sleep(1000);
    // make it full and check that it fails
    rc = ZMQ.send(sa, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(1));
    rc = ZMQ.send(sa, "DATA1", 0);
    assertThat(rc, is(5));
    rc = ZMQ.send(sa, "X", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
    if (rc == 1) {
        // the first frame has been sent
        rc = ZMQ.send(sa, "DATA2", 0);
        assertThat(rc, is(5));
        // send more
        rc = ZMQ.send(sa, "X", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
    }
    assertThat(rc, is(-1));
    assertThat(sa.errno(), is(ZError.EAGAIN));
    // Clean up.
    ZMQ.close(sa);
    ZMQ.close(sb);
    ZMQ.term(ctx);
}
Also used : 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