private LeaseLock lock(long acquireMillis) { try { return JdbcSharedStateManager .createLiveLock( UUID.randomUUID().toString(), jdbcSharedStateManager.getConnection(), sqlProvider, acquireMillis); } catch (SQLException e) { throw new IllegalStateException(e); } }
@Before public void createLockTable() throws Exception { dbConf = createDefaultDatabaseStorageConfiguration(); sqlProvider = JDBCUtils.getSQLProvider( dbConf.getJdbcDriverClassName(), dbConf.getNodeManagerStoreTableName(), SQLProvider.DatabaseStoreType.NODE_MANAGER); if (withExistingTable) { TestJDBCDriver testDriver = TestJDBCDriver .usingConnectionUrl( dbConf.getJdbcConnectionUrl(), dbConf.getJdbcDriverClassName(), sqlProvider); testDriver.start(); testDriver.stop(); } jdbcSharedStateManager = JdbcSharedStateManager .usingConnectionUrl( UUID.randomUUID().toString(), dbConf.getJdbcLockExpirationMillis(), dbConf.getJdbcConnectionUrl(), dbConf.getJdbcDriverClassName(), sqlProvider); }
final Thread[] producerThreads = new Thread[producers]; for (int i = 0; i < producers; i++) { locks[i] = lock(); producerThreads[i] = new Thread(producerTask);
@Test public void shouldRenewAcquiredLock() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(10)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); try { Assert.assertTrue("lock is owned", lock.renew()); } finally { lock.release(); } }
@Test public void shouldOtherAcquireExpiredLock() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(1)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); try { Thread.sleep(lock.expirationMillis() * 2); Assert.assertFalse("lock is already expired", lock.isHeldByCaller()); Assert.assertFalse("lock is already expired", lock.isHeld()); final LeaseLock otherLock = lock(TimeUnit.SECONDS.toMillis(10)); try { Assert.assertTrue("lock is already expired", otherLock.tryAcquire()); } finally { otherLock.release(); } } finally { lock.release(); } }
@Test public void shouldNotRenewLockAcquiredByOthers() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(1)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); try { Thread.sleep(lock.expirationMillis() * 2); Assert.assertFalse("lock is already expired", lock.isHeldByCaller()); Assert.assertFalse("lock is already expired", lock.isHeld()); final LeaseLock otherLock = lock(TimeUnit.SECONDS.toMillis(10)); Assert.assertTrue("lock is already expired", otherLock.tryAcquire()); try { Assert.assertFalse("lock is owned by others", lock.renew()); } finally { otherLock.release(); } } finally { lock.release(); } } }
@Test public void shouldAcquireLock() { final LeaseLock lock = lock(); final boolean acquired = lock.tryAcquire(); Assert.assertTrue("Must acquire the lock!", acquired); try { Assert.assertTrue("The lock is been held by the caller!", lock.isHeldByCaller()); } finally { lock.release(); } }
@Test public void shouldNotAcquireLockTwice() { final LeaseLock lock = lock(); Assert.assertTrue("Must acquire the lock", lock.tryAcquire()); try { Assert.assertFalse("lock already acquired", lock.tryAcquire()); } finally { lock.release(); } }
@Test public void shouldNotAcquireLockWhenAlreadyHeldByOthers() { final LeaseLock lock = lock(); Assert.assertTrue("Must acquire the lock", lock.tryAcquire()); try { Assert.assertTrue("Lock held by the caller", lock.isHeldByCaller()); final LeaseLock failingLock = lock(); Assert.assertFalse("lock already held by other", failingLock.tryAcquire()); Assert.assertFalse("lock already held by other", failingLock.isHeldByCaller()); Assert.assertTrue("lock already held by other", failingLock.isHeld()); } finally { lock.release(); } }
@Test public void shouldAcquireExpiredLock() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(1)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); try { Thread.sleep(lock.expirationMillis() * 2); Assert.assertFalse("lock is already expired", lock.isHeldByCaller()); Assert.assertFalse("lock is already expired", lock.isHeld()); Assert.assertTrue("lock is already expired", lock.tryAcquire()); } finally { lock.release(); } }
@Test public void shouldRenewExpiredLockNotAcquiredByOthers() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(1)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); try { Thread.sleep(lock.expirationMillis() * 2); Assert.assertFalse("lock is already expired", lock.isHeldByCaller()); Assert.assertFalse("lock is already expired", lock.isHeld()); Assert.assertTrue("lock is owned", lock.renew()); } finally { lock.release(); } }
@Test public void shouldNotRenewReleasedLock() throws InterruptedException { final LeaseLock lock = lock(TimeUnit.SECONDS.toMillis(10)); Assert.assertTrue("lock is not owned by anyone", lock.tryAcquire()); lock.release(); Assert.assertFalse("lock is already released", lock.isHeldByCaller()); Assert.assertFalse("lock is already released", lock.isHeld()); Assert.assertFalse("lock is already released", lock.renew()); }