Search in sources :

Example 96 with ByteBuf

use of io.netty.buffer.ByteBuf in project ribbon by Netflix.

the class NettyClientTest method testConnectTimeout.

@Test
public void testConnectTimeout() throws Exception {
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1"));
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("http://www.google.com:81/");
    Observable<HttpClientResponse<ByteBuf>> observable = observableClient.submit(new Server("www.google.com", 81), request);
    ObserverWithLatch<HttpClientResponse<ByteBuf>> observer = new ObserverWithLatch<HttpClientResponse<ByteBuf>>();
    observable.subscribe(observer);
    observer.await();
    assertNotNull(observer.error);
    assertTrue(observer.error instanceof io.netty.channel.ConnectTimeoutException);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 97 with ByteBuf

use of io.netty.buffer.ByteBuf in project ribbon by Netflix.

the class NettyClientTest method testContext.

@Test
public void testContext() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/context");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    String requestId = "xyz";
    ContextsContainerImpl contextsContainer = new ContextsContainerImpl(new MapBackedKeySupplier());
    contextsContainer.addContext("Context1", "value1");
    RxContexts.DEFAULT_CORRELATOR.onNewServerRequest(requestId, contextsContainer);
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(new Server(host, port), request);
    final AtomicReference<ContextsContainer> responseContext = new AtomicReference<ContextsContainer>();
    String requestIdSent = response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {

        @Override
        public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> t1) {
            return t1.getContent();
        }
    }).map(new Func1<ByteBuf, String>() {

        @Override
        public String call(ByteBuf t1) {
            String requestId = RxContexts.DEFAULT_CORRELATOR.getRequestIdForClientRequest();
            responseContext.set(RxContexts.DEFAULT_CORRELATOR.getContextForClientRequest(requestId));
            return t1.toString(Charset.defaultCharset());
        }
    }).toBlocking().single();
    assertEquals(requestId, requestIdSent);
    assertEquals("value1", responseContext.get().getContext("Context1"));
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) MapBackedKeySupplier(io.reactivex.netty.contexts.MapBackedKeySupplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) ContextsContainerImpl(io.reactivex.netty.contexts.ContextsContainerImpl) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ContextsContainer(io.reactivex.netty.contexts.ContextsContainer) Func1(rx.functions.Func1) Test(org.junit.Test)

Example 98 with ByteBuf

use of io.netty.buffer.ByteBuf in project ribbon by Netflix.

the class NettyClientTest method testUnexpectedResponse.

@Test
public void testUnexpectedResponse() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/throttle");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient();
    Observable<HttpClientResponse<ByteBuf>> responseObservable = client.submit(new Server(host, port), request);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    responseObservable.subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> t1) {
            latch.countDown();
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable t1) {
            error.set(t1);
            latch.countDown();
        }
    });
    latch.await();
    assertTrue(error.get() instanceof ClientException);
    ClientException ce = (ClientException) error.get();
    assertTrue(ce.getErrorType() == ClientException.ErrorType.SERVER_THROTTLED);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ClientException(com.netflix.client.ClientException) Test(org.junit.Test)

Example 99 with ByteBuf

use of io.netty.buffer.ByteBuf in project ribbon by Netflix.

the class NettyClientTest method testObservableWithMultipleServers.

@Test
public void testObservableWithMultipleServers() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(1, 3, true));
    Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    ServerStats stats = lbObservables.getServerStats(badServer);
    // two requests to bad server because retry same server is set to 1
    assertEquals(4, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(4, stats.getSuccessiveConnectionFailureCount());
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
    person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    HttpClientListener listener = lbObservables.getListener();
    assertEquals(1, listener.getPoolReuse());
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) HttpClientListener(io.reactivex.netty.servo.http.HttpClientListener) ServerStats(com.netflix.loadbalancer.ServerStats) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 100 with ByteBuf

use of io.netty.buffer.ByteBuf in project ribbon by Netflix.

the class NettyClientTest method testSubmitToAbsoluteURI.

@Test
public void testSubmitToAbsoluteURI() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    // final List<Person> result = Lists.newArrayList();
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    // need to sleep to wait until connection is released
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(1, listener.getConnectionCount());
    assertEquals(1, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {

        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 1;
        }
    });
}
Also used : HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) HttpClientListener(io.reactivex.netty.servo.http.HttpClientListener) Test(org.junit.Test)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1517 Test (org.junit.Test)663 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)160 IOException (java.io.IOException)97 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)80 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)59 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)53 Map (java.util.Map)44 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 HashMap (java.util.HashMap)33 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 List (java.util.List)32