示例#1
0
 @Test(expected = IllegalMonitorStateException.class)
 public void testIllegalConditionUsage2() {
   final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
   final HazelcastInstance instance = nodeFactory.newHazelcastInstance(new Config());
   final ILock lock = instance.getLock("testIllegalConditionUsage");
   final ICondition condition = lock.newCondition("condition");
   condition.signal();
 }
示例#2
0
  @Test(timeout = 100000)
  public void testKeyOwnerDiesOnCondition() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    final Config config = new Config();
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(config);
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    int k = 0;
    final AtomicInteger atomicInteger = new AtomicInteger(0);
    while (keyOwner
        .getCluster()
        .getLocalMember()
        .equals(instance1.getPartitionService().getPartition(k++).getOwner())) {
      Thread.sleep(10);
    }

    final int key = k;
    final ILock lock1 = instance1.getLock(key);
    final String name = "testKeyOwnerDiesOnCondition";
    final ICondition condition1 = lock1.newCondition(name);

    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                final ILock lock = instance2.getLock(key);
                final ICondition condition = lock.newCondition(name);
                lock.lock();
                try {
                  condition.await();
                } catch (InterruptedException e) {
                  e.printStackTrace();
                } finally {
                  lock.unlock();
                }
                atomicInteger.incrementAndGet();
              }
            });
    t.start();
    Thread.sleep(1000);
    lock1.lock();
    keyOwner.getLifecycleService().shutdown();

    condition1.signal();

    lock1.unlock();
    Thread.sleep(1000);
    t.join();
    Assert.assertEquals(1, atomicInteger.get());
  }
示例#3
0
  @Test
  public void testLockConditionSimpleUsage() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();
    final String name = "testLockConditionSimpleUsage";
    final ILock lock = nodeFactory.newHazelcastInstance(config).getLock(name);
    final ICondition condition = lock.newCondition(name + "c");
    final AtomicInteger count = new AtomicInteger(0);

    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  lock.lock();
                  if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                  }
                  condition.await();
                  if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                  }
                } catch (InterruptedException ignored) {
                } finally {
                  lock.unlock();
                }
              }
            });
    t.start();
    Thread.sleep(1000);

    final ILock lock1 = nodeFactory.newHazelcastInstance(config).getLock(name);
    final ICondition condition1 = lock1.newCondition(name + "c");
    Assert.assertEquals(false, lock1.isLocked());
    lock1.lock();
    Assert.assertEquals(true, lock1.isLocked());
    condition1.signal();
    lock1.unlock();
    t.join();
    Assert.assertEquals(2, count.get());
  }