Esempio n. 1
0
  private static void testSendGetSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds)
      throws ServiceUnavailableException, InterruptedException {
    Collection<ScatterGatherBuilder.RequestInfo<Greeting>> scatterGatherRequests =
        buildScatterGatherGetRequests(sg, requestIds);

    final Map<String, Greeting> results = new ConcurrentHashMap<String, Greeting>();
    final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
    final List<Throwable> errors = new ArrayList<Throwable>();

    final List<BatchResponse<Greeting>> responses = new ArrayList<BatchResponse<Greeting>>();
    for (ScatterGatherBuilder.RequestInfo<Greeting> requestInfo : scatterGatherRequests) {
      Callback<Response<BatchResponse<Greeting>>> cb =
          new Callback<Response<BatchResponse<Greeting>>>() {
            @Override
            public void onSuccess(Response<BatchResponse<Greeting>> response) {
              results.putAll(response.getEntity().getResults());
              synchronized (responses) {
                responses.add(response.getEntity());
              }
              latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
              synchronized (errors) {
                errors.add(e);
              }
              latch.countDown();
            }
          };

      REST_CLIENT.sendRequest(requestInfo.getRequest(), requestInfo.getRequestContext(), cb);
    }
    latch.await();

    if (!errors.isEmpty()) {
      Assert.fail("Errors in scatter/gather: " + errors.toString());
    }

    Assert.assertEquals(results.values().size(), requestIds.length);

    Set<Set<String>> responseIdSets = new HashSet<Set<String>>();
    Set<Long> responseIds = new HashSet<Long>();
    for (BatchResponse<Greeting> response : responses) {
      Set<String> theseIds = response.getResults().keySet();
      Assert.assertFalse(responseIdSets.contains(theseIds)); // no duplicate requests
      for (String id : theseIds) {
        Assert.assertFalse(responseIds.contains(Long.parseLong(id))); // no duplicate ids
        responseIds.add(Long.parseLong(id));
      }
      responseIdSets.add(theseIds);
    }
    Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
    Assert.assertEquals(responseIds.size(), requestIds.length);
  }
  @Test(
      dataProvider =
          com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX
              + "clientDataBatchDataProvider")
  public void testBatchGet(RestClient restClient, RestliRequestOptions requestOptions)
      throws RemoteInvocationException {
    List<Long> ids = Arrays.asList(1L, 2L, 3L, 4L);
    Request<BatchResponse<Greeting>> request =
        new GreetingsBuilders(requestOptions).batchGet().ids(ids).build();

    Response<BatchResponse<Greeting>> response = restClient.sendRequest(request).getResponse();
    BatchResponse<Greeting> batchResponse = response.getEntity();
    Assert.assertEquals(batchResponse.getResults().size(), ids.size());
  }