Search in sources :

Example 36 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestReqrepInproc method testReqrepInproc.

// Create REQ/ROUTER wiring.
@Test
public void testReqrepInproc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.bind(sb, "inproc://a");
    assertThat(brc, is(true));
    SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(sc, notNullValue());
    brc = ZMQ.connect(sc, "inproc://a");
    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 37 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RouterMandatoryTest method testRouterMandatory.

@Test
public void testRouterMandatory() throws Exception {
    int sent;
    boolean rc;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    rc = ZMQ.bind(router, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    // Sending a message to an unknown peer with the default setting
    // This will not report any error
    sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE);
    assertThat(sent, is(7));
    sent = ZMQ.send(router, "DATA", 0);
    assertThat(sent, is(4));
    // Send a message to an unknown peer with mandatory routing
    // This will fail
    int mandatory = 1;
    ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_MANDATORY, mandatory);
    // Send a message and check that it fails
    sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
    assertThat(sent, is(-1));
    assertThat(router.errno(), is(ZError.EHOSTUNREACH));
    // Create dealer called "X" and connect it to our router
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    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));
    // Get message from dealer to know when connection is ready
    int ret = ZMQ.send(dealer, "Hello", 0);
    assertThat(ret, is(5));
    Msg msg = ZMQ.recv(router, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.data()[0], is((byte) 'X'));
    // Send a message to connected dealer now
    // It should work
    sent = ZMQ.send(router, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(sent, is(1));
    sent = ZMQ.send(router, "Hello", 0);
    assertThat(sent, is(5));
    // 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 38 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RouterSpecTest method testDestroyQueueOnDisconnect.

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

Example 39 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RouterSpecTest method testFairQueueIn.

@Test
public void testFairQueueIn() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // SHALL receive incoming messages from its peers using a fair-queuing
        // strategy.
        fairQueueIn(ctx, bindAddress, ZMQ.ZMQ_ROUTER, ZMQ.ZMQ_DEALER);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 40 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestReqCorrelateRelaxed method overallSetup.

/**
 * Prepares sockets and runs actual tests.
 *
 * Doing it this way so order is guaranteed.
 *
 * @throws Exception
 */
@Test
public void overallSetup() throws Exception {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    boolean brc = ZMQ.bind(dealer, "inproc://a");
    assertThat(brc, is(true));
    SocketBase reqClient = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(reqClient, notNullValue());
    reqClient.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, 1);
    reqClient.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, 1);
    reqClient.setSocketOpt(ZMQ.ZMQ_RCVTIMEO, 100);
    brc = ZMQ.connect(reqClient, "inproc://a");
    assertThat(brc, is(true));
    // Test good path
    byte[] origRequestId = testReqSentFrames(dealer, reqClient);
    testReqRecvGoodRequestId(dealer, reqClient, origRequestId);
    // Test what happens when a bad request ID is sent back.
    origRequestId = testReqSentFrames(dealer, reqClient);
    testReqRecvBadRequestId(dealer, reqClient, origRequestId);
    ZMQ.close(reqClient);
    ZMQ.close(dealer);
    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