use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class RibbonTest method testCommand.
@Test
public void testCommand() throws IOException, InterruptedException, ExecutionException {
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.enqueue(response);
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();
String result = request.execute().toString(Charset.defaultCharset());
assertEquals(content, result);
// repeat the same request
ByteBuf raw = request.execute();
result = raw.toString(Charset.defaultCharset());
raw.release();
assertEquals(content, result);
result = request.queue().get().toString(Charset.defaultCharset());
assertEquals(content, result);
}
use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class ShutDownTest method testLifeCycleShutdown.
@Test
public void testLifeCycleShutdown() throws Exception {
final AtomicBoolean shutDownCalled = new AtomicBoolean(false);
final HttpClient<ByteBuf, ByteBuf> client = new HttpClient<ByteBuf, ByteBuf>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> submit(HttpClientRequest<ByteBuf> request) {
return null;
}
@Override
public Observable<HttpClientResponse<ByteBuf>> submit(HttpClientRequest<ByteBuf> request, ClientConfig config) {
return null;
}
@Override
public Observable<ObservableConnection<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>> connect() {
return null;
}
@Override
public void shutdown() {
shutDownCalled.set(true);
}
@Override
public String name() {
return "SampleMovieService";
}
@Override
public Subscription subscribe(MetricEventsListener<? extends ClientMetricsEvent<?>> listener) {
return null;
}
};
RibbonTransportFactory transportFactory = new RibbonTransportFactory(ClientConfigFactory.DEFAULT) {
@Override
public HttpClient<ByteBuf, ByteBuf> newHttpClient(IClientConfig config) {
return client;
}
};
HttpResourceGroup.Builder groupBuilder = HttpResourceGroup.Builder.newBuilder("SampleMovieService", ClientConfigFactory.DEFAULT, transportFactory);
HttpResourceGroup group = groupBuilder.build();
SampleMovieService service = RibbonDynamicProxy.newInstance(SampleMovieService.class, group);
ProxyLifeCycle proxyLifeCycle = (ProxyLifeCycle) service;
proxyLifeCycle.shutdown();
assertTrue(proxyLifeCycle.isShutDown());
assertTrue(shutDownCalled.get());
}
use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class RibbonTest method testFallback.
@Test
public void testFallback() throws IOException {
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:12345").withMaxAutoRetriesNextServer(1));
final String fallback = "fallback";
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").withFallbackProvider(new FallbackHandler<ByteBuf>() {
@Override
public Observable<ByteBuf> getFallback(HystrixInvokableInfo<?> hystrixInfo, Map<String, Object> requestProperties) {
try {
return Observable.just(Unpooled.buffer().writeBytes(fallback.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
return Observable.error(e);
}
}
}).build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
final AtomicReference<HystrixInvokableInfo<?>> hystrixInfo = new AtomicReference<HystrixInvokableInfo<?>>();
final AtomicBoolean failed = new AtomicBoolean(false);
Observable<String> result = request.withMetadata().toObservable().flatMap(new Func1<RibbonResponse<Observable<ByteBuf>>, Observable<String>>() {
@Override
public Observable<String> call(final RibbonResponse<Observable<ByteBuf>> response) {
hystrixInfo.set(response.getHystrixInfo());
failed.set(response.getHystrixInfo().isFailedExecution());
return response.content().map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf t1) {
return t1.toString(Charset.defaultCharset());
}
});
}
});
String s = result.toBlocking().single();
// this returns true only after the blocking call is done
assertTrue(hystrixInfo.get().isResponseFromFallback());
assertTrue(failed.get());
assertEquals(fallback, s);
}
use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class RibbonTest method testCacheHit.
@Test
public void testCacheHit() {
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:12345").withMaxAutoRetriesNextServer(1));
final String content = "from cache";
final String cacheKey = "somekey";
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test").withCacheProvider(cacheKey, new CacheProvider<ByteBuf>() {
@Override
public Observable<ByteBuf> get(String key, Map<String, Object> vars) {
if (key.equals(cacheKey)) {
try {
return Observable.just(Unpooled.buffer().writeBytes(content.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
return Observable.error(e);
}
} else {
return Observable.error(new Exception("Cache miss"));
}
}
}).withUriTemplate("/").withMethod("GET").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
String result = request.execute().toString(Charset.defaultCharset());
assertEquals(content, result);
}
use of com.netflix.ribbon.http.HttpResourceGroup 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();
}
}
Aggregations