Search in sources :

Example 1 with MapBackedKeySupplier

use of io.reactivex.netty.contexts.MapBackedKeySupplier in project ribbon by Netflix.

the class NettyClientTest method testContext.

@Test
public void testContext() throws Exception {
    HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/context");
    LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient();
    String requestId = "xyz";
    ContextsContainerImpl contextsContainer = new ContextsContainerImpl(new MapBackedKeySupplier());
    contextsContainer.addContext("Context1", "value1");
    RxContexts.DEFAULT_CORRELATOR.onNewServerRequest(requestId, contextsContainer);
    Observable<HttpClientResponse<ByteBuf>> response = observableClient.submit(new Server(host, port), request);
    final AtomicReference<ContextsContainer> responseContext = new AtomicReference<ContextsContainer>();
    String requestIdSent = response.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {

        @Override
        public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> t1) {
            return t1.getContent();
        }
    }).map(new Func1<ByteBuf, String>() {

        @Override
        public String call(ByteBuf t1) {
            String requestId = RxContexts.DEFAULT_CORRELATOR.getRequestIdForClientRequest();
            responseContext.set(RxContexts.DEFAULT_CORRELATOR.getContextForClientRequest(requestId));
            return t1.toString(Charset.defaultCharset());
        }
    }).toBlocking().single();
    assertEquals(requestId, requestIdSent);
    assertEquals("value1", responseContext.get().getContext("Context1"));
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Server(com.netflix.loadbalancer.Server) MockWebServer(com.google.mockwebserver.MockWebServer) MapBackedKeySupplier(io.reactivex.netty.contexts.MapBackedKeySupplier) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuf(io.netty.buffer.ByteBuf) ContextsContainerImpl(io.reactivex.netty.contexts.ContextsContainerImpl) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ContextsContainer(io.reactivex.netty.contexts.ContextsContainer) Func1(rx.functions.Func1) Test(org.junit.Test)

Aggregations

MockWebServer (com.google.mockwebserver.MockWebServer)1 Server (com.netflix.loadbalancer.Server)1 HttpServer (com.sun.net.httpserver.HttpServer)1 ByteBuf (io.netty.buffer.ByteBuf)1 ContextsContainer (io.reactivex.netty.contexts.ContextsContainer)1 ContextsContainerImpl (io.reactivex.netty.contexts.ContextsContainerImpl)1 MapBackedKeySupplier (io.reactivex.netty.contexts.MapBackedKeySupplier)1 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 Func1 (rx.functions.Func1)1