KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > CondVarTest


1 // $Id: CondVarTest.java,v 1.2 2004/09/23 16:30:01 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import EDU.oswego.cs.dl.util.concurrent.FutureResult;
7 import EDU.oswego.cs.dl.util.concurrent.TimeoutException;
8 import junit.framework.TestCase;
9 import org.jgroups.util.Util;
10 import org.jgroups.util.CondVar;
11
12 import java.lang.reflect.InvocationTargetException JavaDoc;
13
14
15 /**
16  * Various test cases for CondVar
17  * @author Bela Ban
18  */

19 public class CondVarTest extends TestCase {
20     CondVar cond=new CondVar("blocking", Boolean.FALSE);
21
22
23     public CondVarTest(String JavaDoc name) {
24         super(name);
25     }
26
27     public void setUp() throws Exception JavaDoc {
28         super.setUp();
29     }
30
31     public void tearDown() throws Exception JavaDoc {
32         super.tearDown();
33     }
34
35
36     public void testConditionTrue() {
37         try {
38             cond.waitUntilWithTimeout(Boolean.FALSE, 500);
39         }
40         catch(org.jgroups.TimeoutException e) {
41             fail("received TimeoutException");
42         }
43     }
44
45     public void testConditionTrueWaitForever() {
46         cond.waitUntil(Boolean.FALSE);
47     }
48
49
50     public void testWithTimeoutException() {
51         try {
52             cond.waitUntilWithTimeout(Boolean.TRUE, 500);
53             fail("expected timeout exception");
54         }
55         catch(org.jgroups.TimeoutException e) {
56         }
57     }
58
59
60     public void testWithResultSetter() throws org.jgroups.TimeoutException {
61         new ResultSetter(cond, 500).start();
62         cond.waitUntilWithTimeout(Boolean.TRUE, 2000);
63     }
64
65     public void testWithResultSetter_ResultSetBeforeAccess() throws org.jgroups.TimeoutException {
66         new ResultSetter(cond, 10).start();
67         Util.sleep(100);
68         cond.waitUntilWithTimeout(Boolean.TRUE, 2000);
69     }
70
71
72     public void testStressOnGet() {
73         long start, stop;
74         long NUM=1000000L;
75         start=System.currentTimeMillis();
76         for(int i=0; i < NUM; i++) {
77             if(cond.get().equals(Boolean.TRUE))
78                 ;
79         }
80         stop=System.currentTimeMillis();
81         long diff=stop-start;
82         diff*=1000; // microsecs
83
double microsecs_per_get=diff / (double)NUM;
84         System.out.println("took " + microsecs_per_get + " microsecs/get for " + NUM + " gets (" + diff + " microsecs)");
85     }
86
87
88     class ResultSetter extends Thread JavaDoc {
89         long wait_time=2000;
90         CondVar target=null;
91
92         ResultSetter(CondVar target, long wait_time) {
93             this.target=target;
94             this.wait_time=wait_time;
95         }
96
97         public void run() {
98             Util.sleep(wait_time);
99             System.out.println("-- [ResultSetter] set result to true");
100             target.set(Boolean.TRUE);
101             System.out.println("-- [ResultSetter] set result to true -- DONE");
102         }
103     }
104
105
106
107     public static void main(String JavaDoc[] args) {
108         String JavaDoc[] testCaseName={CondVarTest.class.getName()};
109         junit.textui.TestRunner.main(testCaseName);
110     }
111
112 }
113
Popular Tags