use of io.reactivex.netty.contexts.ContextsContainer 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"));
}
Aggregations