Search in sources :

Example 6 with HttpResourceGroup

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);
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 7 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 8 with HttpResourceGroup

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);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) FallbackHandler(com.netflix.ribbon.hystrix.FallbackHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Func1(rx.functions.Func1) Map(java.util.Map) HystrixInvokableInfo(com.netflix.hystrix.HystrixInvokableInfo) Test(org.junit.Test)

Example 9 with HttpResourceGroup

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);
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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 10 with HttpResourceGroup

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();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpResourceGroup(com.netflix.ribbon.http.HttpResourceGroup) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) ByteBuf(io.netty.buffer.ByteBuf) 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