Exemplo n.º 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);
  }
Exemplo n.º 2
0
  public static void bounce(SocketBase sb, SocketBase sc) {
    byte[] content = "12345678ABCDEFGH12345678abcdefgh".getBytes();

    //  Send the message.
    int rc = ZMQ.zmq_send(sc, content, 32, ZMQ.ZMQ_SNDMORE);
    assert (rc == 32);
    rc = ZMQ.zmq_send(sc, content, 32, 0);
    assertThat(rc, is(32));

    //  Bounce the message back.
    Msg msg;
    msg = ZMQ.zmq_recv(sb, 0);
    assert (msg.size() == 32);
    long rcvmore = ZMQ.zmq_getsockopt(sb, ZMQ.ZMQ_RCVMORE);
    assert (rcvmore == 1);
    msg = ZMQ.zmq_recv(sb, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sb, ZMQ.ZMQ_RCVMORE);
    assert (rcvmore == 0);
    rc = ZMQ.zmq_send(sb, new Msg(msg), ZMQ.ZMQ_SNDMORE);
    assert (rc == 32);
    rc = ZMQ.zmq_send(sb, new Msg(msg), 0);
    assert (rc == 32);

    //  Receive the bounced message.
    msg = ZMQ.zmq_recv(sc, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sc, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(1L));
    msg = ZMQ.zmq_recv(sc, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sc, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(0L));
    //  Check whether the message is still the same.
    // assert (memcmp (buf2, content, 32) == 0);
  }