Search in sources :

Example 11 with Person

use of com.netflix.ribbon.test.resources.EmbeddedResources.Person in project ribbon by Netflix.

the class NettyClientTest method testQuery.

@Test
public void testQuery() throws Exception {
    Person myPerson = new Person("hello_world", 4);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/personQuery?name=" + myPerson.name + "&age=" + myPerson.age);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    Person person = getPersonObservable(observableClient.submit(new Server(host, port), request)).toBlocking().single();
    assertEquals(myPerson, person);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) ByteBuf(io.netty.buffer.ByteBuf) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 12 with Person

use of com.netflix.ribbon.test.resources.EmbeddedResources.Person in project ribbon by Netflix.

the class NettyClientTest method testLoadBalancingPostWithNoRetrySameServer.

@Test
public void testLoadBalancingPostWithNoRetrySameServer() throws Exception {
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
    server.play();
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/postTimeout").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)).withHeader("Content-type", "application/json");
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(0, 3, true);
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    RetryHandler handler = new RequestSpecificRetryHandler(true, true, errorHandler, null) {

        @Override
        public boolean isRetriableException(Throwable e, boolean sameServer) {
            return true;
        }
    };
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    server.shutdown();
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    assertEquals(2, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(2, stats.getSuccessiveConnectionFailureCount());
    stats = lbObservables.getServerStats(goodServer);
    assertEquals(1, stats.getTotalRequestsCount());
    assertEquals(0, stats.getActiveRequestsCount());
    assertEquals(0, stats.getSuccessiveConnectionFailureCount());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ServerStats(com.netflix.loadbalancer.ServerStats) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) RetryHandler(com.netflix.client.RetryHandler) DummyPing(com.netflix.loadbalancer.DummyPing) MockWebServer(com.google.mockwebserver.MockWebServer) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 13 with Person

use of com.netflix.ribbon.test.resources.EmbeddedResources.Person in project ribbon by Netflix.

the class NettyClientTest method testLoadBalancingObservablesWithReadTimeout.

@Test
public void testLoadBalancingObservablesWithReadTimeout() throws Exception {
    NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    MockWebServer server = new MockWebServer();
    String content = "{\"name\": \"ribbon\", \"age\": 2}";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
    server.play();
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 100);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/readTimeout");
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
    Server goodServer = new Server("localhost:" + server.getPort());
    Server badServer = new Server("localhost:" + port);
    lb.setServersList(Lists.newArrayList(goodServer, badServer, badServer, goodServer));
    Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
    ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
    observableWithRetries.subscribe(observer);
    observer.await();
    if (observer.error != null) {
        observer.error.printStackTrace();
    }
    assertEquals("ribbon", observer.obj.name);
    assertEquals(2, observer.obj.age);
    ServerStats stats = lbObservables.getServerStats(badServer);
    server.shutdown();
    final HttpClientListener listener = lbObservables.getListener();
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {

        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 5;
        }
    });
    assertEquals(0, listener.getPoolReuse());
    // 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());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) 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) MockWebServer(com.google.mockwebserver.MockWebServer) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 14 with Person

use of com.netflix.ribbon.test.resources.EmbeddedResources.Person in project ribbon by Netflix.

the class NettyClientTest method testStreamWithLoadBalancer.

@Test
public void testStreamWithLoadBalancer() throws Exception {
    // NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
    // IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
    IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true).withMaxAutoRetries(1).withMaxAutoRetriesNextServer(3).build();
    BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
    LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream");
    List<Person> result = Lists.newArrayList();
    Server goodServer = new Server("localhost:" + port);
    Server badServer = new Server("localhost:12245");
    List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
    lb.setServersList(servers);
    result = getPersonListFromResponse(lbObservables.submit(request, null, null));
    assertEquals(EmbeddedResources.entityStream, result);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) ServerSentEvent(io.reactivex.netty.protocol.text.sse.ServerSentEvent) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) 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 15 with Person

use of com.netflix.ribbon.test.resources.EmbeddedResources.Person in project ribbon by Netflix.

the class NettyClientTest method testPoolReuse.

@Test
public void testPoolReuse() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/person");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(IClientConfig.Builder.newBuilder().withDefaultValues().withMaxAutoRetries(1).withMaxAutoRetriesNextServer(1).build());
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    response = observableClient.submit(request);
    person = getPersonObservable(response).toBlocking().single();
    assertEquals(EmbeddedResources.defaultPerson, person);
    final HttpClientListener listener = observableClient.getListener();
    assertEquals(2, listener.getPoolAcquires());
    waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {

        @Override
        public Boolean call() {
            return listener.getPoolReleases() == 2;
        }
    });
    assertEquals(1, listener.getConnectionCount());
    assertEquals(1, listener.getPoolReuse());
}
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

Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)16 ByteBuf (io.netty.buffer.ByteBuf)16 Test (org.junit.Test)16 MockWebServer (com.google.mockwebserver.MockWebServer)12 Server (com.netflix.loadbalancer.Server)12 HttpServer (com.sun.net.httpserver.HttpServer)12 IClientConfig (com.netflix.client.config.IClientConfig)9 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)9 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)9 DummyPing (com.netflix.loadbalancer.DummyPing)9 ServerStats (com.netflix.loadbalancer.ServerStats)8 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)7 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)5 MockResponse (com.google.mockwebserver.MockResponse)4 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)3 RetryHandler (com.netflix.client.RetryHandler)3 ClientException (com.netflix.client.ClientException)2 HttpClientConfig (io.reactivex.netty.protocol.http.client.HttpClient.HttpClientConfig)1 ServerSentEvent (io.reactivex.netty.protocol.text.sse.ServerSentEvent)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1