001: // $Id: CondVarTest.java,v 1.3 2005/07/26 18:36:00 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import EDU.oswego.cs.dl.util.concurrent.FutureResult;
006: import EDU.oswego.cs.dl.util.concurrent.TimeoutException;
007: import junit.framework.TestCase;
008: import org.jgroups.util.Util;
009: import org.jgroups.util.CondVar;
010:
011: import java.lang.reflect.InvocationTargetException;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: /**
016: * Various test cases for CondVar
017: * @author Bela Ban
018: */
019: public class CondVarTest extends TestCase {
020: CondVar cond = new CondVar("blocking", Boolean.FALSE);
021:
022: public CondVarTest(String name) {
023: super (name);
024: }
025:
026: public void setUp() throws Exception {
027: super .setUp();
028: }
029:
030: public void tearDown() throws Exception {
031: super .tearDown();
032: }
033:
034: public void testConditionTrue() {
035: try {
036: cond.waitUntilWithTimeout(Boolean.FALSE, 500);
037: } catch (org.jgroups.TimeoutException e) {
038: fail("received TimeoutException");
039: }
040: }
041:
042: public void testConditionTrueWaitForever() {
043: cond.waitUntil(Boolean.FALSE);
044: }
045:
046: public void testWithTimeoutException() {
047: try {
048: cond.waitUntilWithTimeout(Boolean.TRUE, 500);
049: fail("expected timeout exception");
050: } catch (org.jgroups.TimeoutException e) {
051: }
052: }
053:
054: public void testWithResultSetter()
055: throws org.jgroups.TimeoutException {
056: new ResultSetter(cond, 500).start();
057: cond.waitUntilWithTimeout(Boolean.TRUE, 2000);
058: }
059:
060: public void testWithResultSetter_ResultSetBeforeAccess()
061: throws org.jgroups.TimeoutException {
062: new ResultSetter(cond, 10).start();
063: Util.sleep(100);
064: cond.waitUntilWithTimeout(Boolean.TRUE, 2000);
065: }
066:
067: public void testDoubleLocking() throws org.jgroups.TimeoutException {
068: final Map m = new HashMap();
069: final CondVar c = new CondVar("bla", Boolean.FALSE, m);
070:
071: new Thread() {
072: public void run() {
073: Util.sleep(1000);
074: _setValue(m, c);
075: }
076: }.start();
077:
078: _enterMonitor(m, c);
079: }
080:
081: private void _setValue(Map m, CondVar c) {
082: log("acquiring m");
083: synchronized (m) {
084: log("acquired m. setting c");
085: c.set(Boolean.TRUE);
086: log("set c. released c");
087: }
088: log("released m");
089: }
090:
091: private void _enterMonitor(final Map m, final CondVar c)
092: throws org.jgroups.TimeoutException {
093: log("acquiring m");
094: synchronized (m) {
095: log("acquired m. acquiring and waiting on c");
096: c.waitUntilWithTimeout(Boolean.TRUE, 10000);
097: log("released c");
098: }
099: log("released m");
100: }
101:
102: private void log(String msg) {
103: System.out.println(System.currentTimeMillis() + " "
104: + Thread.currentThread() + " - " + msg);
105: }
106:
107: public void testStressOnGet() {
108: long start, stop;
109: long NUM = 1000000L;
110: start = System.currentTimeMillis();
111: for (int i = 0; i < NUM; i++) {
112: if (cond.get().equals(Boolean.TRUE))
113: ;
114: }
115: stop = System.currentTimeMillis();
116: long diff = stop - start;
117: diff *= 1000; // microsecs
118: double microsecs_per_get = diff / (double) NUM;
119: System.out.println("took " + microsecs_per_get
120: + " microsecs/get for " + NUM + " gets (" + diff
121: + " microsecs)");
122: }
123:
124: class ResultSetter extends Thread {
125: long wait_time = 2000;
126: CondVar target = null;
127:
128: ResultSetter(CondVar target, long wait_time) {
129: this .target = target;
130: this .wait_time = wait_time;
131: }
132:
133: public void run() {
134: Util.sleep(wait_time);
135: System.out.println("-- [ResultSetter] set result to true");
136: target.set(Boolean.TRUE);
137: System.out
138: .println("-- [ResultSetter] set result to true -- DONE");
139: }
140: }
141:
142: public static void main(String[] args) {
143: String[] testCaseName = { CondVarTest.class.getName() };
144: junit.textui.TestRunner.main(testCaseName);
145: }
146:
147: }
|