Search in sources :

Example 6 with Context

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

the class TestZMQ method testEventConnected.

@Test
public void testEventConnected() {
    Context context = ZMQ.context(1);
    ZMQ.Event event;
    Socket helper = context.socket(ZMQ.REQ);
    int port = helper.bindToRandomPort("tcp://127.0.0.1");
    Socket socket = context.socket(ZMQ.REP);
    Socket monitor = context.socket(ZMQ.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 7 with Context

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

the class ZPictureTest 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 = "1248sSbcfm";
    ZMsg msg = new ZMsg();
    msg.add("Hello");
    msg.add("World");
    rc = pic.sendBinaryPicture(push, picture, 255, 65535, 429496729, Long.MAX_VALUE, "Hello World", "Hello cruel World!", "ABC".getBytes(ZMQ.CHARSET), "DEF".getBytes(ZMQ.CHARSET), new ZFrame("My frame"), msg);
    assertThat(rc, is(true));
    Object[] objects = pic.recvBinaryPicture(pull, picture);
    assertThat(objects[0], is(255));
    assertThat(objects[1], is(65535));
    assertThat(objects[2], is(429496729));
    assertThat(objects[3], is(Long.MAX_VALUE));
    assertThat(objects[4], is("Hello World"));
    assertThat(objects[5], is("Hello cruel 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"))));
    ZMsg expectedMsg = new ZMsg();
    expectedMsg.add("Hello");
    expectedMsg.add("World");
    assertThat(objects[9], 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 8 with Context

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

the class TestZMQ method testBindInprocSameAddress.

@Test(expected = ZMQException.class)
public void testBindInprocSameAddress() {
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket socket1 = context.socket(SocketType.REQ);
    ZMQ.Socket socket2 = context.socket(SocketType.REQ);
    socket1.bind("inproc://address.already.in.use");
    socket2.bind("inproc://address.already.in.use");
    assertThat(socket2.errno(), is(ZError.EADDRINUSE));
    socket1.close();
    socket2.close();
    context.term();
}
Also used : Socket(org.zeromq.ZMQ.Socket) Context(org.zeromq.ZMQ.Context) Test(org.junit.Test)

Example 9 with Context

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

the class TestZMQ 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 = "1248sbfzm";
    ZMsg msg = new ZMsg();
    msg.add("Hello");
    msg.add("World");
    rc = push.sendPicture(picture, 255, 65535, 429496729, Long.MAX_VALUE, "Hello World", "ABC".getBytes(ZMQ.CHARSET), new ZFrame("My frame"), msg);
    assertThat(rc, is(true));
    Object[] objects = pull.recvPicture(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("ABC".getBytes(zmq.ZMQ.CHARSET))));
    assertThat(objects[6], is(equalTo(new ZFrame("My frame"))));
    assertThat(objects[7], is(equalTo(new ZFrame())));
    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)

Example 10 with Context

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

the class DealerDealerTest method testIssue335.

@Test
@Ignore
public void testIssue335() throws InterruptedException, IOException {
    final boolean verbose = false;
    final int messagesCount = 1000;
    final ZMQ.Context context = ZMQ.context(1);
    final Deque<String> queue = new LinkedBlockingDeque<>();
    final String host = "tcp://localhost:" + Utils.findOpenPort();
    final Runnable server = new Runnable() {

        @Override
        public void run() {
            final ZMQ.Socket server = context.socket(SocketType.DEALER);
            server.bind(host);
            int msg = messagesCount;
            while (msg-- > 0) {
                final String payload = Integer.toString(msg);
                queue.add(payload);
                if (!server.send(payload)) {
                    System.out.println("Send failed");
                }
                zmq.ZMQ.msleep(10);
            }
            server.close();
        }
    };
    final Client client = new Client(context, verbose, host, messagesCount, queue);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(server);
    executor.submit(client);
    long start = System.currentTimeMillis();
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    long end = System.currentTimeMillis();
    System.out.println("Done in  " + (end - start) + " millis.");
    assertThat(client.missed, is(0));
    assertThat(client.reconnects, is(messagesCount / 10));
    assertThat(client.count, is(client.received));
    context.close();
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Context(org.zeromq.ZMQ.Context) ExecutorService(java.util.concurrent.ExecutorService) Ignore(org.junit.Ignore) 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