use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method submitResponseTest.
/**
* Tests
* {@link FrontendRestRequestService#submitResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)}.
* @throws JSONException
* @throws UnsupportedEncodingException
* @throws URISyntaxException
*/
@Test
public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
String exceptionMsg = TestUtils.getRandomString(10);
responseHandler.shutdown();
// handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
try {
// there is an exception already.
RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
assertTrue("RestRequest channel is not open", restRequest.isOpen());
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg));
assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage());
// there is no exception and exception thrown when the response is submitted.
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
assertTrue("RestRequest channel is not open", restRequest.isOpen());
restResponseChannel = new MockRestResponseChannel();
ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
assertTrue("Response channel is not open", response.isOpen());
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null);
assertNotNull("There is no cause of failure", restResponseChannel.getException());
// resources should have been cleaned up.
assertFalse("Response channel is not cleaned up", response.isOpen());
} finally {
frontendRestRequestService.setupResponseHandler(responseHandler);
responseHandler.start();
}
// verify tracking infos are attached accordingly.
RestRequest restRequest;
MockRestResponseChannel restResponseChannel;
for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) {
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null);
}
restRequest = createRestRequest(RestMethod.GET, "/", null, null);
restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true));
restResponseChannel = new MockRestResponseChannel();
frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME));
assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME));
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method verifyResponsePathAccountAndContainerInjection.
/**
* Test response path account and container injection for V1 blob IDs.
* @param serviceId the service ID for the blob.
* @param isPrivate {@code true} if the blob is private.
* @param expectedAccount the expected {@link Account} to verify its presence in {@link RestRequest}.
* @param expectedContainer the expected {@link Container} to verify its presence in {@link RestRequest}.
* @throws Exception
*/
private void verifyResponsePathAccountAndContainerInjection(String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
BlobProperties blobProperties = new BlobProperties(0, serviceId, "owner", "image/gif", isPrivate, Utils.Infinite_Time, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false, null, null, null);
ReadableStreamChannel content = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
String blobId = router.putBlobWithIdVersion(blobProperties, new byte[0], content, BlobId.BLOB_ID_V1).get();
verifyAccountAndContainerFromBlobId(blobId, expectedAccount, expectedContainer, null);
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class GetAccountsHandlerTest method validRequestsTest.
/**
* Test valid request cases.
* @throws Exception
*/
@Test
public void validRequestsTest() throws Exception {
Account account = accountService.createAndAddRandomAccount();
ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
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("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
};
testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class TailoredPeersClusterMap method handleGoodCaseTest.
/**
* Tests for the good cases where the datanodes are correct. The peers obtained from the response is compared
* against the ground truth.
* @throws Exception
*/
@Test
public void handleGoodCaseTest() throws Exception {
for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
RestRequest restRequest = getRestRequest(datanode);
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)));
Set<String> expectedPeers = clusterMap.getPeers(datanode);
Set<String> peersFromResponse = getPeersFromResponse(RestTestUtils.getJsonizedResponseBody(channel));
assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
}
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class TailoredPeersClusterMap method sendRequestGetResponse.
/**
* Sends the given {@link RestRequest} to the {@link GetPeersHandler} and waits for the response and returns it.
* @param restRequest the {@link RestRequest} to send.
* @param restResponseChannel the {@link RestResponseChannel} where headers will be set.
* @return the response body as a {@link ReadableStreamChannel}.
* @throws Exception
*/
private ReadableStreamChannel sendRequestGetResponse(RestRequest restRequest, RestResponseChannel restResponseChannel) throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final AtomicReference<ReadableStreamChannel> channelRef = new AtomicReference<>();
getPeersHandler.handle(restRequest, restResponseChannel, new Callback<ReadableStreamChannel>() {
@Override
public void onCompletion(ReadableStreamChannel result, Exception exception) {
channelRef.set(result);
exceptionRef.set(exception);
latch.countDown();
}
});
assertTrue("Latch did not count down in time", latch.await(1, TimeUnit.SECONDS));
if (exceptionRef.get() != null) {
throw exceptionRef.get();
}
return channelRef.get();
}
Aggregations