Search in sources :

Example 16 with Context

use of org.zeromq.ZMQ.Context in project jeromq by zeromq.

the class TestEvents method testEventMonitorStopped.

@Test
public void testEventMonitorStopped() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(SocketType.REP);
    Socket monitor = context.socket(SocketType.PAIR);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_MONITOR_STOPPED));
    monitor.connect("inproc://monitor.socket");
    socket.monitor(null, 0);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_MONITOR_STOPPED, event.getEvent());
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 17 with Context

use of org.zeromq.ZMQ.Context in project jeromq by zeromq.

the class TestEvents method testEventConnectDelayed.

@Test
public void testEventConnectDelayed() throws IOException {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(SocketType.REP);
    Socket monitor = context.socket(SocketType.PAIR);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CONNECT_DELAYED));
    monitor.connect("inproc://monitor.socket");
    int randomPort = Utils.findOpenPort();
    socket.connect("tcp://127.0.0.1:" + randomPort);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_CONNECT_DELAYED, event.getEvent());
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 18 with Context

use of org.zeromq.ZMQ.Context in project jeromq by zeromq.

the class TestProxy method testProxy.

@Test
public void testProxy() throws Exception {
    String frontend = "tcp://localhost:" + Utils.findOpenPort();
    String backend = "tcp://localhost:" + Utils.findOpenPort();
    String controlEndpoint = "tcp://localhost:" + Utils.findOpenPort();
    Proxy proxy = new Proxy(frontend, backend, controlEndpoint);
    proxy.start();
    ExecutorService executor = Executors.newFixedThreadPool(4);
    Dealer d1 = new Dealer("Dealer-A", backend);
    Dealer d2 = new Dealer("Dealer-B", backend);
    executor.submit(d1);
    executor.submit(d2);
    Thread.sleep(1000);
    Client c1 = new Client("Client-X", frontend);
    Client c2 = new Client("Client-Y", frontend);
    executor.submit(c1);
    executor.submit(c2);
    executor.shutdown();
    executor.awaitTermination(40, TimeUnit.SECONDS);
    Context ctx = ZMQ.context(1);
    Socket control = ctx.socket(SocketType.PAIR);
    control.connect(controlEndpoint);
    control.send(ZMQ.PROXY_TERMINATE);
    proxy.join();
    control.close();
    ctx.close();
    assertThat(c1.result.get(), is(true));
    assertThat(c2.result.get(), is(true));
    assertThat(d1.result.get(), is(true));
    assertThat(d2.result.get(), is(true));
    assertThat(proxy.result.get(), is(true));
}
Also used : Context(org.zeromq.ZMQ.Context) ExecutorService(java.util.concurrent.ExecutorService) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 19 with Context

use of org.zeromq.ZMQ.Context in project jeromq by zeromq.

the class TestZMQ method testSocketUnbind.

@Test
public void testSocketUnbind() {
    Context context = ZMQ.context(1);
    Socket push = context.socket(SocketType.PUSH);
    Socket pull = context.socket(SocketType.PULL);
    boolean rc = pull.setReceiveTimeOut(50);
    assertThat(rc, is(true));
    // rc = push.setImmediate(false);
    // assertThat(rc, is(true));
    // rc = pull.setImmediate(false);
    // assertThat(rc, is(true));
    int port = push.bindToRandomPort("tcp://127.0.0.1");
    rc = pull.connect("tcp://127.0.0.1:" + port);
    assertThat(rc, is(true));
    System.out.println("Connecting socket to unbind on port " + port);
    byte[] data = "ABC".getBytes();
    rc = push.send(data);
    assertThat(rc, is(true));
    assertArrayEquals(data, pull.recv());
    rc = pull.unbind("tcp://127.0.0.1:" + port);
    assertThat(rc, is(true));
    rc = push.send(data);
    assertThat(rc, is(true));
    assertNull(pull.recv());
    push.close();
    pull.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 20 with Context

use of org.zeromq.ZMQ.Context in project jeromq by zeromq.

the class TestZMQ method testSocketSendRecvBinaryPicture.

@Test
public void testSocketSendRecvBinaryPicture() {
    Context context = ZMQ.context(1);
    Socket push = context.socket(SocketType.PUSH);
    Socket pull = context.socket(SocketType.PULL);
    boolean rc = pull.setReceiveTimeOut(50);
    assertThat(rc, is(true));
    int port = push.bindToRandomPort("tcp://127.0.0.1");
    rc = pull.connect("tcp://127.0.0.1:" + port);
    assertThat(rc, is(true));
    String picture = "1248sScfm";
    ZMsg msg = new ZMsg();
    msg.add("Hello");
    msg.add("World");
    rc = push.sendBinaryPicture(picture, 255, 65535, 429496729, Long.MAX_VALUE, "Hello World", "Hello cruel World!", "ABC".getBytes(ZMQ.CHARSET), new ZFrame("My frame"), msg);
    assertThat(rc, is(true));
    Object[] objects = pull.recvBinaryPicture(picture);
    assertThat(objects[0], is(equalTo(255)));
    assertThat(objects[1], is(equalTo(65535)));
    assertThat(objects[2], is(equalTo(429496729)));
    assertThat(objects[3], is(equalTo(Long.MAX_VALUE)));
    assertThat(objects[4], is(equalTo("Hello World")));
    assertThat(objects[5], is(equalTo("Hello cruel World!")));
    assertThat(objects[6], is(equalTo("ABC".getBytes(zmq.ZMQ.CHARSET))));
    assertThat(objects[7], is(equalTo(new ZFrame("My frame"))));
    ZMsg expectedMsg = new ZMsg();
    expectedMsg.add("Hello");
    expectedMsg.add("World");
    assertThat(objects[8], is(equalTo(expectedMsg)));
    push.close();
    pull.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Aggregations

Context (org.zeromq.ZMQ.Context)35 Test (org.junit.Test)33 Socket (org.zeromq.ZMQ.Socket)33 IOException (java.io.IOException)5 ByteBuffer (java.nio.ByteBuffer)4 CharacterCodingException (java.nio.charset.CharacterCodingException)4 ExecutorService (java.util.concurrent.ExecutorService)3 ZFrame (org.zeromq.ZFrame)2 ZMsg (org.zeromq.ZMsg)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 Ignore (org.junit.Ignore)1