예제 #1
0
  @Test
  public void testTimeo() throws Exception {
    Ctx ctx = ZMQ.zmqInit(1);
    assertThat(ctx, notNullValue());

    SocketBase sb = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PULL);
    assertThat(sb, notNullValue());
    boolean rc = ZMQ.zmq_bind(sb, "inproc://timeout_test");
    assertThat(rc, is(true));

    //  Check whether non-blocking recv returns immediately.
    Msg msg = ZMQ.zmq_recv(sb, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, nullValue());

    //  Check whether recv timeout is honoured.
    int timeout = 500;
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    long watch = ZMQ.zmq_stopwatch_start();
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg, nullValue());
    long elapsed = ZMQ.zmq_stopwatch_stop(watch);
    assertThat(elapsed > 440000 && elapsed < 550000, is(true));

    //  Check whether connection during the wait doesn't distort the timeout.
    timeout = 2000;
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    Thread thread = new Thread(new Worker(ctx));
    thread.start();

    watch = ZMQ.zmq_stopwatch_start();
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg, nullValue());
    elapsed = ZMQ.zmq_stopwatch_stop(watch);
    assertThat(elapsed > 1900000 && elapsed < 2100000, is(true));
    thread.join();

    //  Check that timeouts don't break normal message transfer.
    SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PUSH);
    assertThat(sc, notNullValue());
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_SNDTIMEO, timeout);
    rc = ZMQ.zmq_connect(sc, "inproc://timeout_test");
    assertThat(rc, is(true));
    Msg smsg = new Msg("12345678ABCDEFGH12345678abcdefgh".getBytes(ZMQ.CHARSET));
    int r = ZMQ.zmq_send(sc, smsg, 0);
    assertThat(r, is(32));
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg.size(), is(32));

    ZMQ.zmq_close(sc);
    ZMQ.zmq_close(sb);
    ZMQ.zmq_term(ctx);
  }
예제 #2
0
 @Override
 public void run() {
   ZMQ.zmq_sleep(1);
   SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PUSH);
   assertThat(sc, notNullValue());
   boolean rc = ZMQ.zmq_connect(sc, "inproc://timeout_test");
   assertThat(rc, is(true));
   ZMQ.zmq_sleep(1);
   ZMQ.zmq_close(sc);
 }
예제 #3
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);
  }
예제 #4
0
    @Override
    public void run() {

      SocketBase s = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
      boolean rc = s.connect(monitor_addr);
      assert (rc);
      // Only some of the exceptional events could fire
      while (true) {
        ZMQ.Event event = ZMQ.Event.read(s);
        if (event == null && s.errno() == ZError.ETERM) break;
        assert (event != null);

        switch (event.event) {
            // listener specific
          case ZMQ.ZMQ_EVENT_LISTENING:
            // assert (strcmp(addr, data_->listening.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_LISTENING;
            break;
          case ZMQ.ZMQ_EVENT_ACCEPTED:
            // assert (strcmp(addr, data_->accepted.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_ACCEPTED;
            break;
            // connecter specific
          case ZMQ.ZMQ_EVENT_CONNECTED:
            // assert (strcmp(addr, data_->connected.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CONNECTED;
            break;
          case ZMQ.ZMQ_EVENT_CONNECT_DELAYED:
            // assert (strcmp(addr, data_->connect_delayed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CONNECT_DELAYED;
            break;
            // generic - either end of the socket
          case ZMQ.ZMQ_EVENT_CLOSE_FAILED:
            // assert (strcmp(addr, data_->close_failed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CLOSE_FAILED;
            break;
          case ZMQ.ZMQ_EVENT_CLOSED:
            // assert (strcmp(addr, data_->closed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CLOSED;
            break;
          case ZMQ.ZMQ_EVENT_DISCONNECTED:
            // assert (strcmp(addr, data_->disconnected.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_DISCONNECTED;
            break;
          default:
            // out of band / unexpected event
            assertTrue("Unkown Event " + event.event, true);
        }
      }
      s.close();
    }
예제 #5
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();
  }