Search in sources :

Example 1 with ByteBufferRSC

use of com.github.ambry.router.ByteBufferRSC in project ambry by linkedin.

the class BadRestRequest method doHandleResponseSuccessTest.

/**
 * Tests {@link AsyncRequestResponseHandler#handleResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)} with good input.
 * @throws Exception
 */
private void doHandleResponseSuccessTest(AsyncRequestResponseHandler asyncRequestResponseHandler) throws Exception {
    // both response and exception null
    MockRestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    awaitResponse(asyncRequestResponseHandler, restRequest, restResponseChannel, null, null);
    if (restResponseChannel.getException() != null) {
        throw restResponseChannel.getException();
    }
    // both response and exception not null
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    ByteBuffer responseBuffer = ByteBuffer.wrap(TestUtils.getRandomBytes(1024));
    ByteBufferRSC response = new ByteBufferRSC(responseBuffer);
    EventMonitor<ByteBufferRSC.Event> responseCloseMonitor = new EventMonitor<ByteBufferRSC.Event>(ByteBufferRSC.Event.Close);
    response.addListener(responseCloseMonitor);
    Exception e = new Exception();
    awaitResponse(asyncRequestResponseHandler, restRequest, restResponseChannel, response, e);
    // make sure exception was correctly sent to the RestResponseChannel.
    assertEquals("Exception was not piped correctly", e, restResponseChannel.getException());
    assertTrue("Response is not closed", responseCloseMonitor.awaitEvent(1, TimeUnit.SECONDS));
    // response null but exception not null.
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    e = new Exception();
    awaitResponse(asyncRequestResponseHandler, restRequest, restResponseChannel, null, e);
    // make sure exception was correctly sent to the RestResponseChannel.
    assertEquals("Exception was not piped correctly", e, restResponseChannel.getException());
    // response not null.
    // steady response - full response available.
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    responseBuffer = ByteBuffer.wrap(TestUtils.getRandomBytes(1024));
    response = new ByteBufferRSC(responseBuffer);
    responseCloseMonitor = new EventMonitor<ByteBufferRSC.Event>(ByteBufferRSC.Event.Close);
    response.addListener(responseCloseMonitor);
    awaitResponse(asyncRequestResponseHandler, restRequest, restResponseChannel, response, null);
    if (restResponseChannel.getException() == null) {
        assertArrayEquals("Response does not match", responseBuffer.array(), restResponseChannel.getResponseBody());
        assertTrue("Response is not closed", responseCloseMonitor.awaitEvent(1, TimeUnit.SECONDS));
    } else {
        throw restResponseChannel.getException();
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBufferRSC(com.github.ambry.router.ByteBufferRSC)

Example 2 with ByteBufferRSC

use of com.github.ambry.router.ByteBufferRSC in project ambry by linkedin.

the class BadRestRequest method handleResponseExceptionTest.

/**
 * Tests the reaction of {@link AsyncRequestResponseHandler#handleResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)} to some misbehaving components.
 * @throws Exception
 */
@Test
public void handleResponseExceptionTest() throws Exception {
    ByteBufferRSC response = new ByteBufferRSC(ByteBuffer.allocate(0));
    // RestRequest null.
    try {
        asyncRequestResponseHandler.handleResponse(null, new MockRestResponseChannel(), response, null);
        fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
    // expected. Nothing to do.
    }
    // RestResponseChannel null.
    MockRestRequest restRequest = createRestRequest(RestMethod.GET, "/", new JSONObject(), null);
    restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    try {
        asyncRequestResponseHandler.handleResponse(restRequest, null, response, null);
        fail("Test should have thrown exception, but did not");
    } catch (IllegalArgumentException e) {
    // expected. Nothing to do.
    }
    // Writing response throws exception.
    MockRestRequest goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
    String exceptionMsg = "@@randomMsg@@";
    ReadableStreamChannel badResponse = new IncompleteReadReadableStreamChannel(null, new Exception(exceptionMsg));
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Exception message does not match", exceptionMsg, restResponseChannel.getException().getMessage());
    // Writing response is incomplete
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(0L, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());
    // No bytes read and no exception
    goodRestRequest = createRestRequest(RestMethod.GET, "/", null, null);
    goodRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    badResponse = new IncompleteReadReadableStreamChannel(null, null);
    awaitResponse(asyncRequestResponseHandler, goodRestRequest, restResponseChannel, badResponse, null);
    assertNotNull("MockRestResponseChannel would have been passed an exception", restResponseChannel.getException());
    assertEquals("Unexpected exception", IllegalStateException.class, restResponseChannel.getException().getClass());
    // RestRequest is bad.
    BadRestRequest badRestRequest = new BadRestRequest();
    badRestRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
    restResponseChannel = new MockRestResponseChannel();
    ByteBufferRSC goodResponse = new ByteBufferRSC(ByteBuffer.allocate(0));
    EventMonitor<ByteBufferRSC.Event> responseCloseMonitor = new EventMonitor<ByteBufferRSC.Event>(ByteBufferRSC.Event.Close);
    goodResponse.addListener(responseCloseMonitor);
    awaitResponse(asyncRequestResponseHandler, badRestRequest, restResponseChannel, goodResponse, null);
    assertTrue("Response is not closed", responseCloseMonitor.awaitEvent(1, TimeUnit.SECONDS));
    // AsyncRequestResponseHandler should still be alive and serving requests
    assertTrue("AsyncRequestResponseHandler is dead", asyncRequestResponseHandler.isRunning());
}
Also used : URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteBufferRSC(com.github.ambry.router.ByteBufferRSC) JSONObject(org.json.JSONObject) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) Test(org.junit.Test)

Example 3 with ByteBufferRSC

use of com.github.ambry.router.ByteBufferRSC in project ambry by linkedin.

the class MockHeadCallback method shouldProceed.

/**
 * Handles argument pre-checks and examines the URL to see if any custom operations need to be performed (which might
 * involve throwing exceptions).
 * <p/>
 * Also blocks if required.
 * @param restRequest the {@link RestRequest} that needs to be handled.
 * @param restResponseChannel the {@link RestResponseChannel} that can be used to set headers.
 * @return {@code true} if the pre-checks decided it is OK to continue. Otherwise {@code false}.
 */
private boolean shouldProceed(RestRequest restRequest, RestResponseChannel restResponseChannel) {
    if (blocking) {
        try {
            blockLatch.await();
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
    boolean shouldProceed = canHonorRequest(restRequest, restResponseChannel);
    if (shouldProceed) {
        String uri = restRequest.getUri();
        ReadableStreamChannel response = null;
        Exception exception = null;
        if (uri.startsWith(ECHO_REST_METHOD)) {
            String responseStr = restRequest.getRestMethod().toString() + uri.substring(ECHO_REST_METHOD.length());
            ByteBuffer buffer = ByteBuffer.wrap(responseStr.getBytes());
            response = new ByteBufferRSC(buffer);
            shouldProceed = false;
        } else if (THROW_RUNTIME_EXCEPTION.equals(uri)) {
            throw new RuntimeException(THROW_RUNTIME_EXCEPTION);
        } else if (SEND_RESPONSE_RUNTIME_EXCEPTION.equals(uri)) {
            shouldProceed = false;
            exception = new RuntimeException(SEND_RESPONSE_RUNTIME_EXCEPTION);
        } else if (SEND_RESPONSE_REST_SERVICE_EXCEPTION.equals(uri)) {
            shouldProceed = false;
            RestServiceErrorCode errorCode = RestServiceErrorCode.InternalServerError;
            try {
                errorCode = RestServiceErrorCode.valueOf(verifiableProperties.getString(REST_ERROR_CODE));
            } catch (IllegalArgumentException e) {
            // it's alright.
            }
            exception = new RestServiceException(SEND_RESPONSE_REST_SERVICE_EXCEPTION, errorCode);
        }
        if (!shouldProceed) {
            try {
                if (exception == null) {
                    restResponseChannel.setStatus(ResponseStatus.Ok);
                }
            } catch (RestServiceException e) {
                exception = e;
            } finally {
                handleResponse(restRequest, restResponseChannel, response, exception);
            }
        }
    }
    return shouldProceed;
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) ByteBufferRSC(com.github.ambry.router.ByteBufferRSC)

Aggregations

ByteBufferRSC (com.github.ambry.router.ByteBufferRSC)3 IOException (java.io.IOException)3 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 ByteBuffer (java.nio.ByteBuffer)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 JSONException (org.json.JSONException)2 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)1 RouterException (com.github.ambry.router.RouterException)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1