use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class DiscoveryEnabledServerListTest method testDynamicServers.
@Test
public void testDynamicServers() {
ConfigurationManager.getConfigInstance().setProperty("MyService.ribbon." + Keys.DeploymentContextBasedVipAddresses, getVipAddress());
ConfigurationManager.getConfigInstance().setProperty("MyService.ribbon." + Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("MyService").withClientOptions(ClientOptions.create().withMaxAutoRetriesNextServer(3).withReadTimeout(300000)).build();
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("Hello world", result);
}
use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class HttpResourceGroupFactoryTest method testResourceGroupCreation.
private void testResourceGroupCreation(Class<?> clientInterface, Class<? extends HttpResourceGroup> httpResourceGroupClass) {
ClassTemplate classTemplate = new ClassTemplate(clientInterface);
HttpResourceGroup resourceGroup = new ProxyHttpResourceGroupFactory(classTemplate).createResourceGroup();
assertNotNull("got null and expected instance of " + httpResourceGroupClass, resourceGroup);
}
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 testCacheMiss.
@Test
public void testCacheMiss() 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.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:" + server.getPort()).withMaxAutoRetriesNextServer(1));
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) {
return Observable.error(new Exception("Cache miss again"));
}
}).withMethod("GET").withUriTemplate("/").build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
String result = toStringBlocking(request);
assertEquals(content, result);
}
use of com.netflix.ribbon.http.HttpResourceGroup in project ribbon by Netflix.
the class RibbonTest method testValidator.
@Test
public void testValidator() throws IOException, InterruptedException {
// LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
MockWebServer server = new MockWebServer();
String content = "Hello world";
server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain").setBody(content));
server.play();
HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create().withConfigurationBasedServerList("localhost:" + server.getPort()));
HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class).withUriTemplate("/").withMethod("GET").withResponseValidator(new ResponseValidator<HttpClientResponse<ByteBuf>>() {
@Override
public void validate(HttpClientResponse<ByteBuf> t1) throws UnsuccessfulResponseException {
throw new UnsuccessfulResponseException("error", new IllegalArgumentException());
}
}).build();
RibbonRequest<ByteBuf> request = template.requestBuilder().build();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
request.toObservable().subscribe(new Action1<ByteBuf>() {
@Override
public void call(ByteBuf t1) {
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t1) {
error.set(t1);
latch.countDown();
}
}, new Action0() {
@Override
public void call() {
}
});
latch.await();
assertTrue(error.get() instanceof HystrixBadRequestException);
assertTrue(error.get().getCause() instanceof UnsuccessfulResponseException);
}
Aggregations