Search in sources :

Example 6 with IgniteCondition

use of org.apache.ignite.IgniteCondition in project ignite by apache.

the class IgniteLockAbstractSelfTest method testConditionInterruptAwait.

/**
 * @throws Exception If failed.
 */
private void testConditionInterruptAwait(final boolean fair) throws Exception {
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    final int totalThreads = 2;
    final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertFalse(lock0.isHeldByCurrentThread());
            startedThreads.add(Thread.currentThread());
            boolean isInterrupted = false;
            lock0.lock();
            IgniteCondition cond = lock0.getOrCreateCondition("cond");
            try {
                cond.await();
            } catch (IgniteInterruptedException ignored) {
                isInterrupted = true;
            } finally {
                // Assert that thread was interrupted.
                assertTrue(isInterrupted);
                // Assert that lock is still locked.
                assertTrue(lock0.isLocked());
                // Assert that this thread does own the lock.
                assertTrue(lock0.isHeldByCurrentThread());
                // Release lock.
                if (lock0.isHeldByCurrentThread())
                    lock0.unlock();
            }
            return null;
        }
    }, totalThreads);
    // Wait for all threads to attempt to acquire lock.
    while (startedThreads.size() != totalThreads) {
        Thread.sleep(500);
    }
    for (Thread t : startedThreads) t.interrupt();
    fut.get();
    assertFalse(lock0.isLocked());
    for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
    lock0.close();
}
Also used : GridConcurrentHashSet(org.apache.ignite.internal.util.GridConcurrentHashSet) IgniteLock(org.apache.ignite.IgniteLock) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteCondition(org.apache.ignite.IgniteCondition) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IOException(java.io.IOException)

Aggregations

IgniteCondition (org.apache.ignite.IgniteCondition)6 IgniteLock (org.apache.ignite.IgniteLock)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IOException (java.io.IOException)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)3 GridConcurrentHashSet (org.apache.ignite.internal.util.GridConcurrentHashSet)3 ExpectedException (org.junit.rules.ExpectedException)3 Ignite (org.apache.ignite.Ignite)2 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 IgniteCallable (org.apache.ignite.lang.IgniteCallable)1