KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > core > storage > gammaStore > AsyncExecTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2004 Leo Mekenkamp. All rights reserved.
5
//
6
// $Id$
7

8 package test.ozoneDB.core.storage.gammaStore;
9
10 import junit.framework.Test;
11 import junit.framework.TestResult;
12 import junit.framework.TestSuite;
13 import junit.textui.TestRunner;
14 import org.ozoneDB.core.storage.gammaStore.AsyncExec;
15 import test.OzoneTestCase;
16 import test.OzoneTestRunner;
17
18 /**
19  * @author leo
20  */

21 public class AsyncExecTest extends OzoneTestCase {
22     
23     private AsyncExec asyncExec = new AsyncExec("test", Thread.MAX_PRIORITY, true);
24     
25     private Object JavaDoc lock = new Object JavaDoc();
26     
27     private volatile int taskCount = 0;
28     
29     public AsyncExecTest() {
30         super("testAsyncExec");
31     }
32     
33     public static Test suite() {
34         TestSuite suite = new TestSuite();
35
36         suite.addTest(new LocTest());
37         return suite;
38     }
39
40     public void testAsyncExec() {
41         Object JavaDoc key1 = new Integer JavaDoc(0);
42         Object JavaDoc key2 = new Integer JavaDoc(1);
43         synchronized (lock){
44             asyncExec.put(key1, new Task());
45             asyncExec.put(key2, new Task());
46             try {
47                 lock.wait();
48             } catch (InterruptedException JavaDoc ignore) {
49             }
50             assertNotNull(asyncExec.get(key2));
51             assertEquals(taskCount, 0);
52             assertNull(asyncExec.get(key1));
53             assertEquals(taskCount, 1);
54             try {
55                 lock.wait();
56             } catch (InterruptedException JavaDoc ignore) {
57             }
58             assertNull(asyncExec.get(key2));
59             assertEquals(taskCount, 2);
60             asyncExec.stopWhenReady();
61         }
62     }
63     
64     public static void main(String JavaDoc[] args) {
65         TestRunner testRunner = new TestRunner();
66         testRunner.run(AsyncExecTest.class);
67     }
68
69     private class Task implements Runnable JavaDoc {
70         
71         public void run() {
72             synchronized (lock) {
73                 lock.notifyAll();
74             }
75             try {
76                 Thread.sleep(1000);
77             } catch (InterruptedException JavaDoc ignore) {
78             }
79             taskCount++;
80         }
81         
82     }
83 }
84
Popular Tags