Search in sources :

Example 46 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PushPullSpecTest method testSpecPullFairQueueIn.

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

Example 47 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RouterProbeTest method testProbeRouter.

@Test
public void testProbeRouter() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    Ctx ctx = ZMQ.createContext();
    // Server socket will accept connections
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(server, notNullValue());
    boolean rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    // Create client and connect to server, doing a probe
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(client, notNullValue());
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_IDENTITY, "X");
    assertThat(rc, is(true));
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_PROBE_ROUTER, true);
    assertThat(rc, is(true));
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    // We expect an identity=X + empty message from client
    Msg msg = ZMQ.recv(server, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.get(0), is((byte) 'X'));
    msg = ZMQ.recv(server, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(0));
    // Send a message to client now
    int ret = ZMQ.send(server, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(ret, is(1));
    ret = ZMQ.send(server, "Hello", 0);
    assertThat(ret, is(5));
    msg = ZMQ.recv(client, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(5));
    // TODO DIFF V4 test should stop here, check the logic if we should receive payload in the previous message.
    msg = ZMQ.recv(client, 0);
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("Hello"));
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Shutdown
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 48 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestInvalidRep method testInvalidRep.

// Create REQ/ROUTER wiring.
@Test
public void testInvalidRep() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase routerSocket = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(routerSocket, notNullValue());
    SocketBase reqSocket = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(reqSocket, notNullValue());
    int linger = 0;
    int rc;
    ZMQ.setSocketOption(routerSocket, ZMQ.ZMQ_LINGER, linger);
    ZMQ.setSocketOption(reqSocket, ZMQ.ZMQ_LINGER, linger);
    boolean brc = ZMQ.bind(routerSocket, "inproc://hi");
    assertThat(brc, is(true));
    brc = ZMQ.connect(reqSocket, "inproc://hi");
    assertThat(brc, is(true));
    // Initial request.
    rc = ZMQ.send(reqSocket, "r", 0);
    assertThat(rc, is(1));
    // Receive the request.
    Msg addr;
    Msg bottom;
    Msg body;
    addr = ZMQ.recv(routerSocket, 0);
    int addrSize = addr.size();
    System.out.println("addrSize: " + addr.size());
    assertThat(addr.size() > 0, is(true));
    bottom = ZMQ.recv(routerSocket, 0);
    assertThat(bottom.size(), is(0));
    body = ZMQ.recv(routerSocket, 0);
    assertThat(body.size(), is(1));
    assertThat(body.data()[0], is((byte) 'r'));
    // Send invalid reply.
    rc = ZMQ.send(routerSocket, addr, 0);
    assertThat(rc, is(addrSize));
    // Send valid reply.
    rc = ZMQ.send(routerSocket, addr, ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(addrSize));
    rc = ZMQ.send(routerSocket, bottom, ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(0));
    rc = ZMQ.send(routerSocket, "b", 0);
    assertThat(rc, is(1));
    // Check whether we've got the valid reply.
    body = ZMQ.recv(reqSocket, 0);
    assertThat(body.size(), is(1));
    assertThat(body.data()[0], is((byte) 'b'));
    // Tear down the wiring.
    ZMQ.close(routerSocket);
    ZMQ.close(reqSocket);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 49 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestReqRelaxed method testReqRelaxed.

@Test
public void testReqRelaxed() throws Exception {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(req, notNullValue());
    boolean rc;
    rc = req.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, true);
    assertThat(rc, is(true));
    rc = req.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, true);
    assertThat(rc, is(true));
    rc = ZMQ.bind(req, "inproc://a");
    assertThat(rc, is(true));
    int services = 5;
    List<SocketBase> reps = new ArrayList<>();
    for (int idx = 0; idx < services; ++idx) {
        SocketBase rep = ctx.createSocket(ZMQ.ZMQ_REP);
        assertThat(rep, notNullValue());
        reps.add(rep);
        rc = req.setSocketOpt(ZMQ.ZMQ_RCVTIMEO, 500);
        assertThat(rc, is(true));
        rc = rep.connect("inproc://a");
        assertThat(rc, is(true));
    }
    // We have to give the connects time to finish otherwise the requests
    // will not properly round-robin. We could alternatively connect the
    // REQ sockets to the REP sockets.
    ZMQ.msleep(100);
    // Case 1: Second send() before a reply arrives in a pipe.
    // Send a request, ensure it arrives, don't send a reply
    Helper.sendSeq(req, "A", "B");
    Helper.recvSeq(reps.get(0), "A", "B");
    // Send another request on the REQ socket
    Helper.sendSeq(req, "C", "D");
    Helper.recvSeq(reps.get(1), "C", "D");
    // Send a reply to the first request - that should be discarded by the REQ
    Helper.sendSeq(reps.get(0), "WRONG");
    // Send the expected reply
    Helper.sendSeq(reps.get(1), "OK");
    Helper.recvSeq(req, "OK");
    // Another standard req-rep cycle, just to check
    Helper.sendSeq(req, "E");
    Helper.recvSeq(reps.get(2), "E");
    Helper.sendSeq(reps.get(2), "F", "G");
    Helper.recvSeq(req, "F", "G");
    // Case 2: Second send() after a reply is already in a pipe on the REQ.
    // Send a request, ensure it arrives, send a reply
    Helper.sendSeq(req, "H");
    Helper.recvSeq(reps.get(3), "H");
    Helper.sendSeq(reps.get(3), "BAD");
    // Wait for message to be there.
    ZMQ.msleep(100);
    // Without receiving that reply, send another request on the REQ socket
    Helper.sendSeq(req, "I");
    Helper.recvSeq(reps.get(4), "I");
    // Send the expected reply
    Helper.sendSeq(reps.get(4), "GOOD");
    Helper.recvSeq(req, "GOOD");
    // Case 3: Check issue #1690. Two send() in a row should not close the
    // communication pipes. For example pipe from req to rep[0] should not be
    // closed after executing Case 1. So rep[0] should be the next to receive,
    // not rep[1].
    Helper.sendSeq(req, "J");
    Helper.recvSeq(reps.get(0), "J");
    ZMQ.closeZeroLinger(req);
    for (SocketBase rep : reps) {
        ZMQ.closeZeroLinger(rep);
    }
    // Wait for disconnects.
    ZMQ.msleep(100);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 50 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestReqRelaxed method testIssueLibzmq1965.

@Test
public void testIssueLibzmq1965() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase req;
    boolean rc;
    // Case 4: Check issue #1695. As messages may pile up before a responder
    // is available, we check that responses to messages other than the last
    // sent one are correctly discarded by the REQ pipe
    // Setup REQ socket as client
    req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(req, notNullValue());
    rc = req.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, true);
    assertThat(rc, is(true));
    rc = req.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, true);
    assertThat(rc, is(true));
    rc = ZMQ.connect(req, "inproc://b");
    assertThat(rc, is(true));
    // Setup ROUTER socket as server but do not bind it just yet
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    // Send two requests
    Helper.sendSeq(req, "TO_BE_DISCARDED");
    Helper.sendSeq(req, "TO_BE_ANSWERED");
    // Bind server allowing it to receive messages
    rc = ZMQ.bind(router, "inproc://b");
    assertThat(rc, is(true));
    // Read the two messages and send them back as is
    bounce(router);
    bounce(router);
    Helper.recvSeq(req, "TO_BE_ANSWERED");
    ZMQ.closeZeroLinger(req);
    ZMQ.closeZeroLinger(router);
    ctx.terminate();
}
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