Search in sources :

Example 31 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ProxySingleSocketTest method testProxySingleSocket.

@Test
public void testProxySingleSocket() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    // The main thread simply starts several clients and a server, and then
    // waits for the server to finish.
    Ctx ctx = ZMQ.createContext();
    SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(req, notNullValue());
    boolean rc = ZMQ.connect(req, host);
    assertThat(rc, is(true));
    // Control socket receives terminate command from main over inproc
    SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
    rc = ZMQ.bind(control, "inproc://control");
    assertThat(rc, is(true));
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.submit(new ServerTask(ctx, host));
    int ret = ZMQ.send(req, "msg1", 0);
    assertThat(ret, is(4));
    System.out.print(".");
    Msg msg = ZMQ.recv(req, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg1"));
    ret = ZMQ.send(req, "msg22", 0);
    assertThat(ret, is(5));
    System.out.print(".");
    msg = ZMQ.recv(req, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg22"));
    ret = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
    assertThat(ret, is(9));
    System.out.println(".");
    ZMQ.close(control);
    ZMQ.close(req);
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 32 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestPairIpc method testPairIpc.

// Create REQ/ROUTER wiring.
@Test(timeout = 5000)
public void testPairIpc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase pairBind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(pairBind, notNullValue());
    UUID random;
    do {
        random = UUID.randomUUID();
    } while (!ZMQ.bind(pairBind, "ipc:///tmp/tester/" + random.toString()));
    SocketBase pairConnect = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(pairConnect, notNullValue());
    boolean brc = ZMQ.connect(pairConnect, "ipc:///tmp/tester/" + random.toString());
    assertThat(brc, is(true));
    Helper.bounce(pairBind, pairConnect);
    // Tear down the wiring.
    ZMQ.close(pairBind);
    ZMQ.close(pairConnect);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) UUID(java.util.UUID) Test(org.junit.Test)

Example 33 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ConflateTest method test.

@Test
public void test() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.init(1);
    assert (ctx != null);
    SocketBase in = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
    assert (in != null);
    String host = "tcp://localhost:*";
    int conflate = 1;
    ZMQ.setSocketOption(in, ZMQ.ZMQ_CONFLATE, conflate);
    boolean rc = ZMQ.bind(in, host);
    assert (rc);
    SocketBase out = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
    assert (out != null);
    String ep = (String) ZMQ.getSocketOptionExt(in, ZMQ.ZMQ_LAST_ENDPOINT);
    rc = ZMQ.connect(out, ep);
    assert (rc);
    int messageCount = 20;
    for (int j = 0; j < messageCount; ++j) {
        int count = Helper.send(out, Integer.toString(j));
        assert (count > 0);
    }
    Thread.sleep(200);
    String recvd = Helper.recv(in);
    Assert.assertEquals("19", recvd);
    ZMQ.close(in);
    ZMQ.close(out);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 34 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class PollerTest method catchNotification.

@Test(timeout = 1000)
public void catchNotification() throws IOException, InterruptedException {
    CountDownLatch catched = new CountDownLatch(1);
    AtomicReference<Selector> selectorRef = new AtomicReference<>();
    Ctx ctx = new Ctx() {

        @Override
        public Selector createSelector() {
            selectorRef.set(super.createSelector());
            return selectorRef.get();
        }
    };
    ctx.setNotificationExceptionHandler((t, e) -> {
        if (e instanceof ClosedSelectorException) {
            catched.countDown();
        }
    });
    Poller poller = new Poller(ctx, "test");
    poller.start();
    selectorRef.get().close();
    catched.await();
}
Also used : Ctx(zmq.Ctx) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Selector(java.nio.channels.Selector) ClosedSelectorException(java.nio.channels.ClosedSelectorException) Test(org.junit.Test)

Example 35 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ProxyTcpTest method testProxyTcp.

@Test
public void testProxyTcp() throws Exception {
    int routerPort = Utils.findOpenPort();
    int dealerPort = Utils.findOpenPort();
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    Proxy mt = new Proxy(ctx, routerPort, dealerPort);
    mt.start();
    new Dealer(ctx, "A", dealerPort).start();
    // new Dealer(ctx, "B", dealerPort).start();
    ZMQ.sleep(1);
    Thread client = new Client(routerPort);
    client.start();
    client.join();
    ZMQ.term(ctx);
}
Also used : 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