use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class UndeleteHandlerTest method setupBlob.
/**
* Setup a blob and delete it so later we can undelete it.
* @throws Exception
*/
public void setupBlob() throws Exception {
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(BLOB_DATA));
blobId = router.putBlob(BLOB_PROPERTIES, new byte[0], channel, new PutBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
idConverterFactory.translation = blobId;
router.deleteBlob(blobId, SERVICE_ID).get(1, TimeUnit.SECONDS);
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class GetClusterMapSnapshotHandlerTest method handleGoodCaseTest.
/**
* Handles the case where everything works as expected
* @throws Exception
*/
@Test
public void handleGoodCaseTest() throws Exception {
RestRequest restRequest = createRestRequest();
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
JSONObject expected = clusterMap.getSnapshot();
JSONObject actual = RestTestUtils.getJsonizedResponseBody(channel);
// remove timestamps because they may differ
expected.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
actual.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
assertEquals("Snapshot does not match expected", expected.toString(), actual.toString());
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class GetAccountsHandlerTest method getSingleContainerSuccessTest.
/**
* Test success case of getting single container.
* @throws Exception
*/
@Test
public void getSingleContainerSuccessTest() throws Exception {
Account existingAccount = accountService.createAndAddRandomAccount();
Container existingContainer = existingAccount.getAllContainers().iterator().next();
ThrowingBiConsumer<RestRequest, Container> testAction = (request, expectedContainer) -> {
RestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
assertNotNull("There should be a response", channel);
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
channel.readInto(asyncWritableChannel, null).get();
assertEquals("Container does not match", Collections.singletonList(expectedContainer), AccountCollectionSerde.containersFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream(), existingAccount.getId()));
};
testAction.accept(createRestRequest(existingAccount.getName(), null, existingContainer.getName(), Operations.ACCOUNTS_CONTAINERS), existingContainer);
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class BadRestRequest method multipleResponsesInFlightTest.
/**
* Tests various functions when multiple responses are in flight.
* @throws Exception
*/
@Test
public void multipleResponsesInFlightTest() throws Exception {
ExecutorService executorService = Executors.newSingleThreadExecutor();
AsyncRequestResponseHandler responseHandler = getAsyncRequestResponseHandler(0);
responseHandler.start();
try {
final int EXPECTED_QUEUE_SIZE = 5;
// test for duplicate request submission
// submit a few responses that halt on read
CountDownLatch releaseRead = new CountDownLatch(1);
byte[] data = null;
RestRequest restRequest = null;
MockRestResponseChannel restResponseChannel = null;
ReadableStreamChannel response = null;
for (int i = 0; i < EXPECTED_QUEUE_SIZE; i++) {
data = TestUtils.getRandomBytes(32);
response = new HaltingRSC(ByteBuffer.wrap(data), releaseRead, executorService);
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
restResponseChannel = new MockRestResponseChannel(restRequest);
responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
}
assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
// attach a listener to the last MockRestResponseChannel
EventMonitor<MockRestResponseChannel.Event> eventMonitor = new EventMonitor<MockRestResponseChannel.Event>(MockRestResponseChannel.Event.OnRequestComplete);
restResponseChannel.addListener(eventMonitor);
// we try to re submit the last response. This should fail.
try {
responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
fail("Trying to resubmit a response that is already in flight should have failed");
} catch (RestServiceException e) {
assertEquals("Unexpected error code", RestServiceErrorCode.RequestResponseQueuingFailure, e.getErrorCode());
}
assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
releaseRead.countDown();
if (!eventMonitor.awaitEvent(1, TimeUnit.SECONDS)) {
fail("awaitResponse took too long. There might be a problem or the timeout may need to be increased");
}
// compare the output. We care only about the last one because we want to make sure the duplicate submission
// did not mess with the original
assertArrayEquals("Unexpected data in the response", data, restResponseChannel.getResponseBody());
// test for shutdown when responses are still in progress
releaseRead = new CountDownLatch(1);
List<MockRestResponseChannel> responseChannels = new ArrayList<MockRestResponseChannel>(EXPECTED_QUEUE_SIZE);
for (int i = 0; i < EXPECTED_QUEUE_SIZE; i++) {
response = new HaltingRSC(ByteBuffer.allocate(0), releaseRead, executorService);
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
restResponseChannel = new MockRestResponseChannel(restRequest);
responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
responseChannels.add(restResponseChannel);
}
assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
responseHandler.shutdown();
assertEquals("Response set size unexpected", 0, responseHandler.getResponseSetSize());
releaseRead.countDown();
// all of the responses should have exceptions
for (MockRestResponseChannel channel : responseChannels) {
assertNotNull("There should be an exception", channel.getException());
if (channel.getException() instanceof RestServiceException) {
RestServiceException rse = (RestServiceException) channel.getException();
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.ServiceUnavailable, rse.getErrorCode());
} else {
throw channel.getException();
}
}
} finally {
responseHandler.shutdown();
executorService.shutdown();
}
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class MockHeadCallback method onCompletion.
/**
* If there was no exception, sets headers and submits response.
* @param result The result of the request - a {@link GetBlobResult} object with the {@link BlobInfo} containing the
* blob properties and other headers of the blob, and the {@link ReadableStreamChannel} of blob data.
* This is non null if the request executed successfully.
* @param exception The exception that was reported on execution of the request (if any).
*/
@Override
public void onCompletion(GetBlobResult result, Exception exception) {
try {
restResponseChannel.setHeader(RestUtils.Headers.DATE, new GregorianCalendar().getTime());
if (exception == null && result != null) {
setResponseHeaders(result.getBlobInfo());
} else if (exception != null && exception instanceof RouterException) {
exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode()));
}
} catch (Exception e) {
exception = exception == null ? e : exception;
} finally {
ReadableStreamChannel channel = result != null ? result.getBlobDataChannel() : null;
mockRestRequestService.handleResponse(restRequest, restResponseChannel, channel, exception);
}
}
Aggregations