Search in sources :

Example 31 with Context

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

the class TestEvents method testEventConnected.

@Test
public void testEventConnected() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket helper = context.socket(SocketType.REQ);
    int port = helper.bindToRandomPort("tcp://127.0.0.1");
    Socket socket = context.socket(SocketType.REP);
    Socket monitor = context.socket(SocketType.PAIR);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CONNECTED));
    monitor.connect("inproc://monitor.socket");
    socket.connect("tcp://127.0.0.1:" + port);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_CONNECTED, event.getEvent());
    helper.close();
    socket.close();
    monitor.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 32 with Context

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

the class TestEvents method testEventConnectRetried.

@Test
public void testEventConnectRetried() throws InterruptedException, 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_RETRIED));
    monitor.connect("inproc://monitor.socket");
    int randomPort = Utils.findOpenPort();
    socket.connect("tcp://127.0.0.1:" + randomPort);
    // on windows, this is required, otherwise test fails
    Thread.sleep(1000L);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_CONNECT_RETRIED, 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 33 with Context

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

the class ZPictureTest method testSocketSendRecvPicture.

@Test
public void testSocketSendRecvPicture() {
    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 = "i1248sbcfzm";
    ZMsg msg = new ZMsg();
    msg.add("Hello");
    msg.add("World");
    rc = pic.sendPicture(push, picture, -456, 255, 65535, 429496729, Long.MAX_VALUE, "Hello World", "ABC".getBytes(ZMQ.CHARSET), "DEF".getBytes(ZMQ.CHARSET), new ZFrame("My frame"), msg);
    assertThat(rc, is(true));
    Object[] objects = pic.recvPicture(pull, picture);
    assertThat(objects[0], is(-456));
    assertThat(objects[1], is(255));
    assertThat(objects[2], is(65535));
    assertThat(objects[3], is(429496729));
    assertThat(objects[4], is(Long.MAX_VALUE));
    assertThat(objects[5], is("Hello World"));
    assertThat(objects[6], is("ABC".getBytes(zmq.ZMQ.CHARSET)));
    assertThat(objects[7], is("DEF".getBytes(zmq.ZMQ.CHARSET)));
    assertThat(objects[8], is(equalTo(new ZFrame("My frame"))));
    assertThat(objects[9], is(equalTo(new ZFrame((byte[]) null))));
    ZMsg expectedMsg = new ZMsg();
    expectedMsg.add("Hello");
    expectedMsg.add("World");
    assertThat(objects[10], is(equalTo(expectedMsg)));
    push.close();
    pull.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) ZFrame(org.zeromq.ZFrame) ZMsg(org.zeromq.ZMsg) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 34 with Context

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

the class TestZMQ method testBindSameAddress.

@Test(expected = ZMQException.class)
public void testBindSameAddress() throws IOException {
    int port = Utils.findOpenPort();
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket socket1 = context.socket(SocketType.REQ);
    ZMQ.Socket socket2 = context.socket(SocketType.REQ);
    socket1.bind("tcp://*:" + port);
    try {
        socket2.bind("tcp://*:" + port);
        fail("Exception not thrown");
    } catch (ZMQException e) {
        assertEquals(e.getErrorCode(), ZMQ.Error.EADDRINUSE.getCode());
        throw e;
    } finally {
        socket1.close();
        socket2.close();
        context.term();
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) Test(org.junit.Test)

Example 35 with Context

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

the class TestZMQ method testSocketSendRecvArray.

@Test
public void testSocketSendRecvArray() {
    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));
    byte[] data = "ABC".getBytes(ZMQ.CHARSET);
    rc = push.sendMore("DEF");
    assertThat(rc, is(true));
    rc = push.send(data, 0, data.length - 1, 0);
    assertThat(rc, is(true));
    byte[] recvd = pull.recv();
    assertThat(recvd, is("DEF".getBytes(ZMQ.CHARSET)));
    byte[] datb = new byte[2];
    int received = pull.recv(datb, 0, datb.length, 0);
    assertThat(received, is(2));
    assertThat(datb, is("AB".getBytes(ZMQ.CHARSET)));
    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