Search in sources :

Example 1 with ExecutionGroup

use of com.linkedin.restli.client.ExecutionGroup in project rest.li by linkedin.

the class TestParSeqBasedFluentClientApiWithExecutionGroup method testBatchGet.

/**
 *   This test is to test if the batch method has been triggered.
 *
 *   Note: Currently ParSeqRestClient only support "batch" for gets.
 */
@Test
public void testBatchGet() throws Exception {
    // Test 3+1 = 4 calls using the "runBatchOnClient" method in the fluent Client
    GreetingsFluentClient greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    final List<CompletionStage<Greeting>> results = new ArrayList<>(TEST_BATCH_SIZE + 1);
    greetings.runBatchOnClient(() -> {
        try {
            for (int i = 0; i < OVER_BATCH_SIZE_CALLS; i++) {
                results.add(greetings.get((long) (i + 1)));
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    });
    for (CompletionStage<Greeting> stage : results) {
        stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    }
    assert (results.get(0) instanceof ParSeqBasedCompletionStage);
    Task<Greeting> t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
    Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
    Assert.assertEquals(findTaskOccurrenceInTrace("greetings batch_get", t.getTrace()), OVER_BATCH_SIZE_CALLS / TEST_BATCH_SIZE);
    Assert.assertEquals(findTaskOccurrenceInTrace("greetings get", t.getTrace()), OVER_BATCH_SIZE_CALLS + 1);
    // Test 3+1 = 4 calls generating a new ExecutionGroup and explicitly call the methods with ExecutionGroup
    results.clear();
    ExecutionGroup eg = greetings.generateExecutionGroup();
    for (int i = 0; i < OVER_BATCH_SIZE_CALLS; i++) {
        results.add(greetings.get((long) (i + 1), eg));
    }
    assert (results.get(0) instanceof ParSeqBasedCompletionStage);
    t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
    Assert.assertFalse(t.isDone());
    Assert.assertFalse(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
    eg.execute();
    for (CompletionStage<Greeting> stage : results) {
        stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    }
    Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE, TEST_BATCH_SIZE), t.getTrace()));
    Assert.assertEquals(findTaskOccurrenceInTrace("greetings batch_get", t.getTrace()), OVER_BATCH_SIZE_CALLS / TEST_BATCH_SIZE);
    Assert.assertEquals(findTaskOccurrenceInTrace("greetings get", t.getTrace()), OVER_BATCH_SIZE_CALLS + 1);
    // Test 2( less than 3 ) calls using client's batchOn
    results.clear();
    greetings.runBatchOnClient(() -> {
        try {
            for (int i = 0; i < UNDER_BATCH_SIZE_CALLS; i++) {
                results.add(greetings.get((long) (i + 1)));
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    });
    for (CompletionStage<Greeting> stage : results) {
        stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    }
    assert (results.get(0) instanceof ParSeqBasedCompletionStage);
    t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
    Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
    // Test 2( less than 3 ) calls execution group
    results.clear();
    eg = greetings.generateExecutionGroup();
    for (int i = 0; i < UNDER_BATCH_SIZE_CALLS; i++) {
        results.add(greetings.get((long) (i + 1), eg));
    }
    assert (results.get(0) instanceof ParSeqBasedCompletionStage);
    t = ((ParSeqBasedCompletionStage<Greeting>) results.get(0)).getTask();
    Assert.assertFalse(t.isDone());
    Assert.assertFalse(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
    eg.execute();
    for (CompletionStage<Greeting> stage : results) {
        stage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    }
    Assert.assertTrue(hasTask(String.format("greetings batch_get(reqs: %s, ids: %s)", TEST_BATCH_SIZE - 1, TEST_BATCH_SIZE - 1), t.getTrace()));
    // Testing read the completion Stage within the runnable: Not allowed; should fail
    results.clear();
    try {
        greetings.runBatchOnClient(() -> {
            try {
                results.add(greetings.get(1L));
                results.get(0).toCompletableFuture().get(500, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        Assert.fail("Should fail: the CompletionStage cannot be read ");
    } catch (Exception e) {
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ArrayList(java.util.ArrayList) ExecutionGroup(com.linkedin.restli.client.ExecutionGroup) TimeoutException(java.util.concurrent.TimeoutException) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) ParSeqBasedCompletionStage(com.linkedin.restli.client.ParSeqBasedCompletionStage) CompletionStage(java.util.concurrent.CompletionStage) ParSeqBasedCompletionStage(com.linkedin.restli.client.ParSeqBasedCompletionStage) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Aggregations

ExecutionGroup (com.linkedin.restli.client.ExecutionGroup)1 ParSeqBasedCompletionStage (com.linkedin.restli.client.ParSeqBasedCompletionStage)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 GreetingsFluentClient (com.linkedin.restli.examples.greetings.client.GreetingsFluentClient)1 ArrayList (java.util.ArrayList)1 CompletionStage (java.util.concurrent.CompletionStage)1 TimeoutException (java.util.concurrent.TimeoutException)1 Test (org.testng.annotations.Test)1