Search in sources :

Example 61 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class ConditionAdvancedTest method testDestroyLock_whenOtherWaitingOnConditionAwait.

@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();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 62 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class LockAdvancedTest method testIsLocked2.

//todo:   what does isLocked2 test?
@Test(timeout = 60000)
public void testIsLocked2() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
    final String key = randomString();
    final ILock lock = instance1.getLock(key);
    lock.lock();
    assertTrue(lock.isLocked());
    assertTrue(lock.isLockedByCurrentThread());
    assertTrue(lock.tryLock());
    assertTrue(lock.isLocked());
    assertTrue(lock.isLockedByCurrentThread());
    final AtomicBoolean result = new AtomicBoolean();
    final Thread thread = new Thread() {

        public void run() {
            result.set(lock.isLockedByCurrentThread());
        }
    };
    thread.start();
    thread.join();
    assertFalse(result.get());
    lock.unlock();
    assertTrue(lock.isLocked());
    assertTrue(lock.isLockedByCurrentThread());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 63 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class LockAdvancedTest method testShutDownNodeWhenOtherWaitingOnLock.

private void testShutDownNodeWhenOtherWaitingOnLock(boolean localKey) throws InterruptedException {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance();
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();
    warmUpPartitions(instance2, instance);
    final String key;
    if (localKey) {
        key = generateKeyOwnedBy(instance);
    } else {
        key = generateKeyNotOwnedBy(instance);
    }
    final ILock lock = instance.getLock(key);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            lock.lock();
        }
    });
    thread.start();
    thread.join();
    new Thread(new Runnable() {

        public void run() {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            instance.shutdown();
        }
    }).start();
    lock.lock();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory)

Example 64 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class LockAdvancedTest method testScheduledLockActionForDeadMember.

@Test(timeout = 100000)
public void testScheduledLockActionForDeadMember() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
    final ILock lock1 = h1.getLock("default");
    final HazelcastInstance h2 = nodeFactory.newHazelcastInstance();
    final ILock lock2 = h2.getLock("default");
    assertTrue(lock1.tryLock());
    final AtomicBoolean error = new AtomicBoolean(false);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {
                lock2.lock();
                error.set(true);
            } catch (Throwable ignored) {
            }
        }
    });
    thread.start();
    Thread.sleep(5000);
    assertTrue(lock1.isLocked());
    h2.shutdown();
    thread.join(10000);
    assertFalse(thread.isAlive());
    assertFalse(error.get());
    assertTrue(lock1.isLocked());
    lock1.unlock();
    assertFalse(lock1.isLocked());
    assertTrue(lock1.tryLock());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ILock(com.hazelcast.core.ILock) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 65 with TestHazelcastInstanceFactory

use of com.hazelcast.test.TestHazelcastInstanceFactory in project hazelcast by hazelcast.

the class LockAdvancedTest method testLockFail_whenGreaterThanMaxLeaseTimeUsed.

@Test(expected = IllegalArgumentException.class)
public void testLockFail_whenGreaterThanMaxLeaseTimeUsed() {
    Config config = new Config();
    config.setProperty(GroupProperty.LOCK_MAX_LEASE_TIME_SECONDS.getName(), "1");
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
    HazelcastInstance hz = factory.newHazelcastInstance(config);
    ILock lock = hz.getLock(randomName());
    lock.lock(10, TimeUnit.SECONDS);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ILock(com.hazelcast.core.ILock) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)948 HazelcastInstance (com.hazelcast.core.HazelcastInstance)827 Test (org.junit.Test)774 QuickTest (com.hazelcast.test.annotation.QuickTest)726 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)575 Config (com.hazelcast.config.Config)488 MapConfig (com.hazelcast.config.MapConfig)185 ParallelTest (com.hazelcast.test.annotation.ParallelTest)145 MapStoreConfig (com.hazelcast.config.MapStoreConfig)121 CountDownLatch (java.util.concurrent.CountDownLatch)108 AssertTask (com.hazelcast.test.AssertTask)97 NightlyTest (com.hazelcast.test.annotation.NightlyTest)86 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)81 Before (org.junit.Before)75 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)58 NearCacheConfig (com.hazelcast.config.NearCacheConfig)53 SlowTest (com.hazelcast.test.annotation.SlowTest)53 IndexConfig (com.hazelcast.config.IndexConfig)50 ArrayList (java.util.ArrayList)45 Map (java.util.Map)43