예제 #1
0
  @Test(timeout = 1000l)
  public void testMultipleClientsCannotAccessSameLock() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final Lock firstLock = new ReentrantZkLock(baseLockPath, zkSessionManager);
    final Lock sameLock =
        new ReentrantZkLock(baseLockPath, new BaseZkSessionManager(newZooKeeper()));

    firstLock.lock();
    testService.submit(
        new Runnable() {
          @Override
          public void run() {
            sameLock.lock();
            try {
              latch.countDown();
            } finally {
              sameLock.unlock();
            }
          }
        });

    boolean acquired = latch.await(500, TimeUnit.MILLISECONDS);
    assertTrue("The Lock was acquired twice by two separate threads!", !acquired);

    firstLock.unlock();

    acquired = latch.await(500, TimeUnit.MILLISECONDS);
    assertTrue("The Lock was never acquired by another thread!", acquired);
  }
예제 #2
0
  @Test(timeout = 1500l)
  public void testReentrancy() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Lock firstLock = new ReentrantZkLock(baseLockPath, zkSessionManager);
    firstLock.lock();
    try {
      testService.submit(
          new Runnable() {
            @Override
            public void run() {
              Lock secondLock = new ReentrantZkLock(baseLockPath, zkSessionManager);
              secondLock.lock();
              try {
                latch.countDown();
              } finally {
                secondLock.unlock();
              }
            }
          });

      boolean nowAcquired = latch.await(500, TimeUnit.MILLISECONDS);
      assertTrue("The Second lock was acquired before the first lock was released!", !nowAcquired);

      // this should be fine
      firstLock.lock();
      System.out.println("Lock acquired twice!");
      firstLock.unlock();
      // should still be locked
      nowAcquired = latch.await(500, TimeUnit.MILLISECONDS);
      assertTrue(
          "The Second lock was acquired before the first lock was released twice!", !nowAcquired);
    } finally {
      firstLock.unlock();
    }
  }
예제 #3
0
  @Test(timeout = 1500l)
  public void testOnlyOneLockAllowedTwoThreads() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Lock firstLock = new ReentrantZkLock(baseLockPath, zkSessionManager);
    firstLock.lock();
    try {
      testService.submit(
          new Runnable() {
            @Override
            public void run() {
              Lock secondLock = new ReentrantZkLock(baseLockPath, zkSessionManager);
              secondLock.lock();
              try {
                latch.countDown();
              } finally {
                secondLock.unlock();
              }
            }
          });

      boolean nowAcquired = latch.await(500, TimeUnit.MILLISECONDS);
      assertTrue("The Second lock was acquired before the first lock was released!", !nowAcquired);
    } finally {
      firstLock.unlock();
    }
  }