Search in sources :

Example 1 with IndividualRequest

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);
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) StringMap(com.linkedin.data.template.StringMap) IndividualBody(com.linkedin.restli.common.multiplexer.IndividualBody) MultiplexedRequestContent(com.linkedin.restli.common.multiplexer.MultiplexedRequestContent) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 2 with IndividualRequest

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);
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) MultiplexedRequestContent(com.linkedin.restli.common.multiplexer.MultiplexedRequestContent) Test(org.testng.annotations.Test)

Example 3 with IndividualRequest

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);
}
Also used : IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) Task(com.linkedin.parseq.Task) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString)

Example 4 with IndividualRequest

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;
}
Also used : IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) IndividualRequestMap(com.linkedin.restli.common.multiplexer.IndividualRequestMap) StringMap(com.linkedin.data.template.StringMap)

Example 5 with 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");
    }
}
Also used : Trace(com.linkedin.parseq.trace.Trace) IndividualRequest(com.linkedin.restli.common.multiplexer.IndividualRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServer(com.linkedin.restli.server.RestLiServer) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiConfig(com.linkedin.restli.server.RestLiConfig) Engine(com.linkedin.parseq.Engine) Test(org.testng.annotations.Test)

Aggregations

IndividualRequest (com.linkedin.restli.common.multiplexer.IndividualRequest)21 IndividualRequestMap (com.linkedin.restli.common.multiplexer.IndividualRequestMap)12 Test (org.testng.annotations.Test)12 RequestContext (com.linkedin.r2.message.RequestContext)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 FutureCallback (com.linkedin.common.callback.FutureCallback)7 StringMap (com.linkedin.data.template.StringMap)7 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 ByteString (com.linkedin.data.ByteString)5 RestException (com.linkedin.r2.message.rest.RestException)4 MultiplexedResponseContent (com.linkedin.restli.common.multiplexer.MultiplexedResponseContent)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 ExecutionException (java.util.concurrent.ExecutionException)4 IndividualResponse (com.linkedin.restli.common.multiplexer.IndividualResponse)3 IndividualResponseMap (com.linkedin.restli.common.multiplexer.IndividualResponseMap)3 MultiplexedRequestContent (com.linkedin.restli.common.multiplexer.MultiplexedRequestContent)3 HttpCookie (java.net.HttpCookie)3