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);
}
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"));
}
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);
}
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());
}
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;
}
});
}
Aggregations