use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class GetClusterMapSnapshotHandlerTest method sendRequestGetResponse.
/**
* Sends the given {@link RestRequest} to the {@link GetClusterMapSnapshotHandler} 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<>();
handler.handle(restRequest, restResponseChannel, (result, 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();
}
use of com.github.ambry.router.ReadableStreamChannel 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());
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class ServerTestUtil method checkBlobId.
private static void checkBlobId(Router router, BlobId blobId, byte[] data) throws Exception {
GetBlobResult result = router.getBlob(blobId.getID(), new GetBlobOptionsBuilder().build()).get(20, TimeUnit.SECONDS);
ReadableStreamChannel blob = result.getBlobDataChannel();
assertEquals("Size does not match that of data", data.length, result.getBlobInfo().getBlobProperties().getBlobSize());
RetainingAsyncWritableChannel channel = new RetainingAsyncWritableChannel();
blob.readInto(channel, null).get(1, TimeUnit.SECONDS);
try (InputStream is = channel.consumeContentAsInputStream()) {
assertArrayEquals(data, Utils.readBytesFromStream(is, (int) channel.getBytesWritten()));
}
}
use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.
the class RouterServerTestFramework method startPutBlob.
/**
* Submit a putBlob operation.
* @param opChain the {@link OperationChain} object that this operation is a part of.
*/
private void startPutBlob(OperationChain opChain) {
ReadableStreamChannel putChannel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(opChain.data));
Callback<String> callback = new TestCallback<String>(opChain, false) {
@Override
void action(String result) {
opChain.blobId = result;
}
};
Future<String> future = router.putBlob(opChain.properties, opChain.userMetadata, putChannel, new PutBlobOptionsBuilder().build(), callback, quotaChargeCallback);
TestFuture<String> testFuture = new TestFuture<String>(future, genLabel("putBlob", false), opChain) {
@Override
void check() throws Exception {
checkBlobId(get(), opChain.properties, getOperationName());
}
};
opChain.testFutures.add(testFuture);
}
Aggregations