From: Leo M. <leo...@us...> - 2004-04-24 14:21:42
|
Update of /cvsroot/ozone/ozone/server/src/test/ozoneDB/core/storage/gammaStore In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13140 Modified Files: AsyncExecTest.java Log Message: updated to ensure overwriting a task works ok Index: AsyncExecTest.java =================================================================== RCS file: /cvsroot/ozone/ozone/server/src/test/ozoneDB/core/storage/gammaStore/AsyncExecTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- AsyncExecTest.java 22 Apr 2004 14:46:58 -0000 1.5 +++ AsyncExecTest.java 24 Apr 2004 14:21:32 -0000 1.6 @@ -22,9 +22,15 @@ private AsyncExec asyncExec = new AsyncExec("test", Thread.MAX_PRIORITY, true); - private Object lock = new Object(); + private static final int MAX_TASKS = 2; + private static final int INFO_NULL = 0; + private static final int INFO_CREATED = 1; + private static final int INFO_CANRUN = 2; + private static final int INFO_RUNNING = 3; + private static final int INFO_CANSTOP = 4; + private static final int INFO_STOPPING = 5; - private volatile int taskCount = 0; + private volatile int[] taskInfo = new int[MAX_TASKS]; public AsyncExecTest() { super("testAsyncExec"); @@ -37,52 +43,77 @@ return suite; } - public void xtestAsyncExec() { - Object key1 = new Integer(0); - Object key2 = new Integer(1); - synchronized (lock){ - asyncExec.put(key1, new Task()); - asyncExec.put(key2, new Task()); - try { - lock.wait(); - } catch (InterruptedException ignore) { - } - assertNotNull(asyncExec.get(key2)); - assertEquals(taskCount, 0); - assertNull(asyncExec.get(key1)); - assertEquals(taskCount, 1); - try { - lock.wait(); - } catch (InterruptedException ignore) { + private synchronized int getInfo(int num) { + return taskInfo[num]; + } + + private synchronized void setInfo(int num, int shouldBe, int newValue) { + for (;;) { + if (taskInfo[num] == shouldBe) { + taskInfo[num] = newValue; + notifyAll(); + return; + } else { + try { + wait(); + } catch (InterruptedException ignore) { + } } - assertNull(asyncExec.get(key2)); - assertEquals(taskCount, 2); - asyncExec.stopWhenReady(); } } - public void testOverwriteTask() { - Object key = new Integer(0); - synchronized (lock){ - asyncExec.put(key, new Task()); - Thread.yield(); - asyncExec.put(key, new Task()); - try { - lock.wait(); - } catch (InterruptedException ignore) { + private synchronized void ensureInfo(int num, int shouldBe) { + for (;;) { + if (taskInfo[num] == shouldBe) { + return; + } else { + try { + wait(); + } catch (InterruptedException ignore) { + } } - assertNotNull(asyncExec.get(key)); - assertEquals(taskCount, 0); + } + } + + public void testAsyncExec() { + Object key0 = new Integer(0); + Object key1 = new Integer(1); + asyncExec.put(key0, new Task(0)); + asyncExec.put(key1, new Task(1)); + assertEquals(asyncExec.size(), 2); + setInfo(0, INFO_CREATED, INFO_CANRUN); + setInfo(1, INFO_CREATED, INFO_CANRUN); + assertEquals(asyncExec.size(), 2); + setInfo(0, INFO_RUNNING, INFO_CANSTOP); + setInfo(1, INFO_RUNNING, INFO_CANSTOP); + long now = System.currentTimeMillis(); + do { try { - lock.wait(); + Thread.sleep(100); // should be enough for both tasks to be removed from asyncExec } catch (InterruptedException ignore) { } - assertNull(asyncExec.get(key)); - assertEquals(taskCount, 1); - asyncExec.stopWhenReady(); - } + } while (System.currentTimeMillis() < now + 100); + assertNull(asyncExec.get(key0)); + assertNull(asyncExec.get(key1)); + assertEquals(asyncExec.size(), 0); + asyncExec.stopWhenReady(); } + public void testOverwrite() { + Object key0 = new Integer(0); + asyncExec.put(key0, new Task(0)); + setInfo(0, INFO_CREATED, INFO_CANRUN); + setInfo(0, INFO_RUNNING, INFO_CANSTOP); + asyncExec.put(key0, new Task(1)); + assertEquals(asyncExec.size(), 1); + setInfo(1, INFO_CREATED, INFO_CANRUN); + ensureInfo(1, INFO_RUNNING); + ensureInfo(0, INFO_STOPPING); + setInfo(1, INFO_RUNNING, INFO_CANSTOP); + assertNull(asyncExec.get(key0)); + asyncExec.stopWhenReady(); + } + public static void main(String[] args) { TestRunner testRunner = new TestRunner(); testRunner.run(AsyncExecTest.class); @@ -90,15 +121,23 @@ private class Task implements Runnable { + private int num; + + public Task(int num) { + this.num = num; + setInfo(num, INFO_NULL, INFO_CREATED); + } + public void run() { - synchronized (lock) { - lock.notifyAll(); - } - try { - Thread.sleep(1000); - } catch (InterruptedException ignore) { - } - taskCount++; + setInfo(num, INFO_CANRUN, INFO_RUNNING); + setInfo(num, INFO_CANSTOP, INFO_STOPPING); + long now = System.currentTimeMillis(); + do { + try { + Thread.sleep(10); + } catch (InterruptedException ignore) { + } + } while (System.currentTimeMillis() < now + 25); } } |