Search in sources :

Example 6 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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 7 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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 8 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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 9 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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)

Example 10 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class NettyClientTest method testObservable.

@Test
public void testObservable() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(1, listener.getPoolAcquires());
    assertEquals(1, listener.getConnectionCount());
    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

HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)21 ByteBuf (io.netty.buffer.ByteBuf)18 Test (org.junit.Test)16 Server (com.netflix.loadbalancer.Server)9 MockWebServer (com.google.mockwebserver.MockWebServer)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)5 HttpServer (com.sun.net.httpserver.HttpServer)5 Observable (rx.Observable)5 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)4 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)3 DummyPing (com.netflix.loadbalancer.DummyPing)3 ExecutionContext (com.netflix.loadbalancer.reactive.ExecutionContext)3 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)3 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)3 ClientException (com.netflix.client.ClientException)2 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)2