use of com.linkedin.parseq.Task in project rest.li by linkedin.
the class ParseqTraceDebugRequestHandler method handleRequest.
@Override
public void handleRequest(final RestRequest request, final RequestContext context, final ResourceDebugRequestHandler resourceDebugRequestHandler, final Callback<RestResponse> callback) {
// Find out the path coming after the "__debug" path segment
String fullPath = request.getURI().getPath();
int debugSegmentIndex = fullPath.indexOf(DelegatingDebugRequestHandler.DEBUG_PATH_SEGMENT);
final String debugHandlerPath = fullPath.substring(debugSegmentIndex + DelegatingDebugRequestHandler.DEBUG_PATH_SEGMENT.length() + 1);
assert (debugHandlerPath.startsWith(HANDLER_ID));
// Decide whether this is a user issued debug request or a follow up static content request for tracevis html.
if (debugHandlerPath.equals(TRACEVIS_PATH) || debugHandlerPath.equals(RAW_PATH)) {
if (context.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER) != null) {
callback.onError(new Exception("Unexpected PromiseListener in local attributes: " + context.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER)));
}
// This listener is registered to the resource execution task in RestLiMethodInvoker. Upon resolution of the task,
// this listener gets the trace for the task and send it through the callback.
context.putLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER, new PromiseListener<Object>() {
@Override
public void onResolved(Promise<Object> promise) {
try {
sendDebugResponse(callback, ((Task<?>) promise).getTrace(), debugHandlerPath);
} catch (Throwable e) {
callback.onError(e);
}
}
});
resourceDebugRequestHandler.handleRequest(request, context, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
// No-op. We only care about the ParSeq trace but not the execution result, be it successful or not.
}
@Override
public void onSuccess(RestResponse result) {
// No-op. We only care about the ParSeq trace but not the execution result, be it successful or not.
}
});
} else {
// We know that the request is a static content request. So here we figure out the internal path for the
// JAR resource from the request path. A request uri such as "/__debug/parseqtrace/trace.html" translates to
// "/tracevis/trace.html" for the resource path.
String resourcePath = debugHandlerPath.replaceFirst(HANDLER_ID, ENTRY_PATH_SEGMENT_TRACEVIS);
ClassLoader currentClassLoader = getClass().getClassLoader();
InputStream resourceStream = currentClassLoader.getResourceAsStream(resourcePath);
String mediaType = null;
if (resourceStream != null) {
mediaType = determineMediaType(resourcePath);
}
// If the requested file type is not supported by this debug request handler, return 404.
if (mediaType == null) {
callback.onError(new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
}
try {
sendByteArrayAsResponse(callback, IOUtils.toByteArray(resourceStream), mediaType);
} catch (IOException exception) {
callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, exception));
}
}
}
use of com.linkedin.parseq.Task in project rest.li by linkedin.
the class MockBatchingStrategy method testAddToGroupAndExecute_MultipleGroup.
@Test
public void testAddToGroupAndExecute_MultipleGroup() throws Exception {
// Task will be called twice in two groups
Task mockTask = mock(Task.class);
ExecutionGroup eg2 = new ExecutionGroup(_engine);
eg.addTaskByFluentClient(client1, mockTask);
eg2.addTaskByFluentClient(client1, mockTask);
eg.execute();
eg2.execute();
CountDownLatch waitLatch = new CountDownLatch(2);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
waitLatch.countDown();
return null;
}
}).when(mockTask).contextRun(any(), any(), any());
waitLatch.await(1000, TimeUnit.MILLISECONDS);
verify(mockTask, times(2)).contextRun(any(), any(), any());
}
use of com.linkedin.parseq.Task in project rest.li by linkedin.
the class TestParseqTraceDebugRequestHandler method executeRequestThroughParseqDebugHandler.
private void executeRequestThroughParseqDebugHandler(URI uri, Callback<RestResponse> callback) {
ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
RestRequest request = requestBuilder.build();
RequestContext requestContext = new RequestContext();
requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {
@Override
@SuppressWarnings("unchecked")
public void handleRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
RestResponse response = EasyMock.createMock(RestResponse.class);
JsonTraceCodec jsonTraceCodec = new JsonTraceCodec();
Trace t = null;
try {
t = jsonTraceCodec.decode(TEST_TRACE);
} catch (IOException exc) {
// test will fail later
}
Task<Object> task = EasyMock.createMock(Task.class);
EasyMock.expect(task.getTrace()).andReturn(t);
EasyMock.replay(task);
PromiseListener<Object> promiseListener = (PromiseListener<Object>) requestContext.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER);
promiseListener.onResolved(task);
callback.onSuccess(response);
}
}, callback);
}
Aggregations