Search in sources :

Example 26 with DisposableWrapper

use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionStillActiveAsHttps10ConnectionClose.

@Test(timeout = 5000)
public void testInitiatorInteractionStillActiveAsHttps10ConnectionClose() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
    try {
        final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
        request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                cached.subscribe();
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv all request
                trade.inbound().toCompletable().await();
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                // for HTTP 1.0 Connection: Close response behavior
                final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
                fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
                // missing Content-Length
                // response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
                fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
                trade.outbound(Observable.just(fullrespfromsvr));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                TerminateAware.Util.awaitTerminated(trade);
                final Channel channel = (Channel) initiator.transport();
                assertTrue(!channel.isActive());
                assertTrue(initiator.isActive());
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LocalAddress(io.netty.channel.local.LocalAddress) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ConnectableObservable(rx.observables.ConnectableObservable) Observable(rx.Observable) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Example 27 with DisposableWrapper

use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent.

@Test
public final void testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent() throws IOException {
    final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Nettys4Test.buildByteBuf(REQ_CONTENT));
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    final AtomicReference<DefaultFullHttpRequest> ref1 = new AtomicReference<DefaultFullHttpRequest>();
    trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {

        @Override
        public void call(DisposableWrapper<HttpObject> dwh) {
            ref1.set((DefaultFullHttpRequest) dwh.unwrap());
        }
    });
    final AtomicReference<DefaultFullHttpRequest> ref2 = new AtomicReference<DefaultFullHttpRequest>();
    trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {

        @Override
        public void call(DisposableWrapper<HttpObject> dwh) {
            ref2.set((DefaultFullHttpRequest) dwh.unwrap());
        }
    });
    writeToInboundAndFlush(channel, request);
    final byte[] firstReadBytes = Nettys.dumpByteBufAsBytes(ref1.get().content());
    assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), firstReadBytes));
    final byte[] secondReadBytes = Nettys.dumpByteBufAsBytes(ref2.get().content());
    assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), secondReadBytes));
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 28 with DisposableWrapper

use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForCallAbortAfterPartRequestThenPushError.

@Test
public final void testTradeForCallAbortAfterPartRequestThenPushError() {
    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>> reqSubscriber = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber);
    writeToInboundAndFlush(channel, request);
    writeToInboundAndFlush(channel, req_contents[0]);
    trade.close();
    assertFalse(trade.isActive());
    /* TODO, fix no terminal event
        reqSubscriber.assertTerminalEvent();
        reqSubscriber.assertError(Exception.class);
        */
    reqSubscriber.assertValueCount(2);
    reqSubscriber.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 29 with DisposableWrapper

use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.

the class DefaultHttpTradeTestCase method testTradeForRequestPartError.

@Test
public final void testTradeForRequestPartError() {
    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 List<DisposableWrapper<HttpObject>> part_req = new ArrayList<DisposableWrapper<HttpObject>>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(RxNettys.<HttpObject>wrap4release(request));
            for (int idx = 0; idx < 5; idx++) {
                this.add(RxNettys.<HttpObject>wrap4release(req_contents[idx]));
            }
        }
    };
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>just(req_contents[0]), Observable.<HttpObject>just(req_contents[1]), Observable.<HttpObject>just(req_contents[2]), Observable.<HttpObject>just(req_contents[3]), Observable.<HttpObject>just(req_contents[4])).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    channel.disconnect().syncUninterruptibly();
    assertTrue(!trade.isActive());
    // TODO, fix assert terminal event
    // reqSubscriber.assertTerminalEvent();
    // java.lang.AssertionError: Exceptions differ; expected: java.lang.RuntimeException: RequestPartError,
    // actual: java.lang.RuntimeException: trade unactived
    // reqSubscriber.assertError(Exception.class);
    reqSubscriber.assertNotCompleted();
    reqSubscriber.assertValues(part_req.toArray(new DisposableWrapper[0]));
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) 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 30 with DisposableWrapper

use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionUnsubscribedAlreadyAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionUnsubscribedAlreadyAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    final String addr = UUID.randomUUID().toString();
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                subscriber.unsubscribe();
                final Subscription subscription = getresp.subscribe(subscriber);
                assertTrue(subscription.isUnsubscribed());
                subscriber.assertNoTerminalEvent();
                subscriber.assertNoValues();
            }
        });
        assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) SSLException(javax.net.ssl.SSLException) TransportException(org.jocean.http.TransportException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) TestSubscriber(rx.observers.TestSubscriber) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Subscription(rx.Subscription) Test(org.junit.Test)

Aggregations

DisposableWrapper (org.jocean.idiom.DisposableWrapper)32 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)29 Test (org.junit.Test)28 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)20 LocalAddress (io.netty.channel.local.LocalAddress)20 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)20 Subscription (rx.Subscription)20 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)19 TestSubscriber (rx.observers.TestSubscriber)19 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)18 IOException (java.io.IOException)18 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17 SSLException (javax.net.ssl.SSLException)17 TransportException (org.jocean.http.TransportException)17 ByteBuf (io.netty.buffer.ByteBuf)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)12 Observable (rx.Observable)11