Search in sources :

Example 1 with GridTestSafeThreadFactory

use of org.apache.ignite.testframework.GridTestSafeThreadFactory in project ignite by apache.

the class AbstractCacheJtaSelfTest method testAsyncOpAwait.

/**
 * @throws Exception If failed.
 */
public void testAsyncOpAwait() throws Exception {
    final IgniteCache<String, Integer> cache = jcache();
    GridTestSafeThreadFactory factory = new GridTestSafeThreadFactory("JtaThread");
    final CountDownLatch latch = new CountDownLatch(1);
    Callable<Object> c = new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertNull(grid(0).transactions().tx());
            UserTransaction jtaTx = jotm.getUserTransaction();
            jtaTx.begin();
            try {
                cache.put("key1", 1);
                cache.putAsync("key", 1);
                assertEquals(grid(0).transactions().tx().state(), ACTIVE);
                latch.countDown();
                info("Before JTA commit.");
            } finally {
                jtaTx.commit();
            }
            info("After JTA commit.");
            assertEquals((Integer) 1, cache.get("key"));
            return null;
        }
    };
    Thread task = factory.newThread(c);
    try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
        cache.put("key", 0);
        task.start();
        latch.await();
        while (task.getState() != Thread.State.WAITING) factory.checkError();
        info("Before cache TX commit.");
        tx.commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) UserTransaction(javax.transaction.UserTransaction) Transaction(org.apache.ignite.transactions.Transaction) GridTestSafeThreadFactory(org.apache.ignite.testframework.GridTestSafeThreadFactory) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable)

Aggregations

Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 UserTransaction (javax.transaction.UserTransaction)1 GridTestSafeThreadFactory (org.apache.ignite.testframework.GridTestSafeThreadFactory)1 Transaction (org.apache.ignite.transactions.Transaction)1