Search in sources :

Example 1 with RequestFinalizer

use of com.linkedin.r2.util.finalizer.RequestFinalizer in project rest.li by linkedin.

the class TestClientRequestFinalizerFilter method testStreamRequestOrdering.

@Test
public void testStreamRequestOrdering() {
    final AtomicReference<Observer> observerReference = new AtomicReference<>();
    final AtomicInteger addObserverExecutionOrder = new AtomicInteger(0);
    when(_streamResponse.getEntityStream()).thenReturn(_entityStream);
    doAnswer(invocation -> {
        addObserverExecutionOrder.set(_index.incrementAndGet());
        observerReference.set((Observer) invocation.getArguments()[0]);
        return null;
    }).when(_entityStream).addObserver(anyObject());
    _filterChain.onStreamRequest(null, _requestContext, null);
    registerRequestFinalizer();
    _filterChain.onStreamResponse(_streamResponse, _requestContext, null);
    observerReference.get().onDone();
    Assert.assertEquals(_firstFilter._onRequestExecutionOrder, 1);
    Assert.assertEquals(_lastFilter._onRequestExecutionOrder, 2);
    Assert.assertEquals(_lastFilter._onResponseExecutionOrder, 3);
    Assert.assertEquals(addObserverExecutionOrder.get(), 4, "Expected observer with RequestFinalizer " + "to be added before calling the next filter.");
    Assert.assertEquals(_firstFilter._onResponseExecutionOrder, 5);
    Assert.assertEquals(_testRequestFinalizer._executionOrder, 6, "Expected request finalizer to be " + "executed last.");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(com.linkedin.r2.message.stream.entitystream.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.testng.annotations.Test)

Aggregations

Observer (com.linkedin.r2.message.stream.entitystream.Observer)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.testng.annotations.Test)1