001: // $Id: PromiseTest.java,v 1.3 2006/09/22 11:34:16 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.TestCase;
006: import org.jgroups.TimeoutException;
007: import org.jgroups.util.Promise;
008: import org.jgroups.util.Util;
009:
010: /**
011: * Various test cases for Promise
012: * @author Bela Ban
013: */
014: public class PromiseTest extends TestCase {
015: Promise p;
016:
017: public PromiseTest(String name) {
018: super (name);
019: }
020:
021: public void setUp() throws Exception {
022: super .setUp();
023: p = new Promise();
024: }
025:
026: public void tearDown() throws Exception {
027: p.reset();
028: super .tearDown();
029: }
030:
031: public void testGetResultNoTimeout() {
032: Object result;
033: new ResultSetter(p, 500).start();
034: result = p.getResult(0);
035: assertEquals(result, Boolean.TRUE);
036: }
037:
038: public void testGetResultNoTimeout_ResultAlreadySet() {
039: Object result;
040: new ResultSetter(p, 1).start();
041: Util.sleep(100);
042: result = p.getResult(0);
043: assertEquals(result, Boolean.TRUE);
044: }
045:
046: public void testGetResultWithTimeout() {
047: try {
048: p.getResultWithTimeout(500);
049: fail("this should throw a TimeoutException");
050: } catch (TimeoutException e) {
051: assertTrue(e != null);
052: }
053: }
054:
055: public void testGetResultWithTimeoutNoException() {
056: Object ret = p.getResult(500);
057: assertNull(ret);
058: }
059:
060: public void testGetResultWithTimeoutAndInterrupt() {
061: new Interrupter(Thread.currentThread(), 100).start();
062: Object result = p.getResult(500);
063: assertNull(result);
064: }
065:
066: public void testGetResultWithTimeoutAndResultSetter() {
067: Thread t = new Thread() {
068: public void run() {
069: Util.sleep(500);
070: System.out.println("-- setting promise to \"Bela\"");
071: p.setResult("Bela");
072: }
073: };
074: t.start();
075: long start = System.currentTimeMillis(), stop;
076: Object result = p.getResult(100000);
077: stop = System.currentTimeMillis();
078: System.out.println("-- waited for " + (stop - start)
079: + "ms, result is " + result);
080: assertNotNull(result);
081: assertEquals("Bela", result);
082: assertFalse("promise was reset after getResult()", p
083: .hasResult());
084: }
085:
086: static class ResultSetter extends Thread {
087: long wait_time = 2000;
088: Promise target = null;
089:
090: ResultSetter(Promise target, long wait_time) {
091: this .target = target;
092: this .wait_time = wait_time;
093: }
094:
095: public void run() {
096: Util.sleep(wait_time);
097: target.setResult(Boolean.TRUE);
098: }
099: }
100:
101: static class Interrupter extends Thread {
102: long wait_time = 2000;
103: Thread target = null;
104:
105: Interrupter(Thread target, long wait_time) {
106: this .target = target;
107: this .wait_time = wait_time;
108: }
109:
110: public void run() {
111: Util.sleep(wait_time);
112: target.interrupt();
113: }
114: }
115:
116: public static void main(String[] args) {
117: String[] testCaseName = { PromiseTest.class.getName() };
118: junit.textui.TestRunner.main(testCaseName);
119: }
120:
121: }
|