예제 #1
0
  @Test
  public void testPairInproc() {

    Ctx ctx = ZMQ.zmq_init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.zmq_bind(sb, "inproc://a");
    assertThat(brc, is(true));

    SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sc, notNullValue());
    brc = ZMQ.zmq_connect(sc, "inproc://a");
    assertThat(brc, is(true));

    TestHelper.bounce(sb, sc);

    //  Tear down the wiring.
    ZMQ.zmq_close(sb);
    ZMQ.zmq_close(sc);
    ZMQ.zmq_term(ctx);
  }
예제 #2
0
  @Test
  public void testMonitor() throws Exception {
    String addr = "tcp://127.0.0.1:5590";
    SocketMonitor[] threads = new SocketMonitor[3];
    //  Create the infrastructure
    Ctx ctx = ZMQ.zmq_init(1);
    assert (ctx != null);
    // set socket monitor
    SocketBase rep = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REP);
    assert (rep != null);
    try {
      ZMQ.zmq_socket_monitor(rep, addr, 0);
      assertTrue(false);
    } catch (IllegalArgumentException e) {
    }

    // REP socket monitor, all events
    boolean rc = ZMQ.zmq_socket_monitor(rep, "inproc://monitor.rep", ZMQ.ZMQ_EVENT_ALL);
    assertEquals(true, rc);

    threads[0] = new SocketMonitor(ctx, "inproc://monitor.rep");
    threads[0].start();

    rc = ZMQ.zmq_bind(rep, addr);
    assert (rc);

    SocketBase req = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REQ);
    assert (req != null);

    // REQ socket monitor, all events
    rc = ZMQ.zmq_socket_monitor(req, "inproc://monitor.req", ZMQ.ZMQ_EVENT_ALL);
    assertEquals(true, rc);

    threads[1] = new SocketMonitor(ctx, "inproc://monitor.req");
    threads[1].start();

    rc = ZMQ.zmq_connect(req, addr);
    assert (rc);

    // 2nd REQ socket
    SocketBase req2 = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REQ);
    assert (req2 != null);

    // 2nd REQ socket monitor, connected event only
    rc = ZMQ.zmq_socket_monitor(req2, "inproc://monitor.req2", ZMQ.ZMQ_EVENT_CONNECTED);
    assertEquals(true, rc);

    threads[2] = new SocketMonitor(ctx, "inproc://monitor.req2");
    threads[2].start();

    rc = ZMQ.zmq_connect(req2, addr);
    assert (rc);

    Helper.bounce(rep, req);

    // Allow a window for socket events as connect can be async
    ZMQ.zmq_sleep(1);

    // Close the REP socket
    ZMQ.zmq_close(rep);

    // Allow some time for detecting error states
    ZMQ.zmq_sleep(1);
    // Close the REQ socket
    ZMQ.zmq_close(req);
    // Close the 2nd REQ socket
    ZMQ.zmq_close(req2);

    // Allow for closed or disconnected events to bubble up
    ZMQ.zmq_sleep(1);

    ZMQ.zmq_term(ctx);

    // Expected REP socket events
    // We expect to at least observe these events
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_LISTENING) > 0);
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_ACCEPTED) > 0);
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_CLOSED) > 0);

    // Expected REQ socket events
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_CONNECTED) > 0);
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_DISCONNECTED) > 0);
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_CLOSED) > 0);

    // Expected 2nd REQ socket events
    assertTrue((threads[2].events & ZMQ.ZMQ_EVENT_CONNECTED) > 0);
    assertTrue((threads[2].events & ZMQ.ZMQ_EVENT_CLOSED) == 0);

    threads[0].join();
    threads[1].join();
    threads[2].join();
  }