Search in sources :

Example 11 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class NettyClientTest method testPostWithObservable.

@Test
public void testPostWithObservable() throws Exception {
    Person myPerson = new Person("netty", 5);
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createPost(SERVICE_URI + "testAsync/person").withHeader("Content-type", "application/json").withContent(SerializationUtils.serializeToBytes(JacksonCodec.getInstance(), myPerson, null));
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient(DefaultClientConfigImpl.getClientConfigWithDefaultValues().set(CommonClientConfigKey.ReadTimeout, 10000));
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(new Server(host, port), request);
    Person person = getPersonObservable(response).toBlocking().single();
    assertEquals(myPerson, person);
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) Person(com.netflix.ribbon.test.resources.EmbeddedResources.Person) Test(org.junit.Test)

Example 12 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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)

Example 13 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse 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 14 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project Hystrix by Netflix.

the class HystrixMetricsStreamHandlerTest method testMetricsAreDeliveredAsSseStream.

@Test
public void testMetricsAreDeliveredAsSseStream() throws Exception {
    replayAll();
    Observable<ServerSentEvent> objectObservable = client.submit(HttpClientRequest.createGet(DEFAULT_HYSTRIX_PREFIX)).flatMap(new Func1<HttpClientResponse<ServerSentEvent>, Observable<? extends ServerSentEvent>>() {

        @Override
        public Observable<? extends ServerSentEvent> call(HttpClientResponse<ServerSentEvent> httpClientResponse) {
            return httpClientResponse.getContent().take(1);
        }
    });
    Object first = Observable.amb(objectObservable, Observable.timer(5000, TimeUnit.MILLISECONDS)).toBlocking().first();
    assertTrue("Expected SSE message", first instanceof ServerSentEvent);
    ServerSentEvent sse = (ServerSentEvent) first;
    JsonNode jsonNode = mapper.readTree(sse.contentAsString());
    assertEquals("Expected hystrix key name", HystrixCommandMetricsSamples.SAMPLE_1.getCommandKey().name(), jsonNode.get("name").asText());
}
Also used : HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ServerSentEvent(io.reactivex.netty.protocol.http.sse.ServerSentEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) Observable(rx.Observable) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with HttpClientResponse

use of io.reactivex.netty.protocol.http.client.HttpClientResponse in project ribbon by Netflix.

the class LoadBalancingExample method main.

public static void main(String[] args) throws Exception {
    List<Server> servers = Lists.newArrayList(new Server("www.google.com:80"), new Server("www.examples.com:80"), new Server("www.wikipedia.org:80"));
    BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(servers);
    LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
    final CountDownLatch latch = new CountDownLatch(servers.size());
    Observer<HttpClientResponse<ByteBuf>> observer = new Observer<HttpClientResponse<ByteBuf>>() {

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(HttpClientResponse<ByteBuf> args) {
            latch.countDown();
            System.out.println("Got response: " + args.getStatus());
        }
    };
    for (int i = 0; i < servers.size(); i++) {
        HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
        client.submit(request).subscribe(observer);
    }
    latch.await();
    System.out.println(lb.getLoadBalancerStats());
}
Also used : Server(com.netflix.loadbalancer.Server) Observer(rx.Observer) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) ByteBuf(io.netty.buffer.ByteBuf) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)21 ByteBuf (io.netty.buffer.ByteBuf)18 Test (org.junit.Test)16 Server (com.netflix.loadbalancer.Server)9 MockWebServer (com.google.mockwebserver.MockWebServer)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 IClientConfig (com.netflix.client.config.IClientConfig)5 Person (com.netflix.ribbon.test.resources.EmbeddedResources.Person)5 HttpServer (com.sun.net.httpserver.HttpServer)5 Observable (rx.Observable)5 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)4 AvailabilityFilteringRule (com.netflix.loadbalancer.AvailabilityFilteringRule)3 DummyPing (com.netflix.loadbalancer.DummyPing)3 ExecutionContext (com.netflix.loadbalancer.reactive.ExecutionContext)3 HttpResourceGroup (com.netflix.ribbon.http.HttpResourceGroup)3 HttpClientListener (io.reactivex.netty.servo.http.HttpClientListener)3 ClientException (com.netflix.client.ClientException)2 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 AbortExecutionException (com.netflix.loadbalancer.reactive.ExecutionListener.AbortExecutionException)2