@Test public void testRegisterWithoutThread() throws InterruptedException { System.setProperty(_THREAD_ENABLED_KEY, Boolean.FALSE.toString()); Object testObject = new Object(); MarkFinalizeAction markFinalizeAction = new MarkFinalizeAction(); FinalizeManager.register(testObject, markFinalizeAction); Assert.assertFalse(markFinalizeAction.isMarked()); testObject = null; gc(); FinalizeManager.register(new Object(), markFinalizeAction); Assert.assertTrue(markFinalizeAction.isMarked()); }
public static Session createSession(String workspaceName) throws RepositoryException { if (workspaceName == null) { workspaceName = JCRFactory.WORKSPACE_NAME; } if (!PropsValues.JCR_WRAP_SESSION) { JCRFactory jcrFactory = getJCRFactory(); return jcrFactory.createSession(workspaceName); } Map<String, Session> sessions = _sessions.get(); Session session = sessions.get(workspaceName); if (session != null) { return session; } JCRFactory jcrFactory = getJCRFactory(); Session jcrSession = jcrFactory.createSession(workspaceName); JCRSessionInvocationHandler jcrSessionInvocationHandler = new JCRSessionInvocationHandler(jcrSession); Object sessionProxy = ProxyUtil.newProxyInstance( ClassLoaderUtil.getPortalClassLoader(), new Class<?>[] {Map.class, Session.class}, jcrSessionInvocationHandler); FinalizeManager.register( sessionProxy, jcrSessionInvocationHandler, FinalizeManager.PHANTOM_REFERENCE_FACTORY); session = (Session) sessionProxy; sessions.put(workspaceName, session); return session; }
@Test public void testRegisterWithThread() throws InterruptedException { System.setProperty(_THREAD_ENABLED_KEY, Boolean.TRUE.toString()); Object testObject = new Object(); MarkFinalizeAction markFinalizeAction = new MarkFinalizeAction(); FinalizeManager.register(testObject, markFinalizeAction); Assert.assertFalse(markFinalizeAction.isMarked()); testObject = null; gc(); long startTime = System.currentTimeMillis(); while (!markFinalizeAction.isMarked() && ((System.currentTimeMillis() - startTime) < 10000)) { Thread.sleep(1); } Assert.assertTrue(markFinalizeAction.isMarked()); Thread finalizeThread = null; for (Thread thread : ThreadUtil.getThreads()) { String name = thread.getName(); if (name.equals("Finalize Thread")) { finalizeThread = thread; break; } } Assert.assertNotNull(finalizeThread); // First waiting state startTime = System.currentTimeMillis(); while (finalizeThread.getState() != Thread.State.WAITING) { if ((System.currentTimeMillis() - startTime) > 10000) { Assert.fail("Timeout on waiting finialize thread to enter waiting " + "state"); } } // Interrupt to wake up finalizeThread.interrupt(); // Second waiting state while (finalizeThread.getState() != Thread.State.WAITING) { if ((System.currentTimeMillis() - startTime) > 10000) { Assert.fail("Timeout on waiting finialize thread to enter waiting " + "state"); } } }
protected Reference<V> wrapValue(Object key, Object value) { return FinalizeManager.register((V) value, new RemoveEntryFinalizeAction((K) key)); }