Search in sources :

Example 11 with Context

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

the class TestEvents method testEventBindFailed.

@Test
public void testEventBindFailed() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket helper = context.socket(SocketType.REP);
    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_BIND_FAILED));
    monitor.connect("inproc://monitor.socket");
    try {
        socket.bind("tcp://127.0.0.1:" + port);
    } catch (ZMQException ex) {
    }
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_BIND_FAILED, 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 12 with Context

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

the class TestEvents method testEventAccepted.

@Test
public void testEventAccepted() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(SocketType.REP);
    Socket monitor = context.socket(SocketType.PAIR);
    Socket helper = context.socket(SocketType.REQ);
    monitor.setReceiveTimeOut(100);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_ACCEPTED));
    monitor.connect("inproc://monitor.socket");
    int port = socket.bindToRandomPort("tcp://127.0.0.1");
    helper.connect("tcp://127.0.0.1:" + port);
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_ACCEPTED, 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 13 with Context

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

the class TestEvents method testEventListening.

@Test
public void testEventListening() {
    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_LISTENING));
    monitor.connect("inproc://monitor.socket");
    socket.bindToRandomPort("tcp://127.0.0.1");
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_LISTENING, 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 14 with Context

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

the class TestEvents method testEventDisconnected.

@Test
public void testEventDisconnected() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket socket = context.socket(SocketType.REP);
    Socket monitor = context.socket(SocketType.PAIR);
    Socket helper = context.socket(SocketType.REQ);
    monitor.setReceiveTimeOut(100);
    int port = socket.bindToRandomPort("tcp://127.0.0.1");
    helper.connect("tcp://127.0.0.1:" + port);
    assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_DISCONNECTED));
    monitor.connect("inproc://monitor.socket");
    zmq.ZMQ.sleep(1);
    helper.close();
    event = ZMQ.Event.recv(monitor);
    assertNotNull("No event was received", event);
    assertEquals(ZMQ.EVENT_DISCONNECTED, 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 15 with Context

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

the class TestEvents method testEventClosed.

@Test
public void testEventClosed() {
    Context context = ZMQ.context(1);
    Socket monitor = context.socket(SocketType.PAIR);
    try {
        ZMQ.Event event;
        Socket socket = context.socket(SocketType.REP);
        monitor.setReceiveTimeOut(100);
        socket.bindToRandomPort("tcp://127.0.0.1");
        assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CLOSED));
        monitor.connect("inproc://monitor.socket");
        socket.close();
        event = ZMQ.Event.recv(monitor);
        assertNotNull("No event was received", event);
        assertEquals(ZMQ.EVENT_CLOSED, event.getEvent());
    } finally {
        monitor.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