use of io.reactivex.netty.servo.http.HttpClientListener in project ribbon by Netflix.
the class NettyClientTest method testObservableWithMultipleServersWithOverrideRxConfig.
@Test
public void testObservableWithMultipleServersWithOverrideRxConfig() 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));
HttpClientConfig rxconfig = HttpClientConfig.Builder.newDefaultConfig();
Person person = getPersonObservable(lbObservables.submit(request, rxconfig)).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());
final HttpClientListener listener = lbObservables.getListener();
assertEquals(1, listener.getConnectionCount());
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 1;
}
});
}
use of io.reactivex.netty.servo.http.HttpClientListener 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());
}
use of io.reactivex.netty.servo.http.HttpClientListener 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;
}
});
}
use of io.reactivex.netty.servo.http.HttpClientListener 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;
}
});
}
use of io.reactivex.netty.servo.http.HttpClientListener in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancingWithTwoServers.
@Test
public void testLoadBalancingWithTwoServers() 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();
HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost("/testAsync/person").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), EmbeddedResources.defaultPerson, null)).withHeader("Content-type", "application/json");
NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config, errorHandler);
HttpClientListener externalListener = HttpClientListener.newHttpListener("external");
lbObservables.subscribe(externalListener);
Server server1 = new Server("localhost:" + server.getPort());
Server server2 = new Server("localhost:" + port);
lb.setServersList(Lists.newArrayList(server1, server2));
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();
}
assertEquals("ribbon", observer.obj.name);
assertEquals(EmbeddedResources.defaultPerson.age, observer.obj.age);
observer = new ObserverWithLatch<Person>();
observableWithRetries = getPersonObservable(lbObservables.submit(request, handler, null));
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(server1);
server.shutdown();
// assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
stats = lbObservables.getServerStats(server2);
// two requests to bad server because retry same server is set to 1
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
final HttpClientListener listener = lbObservables.getListener();
assertEquals(2, listener.getPoolAcquires());
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 2;
}
});
assertEquals(2, listener.getConnectionCount());
assertEquals(0, listener.getPoolReuse());
assertEquals(2, externalListener.getPoolAcquires());
}
Aggregations