use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestAllPartitionsRequestBuilder method testSendAllPartitionsRequests.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "restliRequestOptions")
public void testSendAllPartitionsRequests(RestliRequestOptions options, RingFactory<URI> ringFactory) throws ServiceUnavailableException, URISyntaxException, RestException, InterruptedException {
final int PARTITION_NUM = 5;
List<URI> expectedUris = new ArrayList<URI>();
ConsistentHashKeyMapper mapper = getKeyToHostMapper(PARTITION_NUM, expectedUris, ringFactory);
AllPartitionsRequestBuilder<Greeting> searchRB = new AllPartitionsRequestBuilder<Greeting>(mapper);
ActionRequestBuilder<Long, Greeting> builder = new ActionRequestBuilder<Long, Greeting>(TEST_URI, Greeting.class, _COLL_SPEC, options);
ActionRequest<Greeting> request = builder.name("updateTone").id(1L).setParam(new FieldDef<Tone>("newTone", Tone.class, DataTemplateUtil.getSchema(Tone.class)), Tone.FRIENDLY).build();
final Map<String, Greeting> results = new ConcurrentHashMap<String, Greeting>();
final CountDownLatch latch = new CountDownLatch(PARTITION_NUM);
final List<Throwable> errors = new ArrayList<Throwable>();
final List<Greeting> responses = new ArrayList<Greeting>();
Callback<Response<Greeting>> cb = new Callback<Response<Greeting>>() {
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
@Override
public void onSuccess(Response<Greeting> response) {
results.put(response.getEntity().toString(), response.getEntity());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
};
HostSet hostsResult = searchRB.sendRequests(getClient(), request, new RequestContext(), cb);
List<URI> uris = hostsResult.getAllHosts();
Assert.assertTrue(uris.containsAll(expectedUris));
Assert.assertTrue(expectedUris.containsAll(uris));
latch.await();
if (!errors.isEmpty()) {
Assert.fail("I knew it: " + errors.toString());
}
Assert.assertEquals(PARTITION_NUM, responses.size());
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestScatterGather method testSendSGKVRequests.
private static void testSendSGKVRequests(Collection<ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus>> scatterGatherRequests, Long[] requestIds) throws InterruptedException {
final Map<Long, UpdateStatus> results = new ConcurrentHashMap<Long, UpdateStatus>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<Throwable>();
final List<BatchKVResponse<Long, UpdateStatus>> responses = new ArrayList<BatchKVResponse<Long, UpdateStatus>>();
for (ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus> requestInfo : scatterGatherRequests) {
Callback<Response<BatchKVResponse<Long, UpdateStatus>>> cb = new Callback<Response<BatchKVResponse<Long, UpdateStatus>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<Long, UpdateStatus>> 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();
}
};
BatchRequest<BatchKVResponse<Long, UpdateStatus>> request = requestInfo.getRequest();
RequestContext requestContext = requestInfo.getRequestContext();
REST_CLIENT.sendRequest(request, requestContext, cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
Set<Long> responseIds = new HashSet<Long>();
for (BatchKVResponse<Long, UpdateStatus> response : responses) {
Set<Long> theseIds = response.getResults().keySet();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (Long id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(id));
responseIds.add(id);
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestScatterGather method testSendGetSGRequests.
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();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (String id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(Long.parseLong(id)));
responseIds.add(Long.parseLong(id));
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAsyncGetAllComplexKeyResource.
@Test
public void testAsyncGetAllComplexKeyResource() throws Exception {
ResourceModel discoveredResourceModel = buildResourceModel(AsyncDiscoveredItemsResource.class);
RestLiCallback<?> callback = getCallback();
ResourceMethodDescriptor methodDescriptor;
AsyncDiscoveredItemsResource discoveredResource;
methodDescriptor = discoveredResourceModel.findMethod(ResourceMethod.GET_ALL);
discoveredResource = getMockResource(AsyncDiscoveredItemsResource.class);
@SuppressWarnings("unchecked") PagingContext mockCtx = (PagingContext) EasyMock.anyObject();
discoveredResource.getAll(mockCtx, EasyMock.<Callback<List<DiscoveredItem>>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<List<DiscoveredItem>> callback = (Callback<List<DiscoveredItem>>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(discoveredResource);
checkAsyncInvocation(discoveredResource, callback, methodDescriptor, "GET", version, "/asyncdiscovereditems", buildBatchPathKeys());
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAsyncGetAssociativeResource.
@Test
public void testAsyncGetAssociativeResource() throws Exception {
ResourceModel followsResourceModel = buildResourceModel(AsyncFollowsAssociativeResource.class);
RestLiCallback<?> callback = getCallback();
ResourceMethodDescriptor methodDescriptor;
AsyncFollowsAssociativeResource resource;
// #1: get
methodDescriptor = followsResourceModel.findMethod(ResourceMethod.GET);
resource = getMockResource(AsyncFollowsAssociativeResource.class);
CompoundKey rawKey = new CompoundKey();
rawKey.append("followerID", 1L);
rawKey.append("followeeID", 2L);
CompoundKey key = eq(rawKey);
resource.get(key, EasyMock.<Callback<Followed>>anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<Followed> callback = (Callback<Followed>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(resource);
checkAsyncInvocation(resource, callback, methodDescriptor, "GET", version, "/asyncfollows/(followerID:1,followeeID:2)", buildPathKeys("followerID", 1L, "followeeID", 2L, followsResourceModel.getKeyName(), rawKey));
}
Aggregations