Search in sources :

Example 1 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class Http2AlpnHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RequestWithCallback)) {
        ctx.write(msg, promise);
        return;
    }
    _alpnPromise.addListener(f -> {
        ChannelFuture future = (ChannelFuture) f;
        if (future.isSuccess()) {
            ctx.write(msg, promise);
        } else {
            @SuppressWarnings("unchecked") TimeoutAsyncPoolHandle<?> handle = ((RequestWithCallback<?, ?, TimeoutAsyncPoolHandle<?>>) msg).handle();
            handle.error().release();
            TransportCallback<?> callback = ((RequestWithCallback) msg).callback();
            callback.onResponse(TransportResponseImpl.error(future.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)

Example 2 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class TestHttp2AlpnHandler method testChannelCloseBeforeNegotiation.

@Test(timeOut = 10000)
@SuppressWarnings("unchecked")
public void testChannelCloseBeforeNegotiation() throws Exception {
    SslContext sslContext = Mockito.mock(SslContext.class);
    Http2StreamCodec http2StreamCodec = Mockito.mock(Http2StreamCodec.class);
    Http2AlpnHandler handler = new Http2AlpnHandler(sslContext, http2StreamCodec, true, Integer.MAX_VALUE);
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    RequestWithCallback request = Mockito.mock(RequestWithCallback.class);
    TimeoutAsyncPoolHandle handle = Mockito.mock(TimeoutAsyncPoolHandle.class);
    TimeoutTransportCallback callback = Mockito.mock(TimeoutTransportCallback.class);
    Mockito.when(request.handle()).thenReturn(handle);
    Mockito.when(request.callback()).thenReturn(callback);
    // Write should not succeed before negotiation completes
    Assert.assertFalse(channel.writeOutbound(request));
    Assert.assertFalse(channel.finish());
    // Synchronously waiting for channel to close
    channel.close().sync();
    Mockito.verify(request).handle();
    Mockito.verify(request).callback();
    Mockito.verify(handle).dispose();
    Mockito.verify(callback).onResponse(Mockito.any(TransportResponse.class));
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) TimeoutTransportCallback(com.linkedin.r2.transport.http.client.TimeoutTransportCallback) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) SslContext(io.netty.handler.ssl.SslContext) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Example 3 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class TestTimeoutAsyncPoolHandle method testGoodReleaseAfterTimeout.

@Test
public void testGoodReleaseAfterTimeout() throws Exception {
    FakePool<Object> pool = new FakePool<>();
    TimeoutAsyncPoolHandle<Object> handle = new TimeoutAsyncPoolHandle<>(pool, _scheduler, IMMEDIATE_TIMEOUT, TIME_UNIT, new Object());
    CountDownLatch latch = new CountDownLatch(1);
    handle.addTimeoutTask(() -> latch.countDown());
    latch.await(OPERATION_TIMEOUT, TIME_UNIT);
    handle.release();
    Assert.assertEquals(pool.getPutCount(), 1);
    Assert.assertEquals(pool.getDisposeCount(), 0);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Example 4 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class TestTimeoutAsyncPoolHandle method testTimeout.

@Test
public void testTimeout() throws Exception {
    FakePool<Object> pool = new FakePool<>();
    TimeoutAsyncPoolHandle<Object> handle = new TimeoutAsyncPoolHandle<>(pool, _scheduler, IMMEDIATE_TIMEOUT, TIME_UNIT, new Object());
    CountDownLatch latch = new CountDownLatch(1);
    handle.addTimeoutTask(() -> latch.countDown());
    latch.await(OPERATION_TIMEOUT, TIME_UNIT);
    Assert.assertEquals(pool.getPutCount(), 1);
    Assert.assertEquals(pool.getDisposeCount(), 0);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Example 5 with TimeoutAsyncPoolHandle

use of com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle in project rest.li by linkedin.

the class TestTimeoutAsyncPoolHandle method testBadReleaseAfterTimeout.

@Test
public void testBadReleaseAfterTimeout() throws Exception {
    FakePool<Object> pool = new FakePool<>();
    TimeoutAsyncPoolHandle<Object> handle = new TimeoutAsyncPoolHandle<>(pool, _scheduler, IMMEDIATE_TIMEOUT, TIME_UNIT, new Object());
    CountDownLatch latch = new CountDownLatch(1);
    handle.addTimeoutTask(() -> latch.countDown());
    latch.await(OPERATION_TIMEOUT, TIME_UNIT);
    handle.dispose();
    Assert.assertEquals(pool.getPutCount(), 1);
    Assert.assertEquals(pool.getDisposeCount(), 0);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Aggregations

RequestWithCallback (com.linkedin.r2.transport.common.bridge.common.RequestWithCallback)7 TimeoutAsyncPoolHandle (com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle)6 ChannelFuture (io.netty.channel.ChannelFuture)5 Test (org.testng.annotations.Test)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)3 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ByteString (com.linkedin.data.ByteString)2 Response (com.linkedin.r2.message.Response)2 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)2 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)2 TransportResponse (com.linkedin.r2.transport.common.bridge.common.TransportResponse)2 TimeoutTransportCallback (com.linkedin.r2.transport.http.client.TimeoutTransportCallback)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 R2Constants (com.linkedin.r2.filter.R2Constants)1 Request (com.linkedin.r2.message.Request)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)1 Reader (com.linkedin.r2.message.stream.entitystream.Reader)1