Example usage for io.netty.handler.timeout IdleStateEvent FIRST_READER_IDLE_STATE_EVENT

List of usage examples for io.netty.handler.timeout IdleStateEvent FIRST_READER_IDLE_STATE_EVENT

Introduction

In this page you can find the example usage for io.netty.handler.timeout IdleStateEvent FIRST_READER_IDLE_STATE_EVENT.

Prototype

IdleStateEvent FIRST_READER_IDLE_STATE_EVENT

To view the source code for io.netty.handler.timeout IdleStateEvent FIRST_READER_IDLE_STATE_EVENT.

Click Source Link

Usage

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    KeyValueHandler testHandler = new KeyValueHandler(mock(AbstractEndpoint.class), eventSink, requestQueue,
            false) {/*w w w.  ja v a2s.c om*/

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected CoreEnvironment env() {
            return DefaultCoreEnvironment.create();
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(testHandler);

    //test idle event triggers a k/v keepAlive request and hook is called
    testHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(requestQueue.peek() instanceof KeyValueHandler.KeepAliveRequest);
    KeyValueHandler.KeepAliveRequest keepAliveRequest = (KeyValueHandler.KeepAliveRequest) requestQueue.peek();

    //test responding to the request with memcached response is interpreted into a KeepAliveResponse, hook is called
    DefaultFullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {},
            Unpooled.EMPTY_BUFFER);
    response.setOpaque(keepAliveRequest.opaque());
    response.setStatus(KeyValueStatus.ERR_NO_MEM.code());

    channel.writeInbound(response);
    KeyValueHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(KeyValueHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.OUT_OF_MEMORY, keepAliveResponse.status());
}

From source file:com.couchbase.client.core.endpoint.query.QueryHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    QueryHandler testHandler = new QueryHandler(endpoint, responseRingBuffer, queue, false) {
        @Override//from   w ww .jav a  2s . c o  m
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(testHandler);

    //test idle event triggers a query keepAlive request and hook is called
    testHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(queue.peek() instanceof QueryHandler.KeepAliveRequest);
    QueryHandler.KeepAliveRequest keepAliveRequest = (QueryHandler.KeepAliveRequest) queue.peek();

    //test responding to the request with http response is interpreted into a KeepAliveResponse and hook is called
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    LastHttpContent responseEnd = new DefaultLastHttpContent();
    channel.writeInbound(response, responseEnd);
    QueryHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(QueryHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    channel.pipeline().remove(testHandler);
    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.NOT_EXISTS, keepAliveResponse.status());
    assertEquals(0, responseEnd.refCnt());
}

From source file:com.couchbase.client.core.endpoint.search.SearchHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    //similar test to query
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    SearchHandler searchKeepAliveHandler = new SearchHandler(endpoint, responseRingBuffer, queue, false,
            false) {/*from  w ww. j  ava2s  .c  o  m*/
        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(searchKeepAliveHandler);

    //test idle event triggers a query keepAlive request and hook is called
    searchKeepAliveHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(queue.peek() instanceof SearchHandler.KeepAliveRequest);
    SearchHandler.KeepAliveRequest keepAliveRequest = (SearchHandler.KeepAliveRequest) queue.peek();

    //test responding to the request with http response is interpreted into a KeepAliveResponse and hook is called
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    LastHttpContent responseEnd = new DefaultLastHttpContent();
    channel.writeInbound(response, responseEnd);
    SearchHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(SearchHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    channel.pipeline().remove(searchKeepAliveHandler);
    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.NOT_EXISTS, keepAliveResponse.status());
    assertEquals(0, responseEnd.refCnt());
}

From source file:com.couchbase.client.core.endpoint.view.ViewHandlerTest.java

License:Apache License

@Test
public void shouldFireKeepAlive() throws Exception {
    final AtomicInteger keepAliveEventCounter = new AtomicInteger();
    final AtomicReference<ChannelHandlerContext> ctxRef = new AtomicReference();

    ViewHandler testHandler = new ViewHandler(endpoint, responseRingBuffer, queue, false) {
        @Override/*from w w  w  . j a  v a2  s.  c o  m*/
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            super.channelRegistered(ctx);
            ctxRef.compareAndSet(null, ctx);
        }

        @Override
        protected void onKeepAliveFired(ChannelHandlerContext ctx, CouchbaseRequest keepAliveRequest) {
            assertEquals(1, keepAliveEventCounter.incrementAndGet());
        }

        @Override
        protected void onKeepAliveResponse(ChannelHandlerContext ctx, CouchbaseResponse keepAliveResponse) {
            assertEquals(2, keepAliveEventCounter.incrementAndGet());
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(testHandler);

    //test idle event triggers a view keepAlive request and hook is called
    testHandler.userEventTriggered(ctxRef.get(), IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    assertEquals(1, keepAliveEventCounter.get());
    assertTrue(queue.peek() instanceof ViewHandler.KeepAliveRequest);
    ViewHandler.KeepAliveRequest keepAliveRequest = (ViewHandler.KeepAliveRequest) queue.peek();

    //test responding to the request with http response is interpreted into a KeepAliveResponse and hook is called
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    channel.writeInbound(response);
    ViewHandler.KeepAliveResponse keepAliveResponse = keepAliveRequest.observable()
            .cast(ViewHandler.KeepAliveResponse.class).timeout(1, TimeUnit.SECONDS).toBlocking().single();

    assertEquals(2, keepAliveEventCounter.get());
    assertEquals(ResponseStatus.NOT_EXISTS, keepAliveResponse.status());
    //different channel, needs to be closed to release the internal responseContent
    channel.close().awaitUninterruptibly();
}

From source file:org.hawkular.metrics.clients.ptrans.MetricBatcher.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (!(evt instanceof IdleStateEvent)) {
        LOG.trace("Dropping unhandled event '{}' for key '{}'", evt, cacheKey.name());
        return;/*from ww  w  .  j a  va  2s.  co m*/
    }
    IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
    if (idleStateEvent != IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT) {
        LOG.trace("Dropping event, expecting FIRST_READER_IDLE_STATE_EVENT for key '{}'", cacheKey.name());
        return;
    }
    List<SingleMetric> batchList = ctx.attr(cacheKey).getAndRemove();
    if (batchList != null && !batchList.isEmpty()) {
        LOG.trace("Batch delay reached for key '{}', forwarding {} metrics", cacheKey.name(), batchList.size());
        ctx.fireChannelRead(batchList);
    }
}

From source file:org.hawkular.metrics.clients.ptrans.MetricBatcherTest.java

License:Apache License

@Test
public void batcherShouldForwardMetricsIfSizeIsNotReachedButChannelIsIdle() {
    int batchSize = 10;
    MetricBatcher metricBatcher = new MetricBatcher(testName.getMethodName(), batchSize);
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(metricBatcher);

    int metricsCount = batchSize - 5;
    for (int i = 0; i < metricsCount; i++) {
        embeddedChannel.writeInbound(/*from   w w  w.  j a  v a  2  s. c om*/
                new SingleMetric(testName.getMethodName(), System.currentTimeMillis() - i, 13.0d + i));
    }

    embeddedChannel.pipeline().fireUserEventTriggered(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT);

    checkBatchOutput(embeddedChannel, metricsCount);
    assertNull("Unexpected batch forwarded", embeddedChannel.readInbound());
}