예제 #1
0
    @Override
    public void run() {
      try {
        starter.await();
      } catch (final InterruptedException e) {
        Assert.fail();
      }

      for (int i = 0; i < CALLS_PER_THREAD; i++) {
        final String id = getRandom(ids);
        final IdBasedLock<String> lock = lockManager.obtainLock(id);
        lock.lock();

        int activeCount = ACTIVE_CALLS.incrementAndGet();
        if (activeCount > ACTIVE_MAX_CALLS.get()) ACTIVE_MAX_CALLS.set(activeCount);

        int activePerIdCount = ACTIVE_PER_ID_CALLS.get(id).incrementAndGet();
        if (activePerIdCount > ACTIVE_MAX_PER_ID_CALLS.get(id).get())
          ACTIVE_MAX_PER_ID_CALLS.get(id).set(activePerIdCount);

        if (activePerIdCount > 1) UNSAFE_PER_ID_CALLS.get(id).incrementAndGet();

        ACTIVE_PER_ID_CALLS.get(id).decrementAndGet();

        ACTIVE_CALLS.decrementAndGet();

        lock.unlock();

        PROCESSED_PER_ID_CALLS.get(id).incrementAndGet();
      }

      finisher.countDown();
    }
예제 #2
0
  /** Basic {@link ConcurrentIdBasedLockManager} test. */
  @Test
  public void basicConcurrentLockManagerTest() {
    IdBasedLockManager<String> lockManager = new ConcurrentIdBasedLockManager<>();
    Assert.assertNotNull(lockManager.getLocksIds());
    Assert.assertEquals(0, lockManager.getLocksIds().size());

    IdBasedLock<String> lock = lockManager.obtainLock(LOCK_ID);
    Assert.assertNotNull(lock);
    Assert.assertNotNull(lockManager.getLocksIds());
    Assert.assertEquals(1, lockManager.getLocksIds().size());
    Assert.assertNotNull(lockManager.getLocksIds().get(0));
    Assert.assertEquals(LOCK_ID, lockManager.getLocksIds().get(0));

    lock.lock();
    try {
      Assert.assertEquals(LOCK_ID, lock.getId());
      Assert.assertEquals(1, lock.getReferencesCount());
      Assert.assertNotNull(lock.toString());
    } finally {
      lock.unlock();
    }

    Assert.assertNotNull(lockManager.getLocksIds());
    Assert.assertEquals(0, lockManager.getLocksIds().size());
  }