@Test public void testRefCount() throws InterruptedException, ExecutionException { assertEquals(cut.getRefCount(), 0); cut.registerReference(); assertEquals(cut.getRefCount(), 1); assertEquals(cut.getRefCount(), 1); cut.releaseReference(); assertEquals(cut.getRefCount(), 0); // other threads must each increment the ref count final Runnable registerReference = new Runnable() { @Override public void run() { cut.registerReference(); } }; executor.submit(registerReference).get(); assertEquals(cut.getRefCount(), 1); alternateExecutor.submit(registerReference).get(); assertEquals(cut.getRefCount(), 2); // we (no ref registered) must not be able to decrement the ref count cut.releaseReference(); assertEquals(cut.getRefCount(), 2); } }
@Override public BackupResult call() throws Exception { final MemcachedBackupSession session3 = _service.findSession(session.getId()); assertSame(session3, session2); assertEquals(session3.getRefCount(), 2); // let the other thread proceed (or wait) barrier.await(); // and wait again so that the other thread can do some work barrier.await(); final Future<BackupResult> result = _service.backupSession(session.getId(), false, "unused"); _service.getTrackingHostValve().resetRequestThreadLocal(); assertEquals(result.get().getStatus(), BackupResultStatus.SUCCESS); // The session should be released now and no longer stored assertFalse(_service.getManager().getSessionsInternal().containsKey(session.getId())); // just some double checking on expectations... assertEquals(session2.getRefCount(), 0); return result.get(); }
assertEquals(session2.getRefCount(), 1); session2.setAttribute("foo", "bar"); assertEquals(session2.getRefCount(), 1);