Search in sources :

Example 1 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttpReuseChannel.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttpReuseChannel() 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, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        assertSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) LocalAddress(io.netty.channel.local.LocalAddress) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Channel(io.netty.channel.Channel) Subscription(rx.Subscription) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Example 2 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttps.

@Test(timeout = 5000)
public void testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttps() 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);
    assertEquals(0, allActiveAllocationsCount(allocator));
    try {
        final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.<HttpObject>error(new RuntimeException("test error")), new Interaction() {

            @Override
            public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
                final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
                getresp.subscribe(subscriber);
                subscriber.awaitTerminalEvent();
                subscriber.assertError(RuntimeException.class);
                subscriber.assertNoValues();
            }
        }).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
        assertEquals(0, allActiveAllocationsCount(allocator));
        assertSame(ch1, ch2);
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) Channel(io.netty.channel.Channel) 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)

Example 3 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorMultiInteractionSuccessAsHttp.

@Test(timeout = 5000)
public void testInitiatorMultiInteractionSuccessAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    assertEquals(0, allActiveAllocationsCount(allocator));
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    try (final HttpInitiator initiator = client.initiator().remoteAddress(new LocalAddress(addr)).build().toBlocking().single()) {
        {
            final Observable<? extends DisposableWrapper<HttpObject>> cached = initiator.defineInteraction(Observable.just(fullHttpRequest())).cache();
            cached.subscribe();
            // server side recv req
            final HttpTrade trade = trades.take();
            // recv all request
            trade.inbound().toCompletable().await();
            assertEquals(0, allActiveAllocationsCount(allocator));
            final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
            // send back resp
            trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
            // wait for recv all resp at client side
            cached.toCompletable().await();
            svrRespContent.release();
            assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached.compose(RxNettys.message2fullresp(initiator))), CONTENT));
            cached.doOnNext(DISPOSE_EACH).toCompletable().await();
        }
        assertEquals(0, allActiveAllocationsCount(allocator));
        {
            final Observable<? extends DisposableWrapper<HttpObject>> cached = initiator.defineInteraction(Observable.just(fullHttpRequest())).cache();
            final Observable<HttpObject> resp2 = cached.map(DisposableWrapperUtil.<HttpObject>unwrap());
            resp2.subscribe();
            // server side recv req
            final HttpTrade trade = trades.take();
            // recv all request
            trade.inbound().toCompletable().await();
            // assertEquals(0, allActiveAllocationsCount(allocator));
            final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
            // send back resp
            trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
            // wait for recv all resp at client side
            resp2.toCompletable().await();
            svrRespContent.release();
            assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached.compose(RxNettys.message2fullresp(initiator))), CONTENT));
        }
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) ByteBuf(io.netty.buffer.ByteBuf) 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) HttpObject(io.netty.handler.codec.http.HttpObject) Subscription(rx.Subscription) Test(org.junit.Test)

Example 4 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorMultiCalldefineInteractionAndSubscribe.

@Test(timeout = 5000)
public void testInitiatorMultiCalldefineInteractionAndSubscribe() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    assertEquals(0, allActiveAllocationsCount(allocator));
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    try (final HttpInitiator initiator = client.initiator().remoteAddress(new LocalAddress(addr)).build().toBlocking().single()) {
        final Observable<? extends DisposableWrapper<HttpObject>> resp1 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
        final Observable<? extends DisposableWrapper<HttpObject>> resp2 = initiator.defineInteraction(Observable.just(fullHttpRequest()));
        resp1.subscribe();
        final TestSubscriber<DisposableWrapper<HttpObject>> subscriber = new TestSubscriber<>();
        resp2.subscribe(subscriber);
        subscriber.awaitTerminalEvent();
        subscriber.assertError(RuntimeException.class);
    // assertEquals(0, allActiveAllocationsCount(allocator));
    } finally {
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) HttpInitiator(org.jocean.http.client.HttpClient.HttpInitiator) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) HttpObject(io.netty.handler.codec.http.HttpObject) TestSubscriber(rx.observers.TestSubscriber) Subscription(rx.Subscription) Test(org.junit.Test)

Example 5 with HttpTrade

use of org.jocean.http.server.HttpServerBuilder.HttpTrade in project jocean-http by isdom.

the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttp.

@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttp() throws Exception {
    // 配置 池化分配器 为 取消缓存,使用 Heap
    configDefaultAllocator();
    final PooledByteBufAllocator allocator = defaultAllocator();
    assertEquals(0, allActiveAllocationsCount(allocator));
    final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
    final String addr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
    final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
    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 Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
                cached.subscribe();
                // response.subscribe();
                // server side recv req
                final HttpTrade trade = trades.take();
                // recv all request
                trade.inbound().toCompletable().await();
                assertEquals(0, allActiveAllocationsCount(allocator));
                final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
                assertEquals(1, allActiveAllocationsCount(allocator));
                // send back resp
                trade.outbound(TestHttpUtil.buildByteBufResponse("text/plain", svrRespContent));
                // wait for recv all resp at client side
                cached.toCompletable().await();
                svrRespContent.release();
                // holder create clientside resp's content
                assertEquals(1, allActiveAllocationsCount(allocator));
                assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
            }
        });
    } finally {
        assertEquals(0, allActiveAllocationsCount(allocator));
        client.close();
        server.unsubscribe();
    }
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) DisposableWrapper(org.jocean.idiom.DisposableWrapper) 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)

Aggregations

HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)59 Test (org.junit.Test)56 Subscription (rx.Subscription)41 DisposableWrapper (org.jocean.idiom.DisposableWrapper)29 LocalAddress (io.netty.channel.local.LocalAddress)26 IOException (java.io.IOException)25 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)24 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)23 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)20 Nettys4Test (org.jocean.http.util.Nettys4Test)20 TestSubscriber (rx.observers.TestSubscriber)20 SSLException (javax.net.ssl.SSLException)18 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 HttpObject (io.netty.handler.codec.http.HttpObject)17 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17