use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.
the class TestMultiplexedRequestBuilder method testBody.
@Test
public void testBody() throws IOException {
TestRecord entity = fakeEntity(0);
CreateRequest<TestRecord> request = fakeCreateRequest(entity);
NoOpCallback<EmptyRecord> callback = new NoOpCallback<>();
MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createSequentialRequest().addRequest(request, callback).build();
IndividualRequest individualRequest = new IndividualRequest().setMethod(HttpMethod.POST.name()).setHeaders(new StringMap(HEADERS)).setRelativeUrl(BASE_URI).setBody(new IndividualBody(entity.data()));
MultiplexedRequestContent expectedRequests = new MultiplexedRequestContent();
expectedRequests.setRequests(new IndividualRequestMap(ImmutableMap.of("0", individualRequest)));
assertMultiplexedRequestContentEquals(multiplexedRequest.getContent(), expectedRequests);
}
use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.
the class TestMultiplexedRequestBuilder method testParallel.
@Test
public void testParallel() throws RestLiEncodingException {
MultiplexedRequest multiplexedRequest = MultiplexedRequestBuilder.createParallelRequest().addRequest(request1, callback1).addRequest(request2, callback2).build();
// verify requests
IndividualRequest ir1 = fakeIndividualRequest(getUri(ID1));
IndividualRequest ir2 = fakeIndividualRequest(getUri(ID2));
MultiplexedRequestContent expectedRequests = new MultiplexedRequestContent();
expectedRequests.setRequests(new IndividualRequestMap(ImmutableMap.of("0", ir1, "1", ir2)));
assertMultiplexedRequestContentEquals(multiplexedRequest.getContent(), expectedRequests);
verifyCallbacks(multiplexedRequest);
}
use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.
the class MultiplexedRequestHandlerImpl method createParallelRequestsTask.
private Task<?> createParallelRequestsTask(RestRequest envelopeRequest, RequestContext requestContext, IndividualRequestMap individualRequests, IndividualResponseMap individualResponses, Map<String, HttpCookie> responseCookies) {
List<Task<?>> tasks = new ArrayList<>(individualRequests.size());
for (IndividualRequestMap.Entry<String, IndividualRequest> individualRequestMapEntry : individualRequests.entrySet()) {
String id = individualRequestMapEntry.getKey();
IndividualRequest individualRequest = individualRequestMapEntry.getValue();
// create a task for the current request
Task<Void> individualRequestTask = createRequestHandlingTask(id, envelopeRequest, requestContext, individualRequest, individualResponses, responseCookies);
IndividualRequestMap dependentRequests = individualRequest.getDependentRequests();
if (dependentRequests.isEmpty()) {
tasks.add(individualRequestTask);
} else {
// recursively process dependent requests
Task<?> dependentRequestsTask = createParallelRequestsTask(envelopeRequest, requestContext, dependentRequests, individualResponses, responseCookies);
// tasks for dependant requests are executed after the current request's task
tasks.add(individualRequestTask.andThen(dependentRequestsTask));
}
}
return Task.par(tasks);
}
use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.
the class TestAsyncMethodInvocationPlanClass method individualRequest.
private static IndividualRequest individualRequest(String url, Map<String, String> headers, Map<String, IndividualRequest> dependentRequests) {
IndividualRequest individualRequest = new IndividualRequest();
individualRequest.setMethod(HttpMethod.GET.name());
individualRequest.setRelativeUrl(url);
if (headers != null && headers.size() > 0) {
individualRequest.setHeaders(new StringMap(headers));
}
individualRequest.setDependentRequests(new IndividualRequestMap(dependentRequests));
return individualRequest;
}
use of com.linkedin.restli.common.multiplexer.IndividualRequest in project rest.li by linkedin.
the class TestAsyncMethodInvocationPlanClass method testMultiplexedAsyncGet.
@Test(dataProvider = "multiplexerConfigurations")
public void testMultiplexedAsyncGet(MultiplexerRunMode multiplexerRunMode) throws URISyntaxException, IOException, InterruptedException {
RestLiConfig config = new RestLiConfig();
config.addResourcePackageNames("com.linkedin.restli.server.multiplexer.resources");
config.setMultiplexerRunMode(multiplexerRunMode);
SettablePromise<Trace> traceHolder = Promises.settable();
Engine engine = engine(traceHolder);
RestLiServer server = new RestLiServer(config, resourceFactory(), engine);
IndividualRequest r0 = individualRequest("/users/0", null, Collections.<String, IndividualRequest>emptyMap());
IndividualRequest r1 = individualRequest("/users/1", null, Collections.<String, IndividualRequest>emptyMap());
IndividualRequest r2 = individualRequest("/users/2", null, ImmutableMap.of("0", r0, "1", r1));
// request is seq(par(r0, r1), r2)
RestRequest request = muxRestRequest(ImmutableMap.of("2", r2));
CountDownLatch latch = new CountDownLatch(1);
server.handleRequest(request, new RequestContext(), callback(latch));
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertTrue(traceHolder.await(5, TimeUnit.SECONDS));
if (multiplexerRunMode == MultiplexerRunMode.SINGLE_PLAN) {
// For multiplexed requests in SINGLE_PLAN mode there is only one plan with class "mux"
assertEquals(traceHolder.get().getPlanClass(), "mux");
} else {
// For multiplexed requests in MULTIPLE_PLANS mode there are multiple plans, first one is with class "mux",
// following 3 are with class "resource=users,method=get", last one will have class "resource=users,method=get"
assertEquals(traceHolder.get().getPlanClass(), "resource=users,method=get");
}
}
Aggregations