Search in sources :

Example 1 with HttpResourceGroup

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);
}
Also used : DiscoveryEnabledNIWSServerList(com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test) MockedDiscoveryServerListTest(com.netflix.ribbon.testutils.MockedDiscoveryServerListTest)

Example 2 with HttpResourceGroup

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);
}
Also used : SampleHttpResourceGroup(com.netflix.ribbon.proxy.sample.ResourceGroupClasses.SampleHttpResourceGroup) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup)

Example 3 with HttpResourceGroup

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());
}
Also used : ObservableConnection(io.reactivex.netty.channel.ObservableConnection) RibbonTransportFactory(com.netflix.ribbon.RibbonTransportFactory) MetricEventsListener(io.reactivex.netty.metrics.MetricEventsListener) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) SampleMovieService(com.netflix.ribbon.proxy.sample.MovieServiceInterfaces.SampleMovieService) ClientMetricsEvent(io.reactivex.netty.client.ClientMetricsEvent) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) IClientConfig(com.netflix.client.config.IClientConfig) IClientConfig(com.netflix.client.config.IClientConfig) Test(org.junit.Test)

Example 4 with HttpResourceGroup

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);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 5 with HttpResourceGroup

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);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) Action0(rx.functions.Action0) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) MockWebServer(com.google.mockwebserver.MockWebServer) Test(org.junit.Test)

Aggregations

HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)13 ByteBuf (io.netty.buffer.ByteBuf)11 Test (org.junit.Test)10 MockResponse (com.google.mockwebserver.MockResponse)6 MockWebServer (com.google.mockwebserver.MockWebServer)6 HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Map (java.util.Map)5 IOException (java.io.IOException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Observable (rx.Observable)4 Func1 (rx.functions.Func1)4 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HystrixInvokableInfo (com.netflix.hystrix.HystrixInvokableInfo)2 FallbackHandler (com.netflix.ribbon.hystrix.FallbackHandler)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 IClientConfig (com.netflix.client.config.IClientConfig)1 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)1