Search in sources :

Example 1 with RequestFinalizerManagerImpl

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

the class TestClientRequestFinalizerFilter method testExistingRequestFinalizerManager.

@Test
public void testExistingRequestFinalizerManager() {
    final RequestFinalizerManagerImpl manager = new RequestFinalizerManagerImpl(null, null);
    final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    manager.registerRequestFinalizer((request, response, requestContext1, throwable) -> atomicBoolean.set(true));
    _requestContext.putLocalAttr(R2Constants.CLIENT_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, manager);
    _filterChain.onRestRequest(null, _requestContext, null);
    registerRequestFinalizer();
    _filterChain.onRestResponse(null, _requestContext, null);
    assertExecutionOrders();
    Assert.assertTrue(atomicBoolean.get(), "Expected the request finalizer registered before reaching the" + "ClientRequestFinalizerFilter to still be invoked.");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestFinalizerManagerImpl(com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl) Test(org.testng.annotations.Test)

Example 2 with RequestFinalizerManagerImpl

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

the class TestRequestContextUtil method testGetServerRequestFinalizerManager.

@Test
public void testGetServerRequestFinalizerManager() {
    Assert.assertNull(RequestContextUtil.getServerRequestFinalizerManager(_requestContext));
    _requestContext.putLocalAttr(R2Constants.SERVER_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, new RequestFinalizerManagerImpl(null, null));
    Assert.assertNotNull(RequestContextUtil.getServerRequestFinalizerManager(_requestContext));
}
Also used : RequestFinalizerManagerImpl(com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl) Test(org.testng.annotations.Test)

Example 3 with RequestFinalizerManagerImpl

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

the class TestRequestContextUtil method testGetClientRequestFinalizerManager.

@Test
public void testGetClientRequestFinalizerManager() {
    Assert.assertNull(RequestContextUtil.getClientRequestFinalizerManager(_requestContext));
    _requestContext.putLocalAttr(R2Constants.CLIENT_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, new RequestFinalizerManagerImpl(null, null));
    Assert.assertNotNull(RequestContextUtil.getClientRequestFinalizerManager(_requestContext));
}
Also used : RequestFinalizerManagerImpl(com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl) Test(org.testng.annotations.Test)

Example 4 with RequestFinalizerManagerImpl

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

the class TestRequestFinalizerDispatcher method testExistingRequestFinalizerManager.

@Test
public void testExistingRequestFinalizerManager() {
    when(_restTransportResponse.getResponse()).thenReturn(_restResponse);
    final RequestContext requestContext = new RequestContext();
    final RequestFinalizerManagerImpl manager = new RequestFinalizerManagerImpl(null, requestContext);
    final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    manager.registerRequestFinalizer((request, response, requestContext1, throwable) -> atomicBoolean.set(true));
    requestContext.putLocalAttr(R2Constants.SERVER_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, manager);
    final TestTransportCallback<RestResponse> transportCallback = new TestTransportCallback<>(false);
    _outerDispatcher.handleRestRequest(null, null, requestContext, transportCallback);
    Assert.assertEquals(_outerDispatcher._executionOrder, 1);
    Assert.assertEquals(_innerDispatcher._executionOrder, 2);
    Assert.assertEquals(_innerDispatcher._transportCallback._executionOrder, 3);
    Assert.assertEquals(_outerDispatcher._transportCallback._executionOrder, 4);
    Assert.assertEquals(transportCallback._executionOrder, 5);
    Assert.assertEquals(_requestFinalizer._executionOrder, 6, "Expected request to be finalized after the callback.");
    Assert.assertTrue(atomicBoolean.get(), "Expected the request finalizer registered before reaching the" + "RequestFinalizerDispatcher to still be invoked.");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 5 with RequestFinalizerManagerImpl

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

the class ClientRequestFinalizerFilter method handleRequest.

private <REQ extends Request, RES extends Response> void handleRequest(REQ request, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<REQ, RES> nextFilter) {
    final RequestFinalizerManagerImpl manager = (RequestFinalizerManagerImpl) requestContext.getLocalAttr(R2Constants.CLIENT_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY);
    if (manager == null) {
        requestContext.putLocalAttr(R2Constants.CLIENT_REQUEST_FINALIZER_MANAGER_REQUEST_CONTEXT_KEY, new RequestFinalizerManagerImpl(request, requestContext));
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("A RequestFinalizerManager already exists in the RequestContext.\nRequest ID: %s\nRequest: %s\nRequestContext ID: %s" + "\nRequestContext: %s", System.identityHashCode(request), request, System.identityHashCode(requestContext), requestContext), new RuntimeException());
        }
    }
    nextFilter.onRequest(request, requestContext, wireAttrs);
}
Also used : RequestFinalizerManagerImpl(com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl)

Aggregations

RequestFinalizerManagerImpl (com.linkedin.r2.util.finalizer.RequestFinalizerManagerImpl)4 Test (org.testng.annotations.Test)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RequestContext (com.linkedin.r2.message.RequestContext)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1