/* * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hazelcast.concurrent.lock; import com.hazelcast.config.Config; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstanceNotActiveException; import com.hazelcast.core.ICondition; import com.hazelcast.core.ILock; import com.hazelcast.spi.exception.DistributedObjectDestroyedException; import com.hazelcast.spi.properties.GroupProperty; import com.hazelcast.test.HazelcastParallelClassRunner; import com.hazelcast.test.HazelcastTestSupport; import com.hazelcast.test.TestHazelcastInstanceFactory; import com.hazelcast.test.annotation.ParallelTest; import com.hazelcast.test.annotation.QuickTest; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; @RunWith(HazelcastParallelClassRunner.class) @Category({QuickTest.class, ParallelTest.class}) public class ConditionAdvancedTest extends HazelcastTestSupport { @Test(timeout = 60000) public void testInterruptionDuringWaiting() throws InterruptedException { Config config = new Config(); // the system should wait at most 5000 ms in order to determine the operation status config.setProperty(GroupProperty.OPERATION_CALL_TIMEOUT_MILLIS.getName(), "5000"); HazelcastInstance instance = createHazelcastInstance(config); final ILock lock = instance.getLock(randomString()); final ICondition condition0 = lock.newCondition(randomString()); final CountDownLatch latch = new CountDownLatch(1); Thread thread = new Thread(new Runnable() { @Override public void run() { try { lock.lock(); condition0.await(); } catch (InterruptedException e) { latch.countDown(); } } }); thread.start(); sleepSeconds(2); thread.interrupt(); assertOpenEventually(latch); } // ====================== tests to make sure the condition can deal with cluster member failure ==================== @Test(timeout = 100000) public void testKeyOwnerDiesOnCondition() throws Exception { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3); final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(); final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(); final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(); final AtomicInteger signalCounter = new AtomicInteger(0); final String key = generateKeyOwnedBy(instance1); final ILock lock1 = instance1.getLock(key); final String conditionName = randomString(); final ICondition condition1 = lock1.newCondition(conditionName); Thread t = new Thread(new Runnable() { public void run() { ILock lock = instance2.getLock(key); ICondition condition = lock.newCondition(conditionName); lock.lock(); try { condition.await(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } signalCounter.incrementAndGet(); } }); t.start(); Thread.sleep(1000); lock1.lock(); keyOwner.shutdown(); condition1.signal(); lock1.unlock(); Thread.sleep(1000); t.join(); assertEquals(1, signalCounter.get()); } @Test(timeout = 60000, expected = DistributedObjectDestroyedException.class) public void testDestroyLock_whenOtherWaitingOnConditionAwait() throws InterruptedException { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); final HazelcastInstance instance = nodeFactory.newHazelcastInstance(); final ILock lock = instance.getLock(randomString()); final ICondition condition = lock.newCondition("condition"); final CountDownLatch latch = new CountDownLatch(1); new Thread(new Runnable() { public void run() { try { latch.await(30, TimeUnit.SECONDS); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } lock.destroy(); } }).start(); lock.lock(); latch.countDown(); condition.await(); lock.unlock(); } @Test(timeout = 60000, expected = HazelcastInstanceNotActiveException.class) public void testShutDownNode_whenOtherWaitingOnConditionAwait() throws InterruptedException { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); final HazelcastInstance instance = nodeFactory.newHazelcastInstance(); nodeFactory.newHazelcastInstance(); final String name = randomString(); final ILock lock = instance.getLock(name); final ICondition condition = lock.newCondition("condition"); final CountDownLatch latch = new CountDownLatch(1); new Thread(new Runnable() { public void run() { try { latch.await(1, TimeUnit.MINUTES); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } instance.shutdown(); } }).start(); lock.lock(); try { latch.countDown(); condition.await(); } catch (InterruptedException e) { } lock.unlock(); } @Test public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2); final String name = randomString(); final HazelcastInstance instance = nodeFactory.newHazelcastInstance(); final AtomicInteger count = new AtomicInteger(0); final int size = 50; final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(); warmUpPartitions(instance, keyOwner); final String key = generateKeyOwnedBy(keyOwner); final ILock lock = instance.getLock(key); final ICondition condition = lock.newCondition(name); final CountDownLatch awaitLatch = new CountDownLatch(size); final CountDownLatch finalLatch = new CountDownLatch(size); for (int i = 0; i < size; i++) { new Thread(new Runnable() { public void run() { lock.lock(); try { awaitLatch.countDown(); condition.await(); if (lock.isLockedByCurrentThread()) { count.incrementAndGet(); } } catch (InterruptedException ignored) { } finally { lock.unlock(); finalLatch.countDown(); } } }).start(); } ILock lock1 = keyOwner.getLock(key); ICondition condition1 = lock1.newCondition(name); awaitLatch.await(1, TimeUnit.MINUTES); lock1.lock(); condition1.signalAll(); lock1.unlock(); keyOwner.shutdown(); finalLatch.await(2, TimeUnit.MINUTES); assertEquals(size, count.get()); } }