Search in sources :

Example 66 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForResponseAfterAbort.

@Test
public final void testTradeForResponseAfterAbort() {
    final HttpTrade trade = new DefaultHttpTrade(new EmbeddedChannel());
    trade.close();
    assertFalse(trade.isActive());
    final SubscriberHolder<HttpObject> subsholder = new SubscriberHolder<>();
    final Subscription subscription = trade.outbound(Observable.unsafeCreate(subsholder));
    assertNull(subscription);
    assertEquals(0, subsholder.getSubscriberCount());
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Subscription(rx.Subscription) SubscriberHolder(org.jocean.idiom.rx.SubscriberHolder) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 67 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testDoOnClosedBeforeAndAfterOutboundResponse.

@Test
public final void testDoOnClosedBeforeAndAfterOutboundResponse() {
    final HttpTrade trade = new DefaultHttpTrade(new EmbeddedChannel());
    final AtomicBoolean onClosed = new AtomicBoolean(false);
    trade.doOnTerminate(new Action1<HttpTrade>() {

        @Override
        public void call(final HttpTrade trade) {
            onClosed.set(true);
        }
    });
    assertFalse(onClosed.get());
    assertTrue(trade.isActive());
    trade.outbound(Observable.<HttpObject>error(new RuntimeException("ResponseError")));
    assertFalse(trade.isActive());
    assertTrue(onClosed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 68 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForRequestAndContentsSourceAndDumpFullRequests.

@Test
public final void testTradeForRequestAndContentsSourceAndDumpFullRequests() throws IOException {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>from(req_contents), Observable.<HttpObject>just(LastHttpContent.EMPTY_LAST_CONTENT)).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 69 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForMultiSubscribeRequestOnlyOneToSource2.

// 3 subscriber subscribe inbound request at different time,
// so push with different httpobject
@Test
public final void testTradeForMultiSubscribeRequestOnlyOneToSource2() {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber1 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber1);
    writeToInboundAndFlush(channel, request);
    reqSubscriber1.assertValueCount(1);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request));
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber2 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber2);
    writeToInboundAndFlush(channel, req_contents[0]);
    reqSubscriber1.assertValueCount(2);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber2.assertValueCount(2);
    reqSubscriber2.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber3 = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber3);
    reqSubscriber1.assertValueCount(2);
    reqSubscriber1.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber2.assertValueCount(2);
    reqSubscriber2.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
    reqSubscriber3.assertValueCount(2);
    reqSubscriber3.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestSubscriber(rx.observers.TestSubscriber) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 70 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project jocean-http by isdom.

the class ChannelTestCase method testChannelInactiveForInactiveChannel.

@Test
public final void testChannelInactiveForInactiveChannel() throws InterruptedException {
    final EmbeddedChannel channel = new EmbeddedChannel();
    channel.disconnect().syncUninterruptibly();
    assertTrue(!channel.isActive());
    final AtomicBoolean isInactiveCalled = new AtomicBoolean(false);
    channel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
            isInactiveCalled.set(true);
        }
    });
    Thread.currentThread().sleep(1000);
    assertFalse(isInactiveCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1027 Test (org.junit.jupiter.api.Test)515 ByteBuf (io.netty.buffer.ByteBuf)356 Test (org.junit.Test)342 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)85 HttpResponse (io.netty.handler.codec.http.HttpResponse)73 HttpRequest (io.netty.handler.codec.http.HttpRequest)69 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)64 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)60 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)55 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)50 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)49 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)46 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)42 IOException (java.io.IOException)38 InetSocketAddress (java.net.InetSocketAddress)38 Executable (org.junit.jupiter.api.function.Executable)36 ArrayList (java.util.ArrayList)35 Before (org.junit.Before)32 ChannelHandler (io.netty.channel.ChannelHandler)27