Search in sources :

Example 31 with IClientConfig

use of com.netflix.client.config.IClientConfig 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 32 with IClientConfig

use of com.netflix.client.config.IClientConfig in project ribbon by Netflix.

the class DiscoveryLoadBalancerTest method testLoadBalancer.

@Test
public void testLoadBalancer() {
    IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues().withDeploymentContextBasedVipAddresses(getVipAddress()).build().set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config);
    LoadBalancerContext lbContext = client.getLoadBalancerContext();
    List<Server> serverList = lbContext.getLoadBalancer().getAllServers();
    assertEquals(getMockServerList(), serverList);
}
Also used : LoadBalancerContext(com.netflix.loadbalancer.LoadBalancerContext) Server(com.netflix.loadbalancer.Server) DiscoveryEnabledNIWSServerList(com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList) IClientConfig(com.netflix.client.config.IClientConfig) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test) MockedDiscoveryServerListTest(com.netflix.ribbon.testutils.MockedDiscoveryServerListTest)

Example 33 with IClientConfig

use of com.netflix.client.config.IClientConfig in project ribbon by Netflix.

the class ListenerTest method testDisabledListener.

@Test
public void testDisabledListener() throws Exception {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues("myClient").withProperty(CommonClientConfigKey.ConnectTimeout, "2000").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    List<Server> servers = Lists.newArrayList(badServer);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    ConfigurationManager.getConfigInstance().setProperty("ribbon.listener." + TestExecutionListener.class.getName() + ".disabled", "true");
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
    } catch (Exception e) {
        assertNotNull(e);
    }
    assertEquals(0, listener.executionStartCounter.get());
    assertEquals(0, listener.startWithServerCounter.get());
    assertEquals(0, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(0, listener.executionSuccessCounter.get());
    try {
        client.submit(request, null, overrideConfig).toBlocking().last();
    } catch (Exception e) {
        assertNotNull(e);
    }
    assertEquals(0, listener.executionStartCounter.get());
    assertEquals(0, listener.startWithServerCounter.get());
    assertEquals(0, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(0, listener.executionSuccessCounter.get());
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ClientException(com.netflix.client.ClientException) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) IOException(java.io.IOException) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 34 with IClientConfig

use of com.netflix.client.config.IClientConfig in project ribbon by Netflix.

the class ListenerTest method testSuccessExecution.

@Test
public void testSuccessExecution() throws IOException {
    MockWebServer server = new MockWebServer();
    String content = "OK";
    server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "application/json").setBody(content));
    server.play();
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "2000").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server goodServer = new Server("localhost:" + server.getPort());
    List<Server> servers = Lists.newArrayList(goodServer, badServer);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig().set(CommonClientConfigKey.ConnectTimeout, 500);
    TestExecutionListener<ByteBuf, ByteBuf> listener = new TestExecutionListener<ByteBuf, ByteBuf>(request, overrideConfig);
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    HttpClientResponse<ByteBuf> response = client.submit(request, null, overrideConfig).toBlocking().last();
    assertEquals(200, response.getStatus().code());
    assertEquals(1, listener.executionStartCounter.get());
    assertEquals(3, listener.startWithServerCounter.get());
    assertEquals(2, listener.exceptionWithServerCounter.get());
    assertEquals(0, listener.executionFailedCounter.get());
    assertEquals(1, listener.executionSuccessCounter.get());
    assertEquals(500, listener.getContext().getClientProperty(CommonClientConfigKey.ConnectTimeout).intValue());
    assertTrue(listener.isContextChecked());
    assertTrue(listener.isCheckExecutionInfo());
    assertSame(response, listener.getResponse());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) DummyPing(com.netflix.loadbalancer.DummyPing) MockWebServer(com.google.mockwebserver.MockWebServer) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Example 35 with IClientConfig

use of com.netflix.client.config.IClientConfig in project ribbon by Netflix.

the class ListenerTest method testAbortedExecutionOnServer.

@Test
public void testAbortedExecutionOnServer() {
    IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "100").withProperty(CommonClientConfigKey.MaxAutoRetries, 1).withProperty(CommonClientConfigKey.MaxAutoRetriesNextServer, 1);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
    Server badServer = new Server("localhost:12345");
    Server badServer2 = new Server("localhost:34567");
    List<Server> servers = Lists.newArrayList(badServer, badServer2);
    BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder().withRule(new AvailabilityFilteringRule()).withPing(new DummyPing()).buildFixedServerListLoadBalancer(servers);
    IClientConfig overrideConfig = DefaultClientConfigImpl.getEmptyConfig();
    TestExecutionListener listener = new TestExecutionListener(request, overrideConfig) {

        @Override
        public void onStartWithServer(ExecutionContext context, ExecutionInfo info) {
            throw new AbortExecutionException("exit now");
        }
    };
    List<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>> listeners = Lists.<ExecutionListener<HttpClientRequest<ByteBuf>, HttpClientResponse<ByteBuf>>>newArrayList(listener);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb, config, new NettyHttpLoadBalancerErrorHandler(config), listeners);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> ref = new AtomicReference<Throwable>();
    client.submit(request, null, overrideConfig).subscribe(new Action1<HttpClientResponse<ByteBuf>>() {

        @Override
        public void call(HttpClientResponse<ByteBuf> byteBufHttpClientResponse) {
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            ref.set(throwable);
            latch.countDown();
        }
    });
    try {
        latch.await(500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(ref.get() instanceof AbortExecutionException);
}
Also used : Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) ExecutionInfo(com.netflix.loadbalancer.reactive.ExecutionInfo) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) AbortExecutionException(com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException) ByteBuf(io.netty.buffer.ByteBuf) ExecutionListener(com.netflix.loadbalancer.reactive.ExecutionListener) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionContext(com.netflix.loadbalancer.reactive.ExecutionContext) DummyPing(com.netflix.loadbalancer.DummyPing) IClientConfig(com.netflix.client.config.IClientConfig) AvailabilityFilteringRule(com.netflix.loadbalancer.AvailabilityFilteringRule) Test(org.junit.Test)

Aggregations

IClientConfig (com.netflix.client.config.IClientConfig)36 Test (org.junit.Test)27 Server (com.netflix.loadbalancer.Server)20 ByteBuf (io.netty.buffer.ByteBuf)20 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)18 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)18 DummyPing (com.netflix.loadbalancer.DummyPing)18 MockWebServer (com.google.mockwebserver.MockWebServer)17 HttpServer (com.sun.net.httpserver.HttpServer)10 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)9 ServerStats (com.netflix.loadbalancer.ServerStats)8 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)7 MockResponse (com.google.mockwebserver.MockResponse)6 ClientException (com.netflix.client.ClientException)6 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)5 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)5 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)4 RetryHandler (com.netflix.client.RetryHandler)4 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)4 ExecutionContext (com.netflix.loadbalancer.reactive.ExecutionContext)3