Ejemplo n.º 1
0
  /** Multi threaded {@link ConcurrentIdBasedLockManager} test. */
  @Test
  public void multithreadedConcurrentLockManagerTest() {
    final IdBasedLockManager<String> lockManager = new ConcurrentIdBasedLockManager<>();
    lockManagerTestExecutor(lockManager, LOCKS_IDS);

    // checking results
    Assert.assertEquals(0, lockManager.getLocksCount());
    Assert.assertNotNull(lockManager.getLocksIds());
    Assert.assertEquals(0, lockManager.getLocksIds().size());

    int processedTotal = 0;
    int unsafeTotal = 0;
    for (final String id : LOCKS_IDS) {
      final int processed = PROCESSED_PER_ID_CALLS.get(id).get();
      processedTotal += processed;
      final int unsafe = UNSAFE_PER_ID_CALLS.get(id).get();
      unsafeTotal += unsafe;

      Assert.assertEquals(1, ACTIVE_MAX_PER_ID_CALLS.get(id).get()); // should be not more then 1
    }

    Assert.assertEquals(THREADS * CALLS_PER_THREAD, processedTotal);
    Assert.assertTrue(THREADS >= ACTIVE_MAX_CALLS.get()); // should be not more then threads amount
    Assert.assertEquals(0, unsafeTotal);
  }
Ejemplo n.º 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());
  }