Пример #1
0
  @Test(expected = HazelcastInstanceNotActiveException.class)
  public void testShutDownNodeWhenOtherWaitingOnConditionAwait() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(new Config());
    nodeFactory.newHazelcastInstance(new Config());
    final String name = "testShutDownNodeWhenOtherWaitingOnConditionAwait";
    final ILock lock = instance.getLock(name);
    final ICondition condition = lock.newCondition("s");
    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.getLifecycleService().shutdown();
              }
            })
        .start();

    lock.lock();
    try {
      latch.countDown();
      condition.await();
    } catch (InterruptedException e) {
    }
    lock.unlock();
  }
Пример #2
0
  @Test(expected = DistributedObjectDestroyedException.class)
  public void testDestroyLockWhenOtherWaitingOnConditionAwait() {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(new Config());
    final ILock lock = instance.getLock("testDestroyLockWhenOtherWaitingOnConditionAwait");
    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();
    try {
      latch.countDown();
      condition.await();
    } catch (InterruptedException e) {
    }
    lock.unlock();
  }
Пример #3
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();
 }
Пример #4
0
  @Test
  public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();
    final String name = "testLockConditionSignalAllShutDownKeyOwner";
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    final AtomicInteger count = new AtomicInteger(0);
    final int size = 50;
    int k = 0;
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(config);
    while (!keyOwner
        .getCluster()
        .getLocalMember()
        .equals(instance.getPartitionService().getPartition(++k).getOwner())) {
      Thread.sleep(10);
    }

    final ILock lock = instance.getLock(k);
    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();
                    Thread.sleep(5);
                    if (lock.isLockedByCurrentThread()) {
                      count.incrementAndGet();
                    }
                  } catch (InterruptedException ignored) {
                  } finally {
                    lock.unlock();
                    finalLatch.countDown();
                  }
                }
              })
          .start();
    }

    final ILock lock1 = keyOwner.getLock(k);
    final ICondition condition1 = lock1.newCondition(name);
    awaitLatch.await(1, TimeUnit.MINUTES);
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    keyOwner.getLifecycleService().shutdown();

    finalLatch.await(2, TimeUnit.MINUTES);
    Assert.assertEquals(size, count.get());
  }
Пример #5
0
 @Test(expected = IllegalMonitorStateException.class)
 public void testIllegalConditionUsage1() {
   final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
   final HazelcastInstance instance = nodeFactory.newHazelcastInstance(new Config());
   final ILock lock = instance.getLock("testIllegalConditionUsage");
   final ICondition condition = lock.newCondition("condition");
   try {
     condition.await();
   } catch (InterruptedException e) {
   }
 }
Пример #6
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());
  }
Пример #7
0
  @Test
  public void testLockConditionSignalAll() 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);
    final int k = 50;

    final CountDownLatch awaitLatch = new CountDownLatch(k);
    final CountDownLatch finalLatch = new CountDownLatch(k);
    for (int i = 0; i < k; i++) {
      new Thread(
              new Runnable() {
                public void run() {
                  try {
                    lock.lock();
                    if (lock.isLockedByCurrentThread()) {
                      count.incrementAndGet();
                    }
                    awaitLatch.countDown();
                    condition.await();
                    if (lock.isLockedByCurrentThread()) {
                      count.incrementAndGet();
                    }
                  } catch (InterruptedException ignored) {
                  } finally {
                    lock.unlock();
                    finalLatch.countDown();
                  }
                }
              })
          .start();
    }

    awaitLatch.await(1, TimeUnit.MINUTES);
    final ILock lock1 = nodeFactory.newHazelcastInstance(config).getLock(name);
    final ICondition condition1 = lock1.newCondition(name + "c");
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    finalLatch.await(1, TimeUnit.MINUTES);
    Assert.assertEquals(k * 2, count.get());
  }
Пример #8
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());
  }