use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class DiscoveryEnabledServerListTest method init.
@BeforeClass
public static void init() throws IOException {
server = new MockWebServer();
String content = "Hello world";
MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
server.enqueue(response);
server.play();
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class RibbonTest method testHystrixCache.
@Test
public void testHystrixCache() throws IOException {
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
MockWebServer server = new MockWebServer();
String content = "Hello world";
MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content);
server.enqueue(response);
server.enqueue(response);
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("myclient").build();
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("http://localhost:" + server.getPort()).withMethod("GET").withRequestCacheKey("xyz").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
RibbonResponse<ByteBuf> ribbonResponse = request.withMetadata().execute();
assertFalse(ribbonResponse.getHystrixInfo().isResponseFromCache());
ribbonResponse = request.withMetadata().execute();
assertTrue(ribbonResponse.getHystrixInfo().isResponseFromCache());
} finally {
context.shutdown();
}
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class RibbonTest method testObserve.
@Test
public void testObserve() throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
String content = "Hello world";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withMaxAutoRetriesNextServer(3).withReadTimeout(300000).withConfigurationBasedServerList("localhost:12345, localhost:10092, localhost:" + server.getPort()));
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
Observable<ByteBuf> result = request.observe();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> fromCommand = new AtomicReference<String>();
// We need to wait until the response is received and processed by event loop
// and make sure that subscribing to it again will not cause ByteBuf ref count issue
result.toBlocking().last();
result.subscribe(new Action1<ByteBuf>() {
@Override
public void call(ByteBuf t1) {
try {
fromCommand.set(t1.toString(Charset.defaultCharset()));
} catch (Exception e) {
e.printStackTrace();
}
latch.countDown();
}
});
latch.await();
assertEquals(content, fromCommand.get());
Observable<RibbonResponse<Observable<ByteBuf>>> metaResult = request.withMetadata().observe();
String result2 = "";
// We need to wait until the response is received and processed by event loop
// and make sure that subscribing to it again will not cause ByteBuf ref count issue
metaResult.toBlocking().last();
result2 = metaResult.flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<ByteBuf>>() {
@Override
public Observable<ByteBuf> call(RibbonResponse<Observable<ByteBuf>> t1) {
return t1.content();
}
}).map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf t1) {
return t1.toString(Charset.defaultCharset());
}
}).toBlocking().single();
assertEquals(content, result2);
}
use of com.google.mockwebserver.MockWebServer in project ribbon by Netflix.
the class NettyClientTest method testLoadBalancingPostWithReadTimeout.
@Test
public void testLoadBalancingPostWithReadTimeout() 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(1, 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();
}
assertEquals("ribbon", observer.obj.name);
assertEquals(2, observer.obj.age);
ServerStats stats = lbObservables.getServerStats(badServer);
server.shutdown();
assertEquals(4, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(4, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
// 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());
}
use of com.google.mockwebserver.MockWebServer 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());
}
Aggregations