Use a native window to catch shutdown events.

This commit is contained in:
akwizgran
2011-11-19 15:45:23 +01:00
parent 046becd388
commit 3dc8a590c7
3 changed files with 116 additions and 60 deletions

View File

@@ -12,7 +12,7 @@ public class ShutdownManagerImplTest extends TestCase {
@Test
public void testAddAndRemove() {
ShutdownManager s = new ShutdownManagerImpl();
ShutdownManager s = createShutdownManager();
Set<Integer> handles = new HashSet<Integer>();
for(int i = 0; i < 100; i++) {
int handle = s.addShutdownHook(new Runnable() {
@@ -26,4 +26,8 @@ public class ShutdownManagerImplTest extends TestCase {
// The hooks should no longer be removable
for(int handle : handles) assertFalse(s.removeShutdownHook(handle));
}
protected ShutdownManager createShutdownManager() {
return new ShutdownManagerImpl();
}
}

View File

@@ -0,0 +1,39 @@
package net.sf.briar.lifecycle;
import net.sf.briar.api.lifecycle.ShutdownManager;
import org.junit.Test;
public class WindowsShutdownManagerImplTest extends ShutdownManagerImplTest {
@Override
protected ShutdownManager createShutdownManager() {
return new WindowsShutdownManagerImpl();
}
@Test
public void testManagerWaitsForHooksToRun() {
WindowsShutdownManagerImpl s = new WindowsShutdownManagerImpl();
SlowHook[] hooks = new SlowHook[10];
for(int i = 0; i < hooks.length; i++) {
hooks[i] = new SlowHook();
s.addShutdownHook(hooks[i]);
}
s.runShutdownHooks();
for(int i = 0; i < hooks.length; i++) assertTrue(hooks[i].finished);
}
private static class SlowHook implements Runnable {
private volatile boolean finished = false;
public void run() {
try {
Thread.sleep(100);
finished = true;
} catch(InterruptedException e) {
// Don't finish
}
}
}
}