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 FrontendTestUrlSigningServiceFactory method releaseResourcesTest.
/**
* Tests releasing of resources if response submission fails.
* @throws JSONException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
@Test
public void releaseResourcesTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
responseHandler.shutdown();
// handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
try {
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
assertTrue("RestRequest channel not open", restRequest.isOpen());
assertTrue("ReadableStreamChannel not open", channel.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, channel, null);
assertFalse("ReadableStreamChannel is still open", channel.isOpen());
// null ReadableStreamChannel
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
assertTrue("RestRequest channel not open", restRequest.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
// bad RestRequest (close() throws IOException)
channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
restResponseChannel = new MockRestResponseChannel();
assertTrue("ReadableStreamChannel not open", channel.isOpen());
frontendRestRequestService.submitResponse(new BadRestRequest(), restResponseChannel, channel, null);
// bad ReadableStreamChannel (close() throws IOException)
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
assertTrue("RestRequest channel not open", restRequest.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, new BadRSC(), null);
} finally {
frontendRestRequestService.setupResponseHandler(responseHandler);
responseHandler.start();
}
}
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 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 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();
}
}
Aggregations