Search in sources :

Example 1 with SuccessCallback

use of com.datastax.driver.core.utils.MoreFutures.SuccessCallback in project java-driver by datastax.

the class MapperAsyncTest method should_get_query_async_without_blocking.

/**
 * Validates that when using save, get and deleteAsync using the same executor as the caller that the driver
 * does not block indefinitely in a netty worker thread.
 *
 * @jira_ticket JAVA-1070
 * @test_category queries:async
 * @test_category object_mapper
 */
@Test(groups = "short")
public void should_get_query_async_without_blocking() {
    final CountDownLatch latch = new CountDownLatch(1);
    // create a second cluster to perform everything asynchronously,
    // including session initialization
    Cluster cluster2 = register(createClusterBuilder().addContactPoints(getContactPoints()).withPort(ccm().getBinaryPort()).build());
    ListenableFuture<MappingManager> mappingManagerFuture = Futures.transform(cluster2.connectAsync(), new Function<Session, MappingManager>() {

        @Override
        public MappingManager apply(Session session) {
            return new MappingManager(session);
        }
    });
    Futures.addCallback(mappingManagerFuture, new SuccessCallback<MappingManager>() {

        @Override
        public void onSuccess(MappingManager manager) {
            final Mapper<User> mapper = manager.mapper(User.class);
            ListenableFuture<Void> saveFuture = mapper.saveAsync(paul);
            Futures.addCallback(saveFuture, new SuccessCallback<Void>() {

                @Override
                public void onSuccess(Void result) {
                    ListenableFuture<User> getFuture = mapper.getAsync(paul.getUserId());
                    Futures.addCallback(getFuture, new SuccessCallback<User>() {

                        @Override
                        public void onSuccess(User paul) {
                            Futures.addCallback(mapper.deleteAsync(paul), new SuccessCallback<Void>() {

                                @Override
                                public void onSuccess(Void result) {
                                    latch.countDown();
                                }
                            });
                        }
                    });
                }
            });
        }
    });
    try {
        Uninterruptibles.awaitUninterruptibly(latch, 5, MINUTES);
    } catch (Exception e) {
        fail("Operation did not complete normally within 5 minutes");
    }
}
Also used : SuccessCallback(com.datastax.driver.core.utils.MoreFutures.SuccessCallback) Cluster(com.datastax.driver.core.Cluster) CountDownLatch(java.util.concurrent.CountDownLatch) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Session(com.datastax.driver.core.Session) Test(org.testng.annotations.Test)

Aggregations

Cluster (com.datastax.driver.core.Cluster)1 Session (com.datastax.driver.core.Session)1 SuccessCallback (com.datastax.driver.core.utils.MoreFutures.SuccessCallback)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.testng.annotations.Test)1