LockService extends AbstractLockService<Token, TokenReadWriteLock> { public ReadLock getReadLock(Object... objects) { return getReadLock(Token.wrap(objects)); } private  LockService(); private  LockService(ConcurrentHashMap<Token, TokenReadWriteLock> locks); static LockService create(); static LockService noOp(); ReadLock getReadLock(Object... objects); WriteLock getWriteLock(Object... objects);  }
@Test public void testLockServiceDoesNotEvictLocksThatAreBeingUsedWithHighConcurrencyAndDifferentActions() throws InterruptedException { int clients = TestData.getScaleCount(); final AtomicBoolean done = new AtomicBoolean(false); final AtomicBoolean failed = new AtomicBoolean(false); final Set<String> keys = Sets.newHashSet(); while (keys.size() < clients) { keys.add(TestData.getString()); } final Set<Long> records = Sets.newHashSet(); while (records.size() < clients) { records.add(TestData.getLong()); } Runnable r = new Runnable() { @Override public void run() { while (!done.get()) { try { String key = TCollections.getRandomElement(keys); long record = TCollections.getRandomElement(records); ReadLock readLock = lockService.getReadLock(key, record); readLock.lock(); readLock.unlock(); } catch (IllegalMonitorStateException e) { e.printStackTrace(); done.set(true); failed.set(true); } } } }; for (int i = 0; i < clients; i++) { new Thread(r).start(); } Thread.sleep(TestData.getScaleCount() * 10); done.set(true); Assert.assertFalse(failed.get()); }
@Test public void testLockServiceDoesNotEvictLocksThatAreBeingUsedWithHighConcurrency() throws InterruptedException { int clients = TestData.getScaleCount(); final AtomicBoolean done = new AtomicBoolean(false); final AtomicBoolean failed = new AtomicBoolean(false); Runnable r = new Runnable() { @Override public void run() { while (!done.get()) { try { ReadLock readLock = lockService.getReadLock("bar", 1); readLock.lock(); readLock.unlock(); } catch (IllegalMonitorStateException e) { done.set(true); failed.set(true); e.printStackTrace(); } } } }; for (int i = 0; i < clients; i++) { new Thread(r).start(); } Thread.sleep(TestData.getScaleCount() * 10); done.set(true); Assert.assertFalse(failed.get()); }
@Test public void testLockServiceDoesNotEvictLocksThatAreBeingUsedEvenWithSomeDelay() { final AtomicBoolean wait0 = new AtomicBoolean(true); final AtomicBoolean wait1 = new AtomicBoolean(true); final AtomicBoolean done = new AtomicBoolean(false); final AtomicBoolean failed = new AtomicBoolean(false); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { ReadLock readLock = lockService.getReadLock("foo", 1); readLock.lock(); wait0.set(false); while (wait1.get()) { continue; } Thread.sleep(TestData.getScaleCount() * 4); readLock.unlock(); } catch (IllegalMonitorStateException e) { failed.set(true); e.printStackTrace(); } catch (InterruptedException e) { throw CheckedExceptions.wrapAsRuntimeException(e); } finally { done.set(true); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { while (wait0.get()) { continue; } ReadLock readLock = lockService.getReadLock("foo", 1); readLock.lock(); readLock.unlock(); wait1.set(false); } }); t1.start(); t2.start(); while (!done.get()) { continue; } Assert.assertFalse(failed.get()); }