001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.async.junit;
024:
025: import junit.framework.*;
026: import com.mchange.v2.async.*;
027:
028: public class ThreadPerTaskAsynchronousRunnerJUnitTestCase extends
029: TestCase {
030: ThreadPerTaskAsynchronousRunner runner;
031:
032: boolean no_go = true;
033: int gone = 0;
034:
035: protected void setUp() {
036: runner = new ThreadPerTaskAsynchronousRunner(5);
037: }
038:
039: protected void tearDown() {
040: runner.close();
041: go(); //get any interrupt ignorers going...
042: }
043:
044: private synchronized void go() {
045: no_go = false;
046: this .notifyAll();
047: }
048:
049: public void testBasicBehavior() {
050: try {
051: DumbTask dt = new DumbTask();
052: for (int i = 0; i < 10; ++i)
053: runner.postRunnable(dt);
054: Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to the wait loop...
055: assertEquals("running count should be 5", 5, runner
056: .getRunningCount());
057: assertEquals("waiting count should be 5", 5, runner
058: .getWaitingCount());
059: go();
060: Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to finish...
061: assertEquals("running should be done.", 0, runner
062: .getRunningCount());
063: assertEquals("waiting should be done.", 0, runner
064: .getWaitingCount());
065: } catch (InterruptedException e) {
066: e.printStackTrace();
067: fail("Unexpected InterruptedException: " + e);
068: }
069: }
070:
071: public void testBasicBehaviorFastNoSkipClose() {
072: try {
073: DumbTask dt = new DumbTask();
074: for (int i = 0; i < 10; ++i)
075: runner.postRunnable(dt);
076: runner.close(false);
077: Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to the wait loop...
078: assertEquals("running count should be 5", 5, runner
079: .getRunningCount());
080: assertEquals("waiting count should be 5", 5, runner
081: .getWaitingCount());
082: go();
083: Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to finish...
084: assertEquals("running should be done.", 0, runner
085: .getRunningCount());
086: assertEquals("waiting should be done.", 0, runner
087: .getWaitingCount());
088: assertTrue(runner.isDoneAndGone());
089: } catch (InterruptedException e) {
090: e.printStackTrace();
091: fail("Unexpected InterruptedException: " + e);
092: }
093: }
094:
095: public void testBasicBehaviorFastSkipClose() {
096: try {
097: DumbTask dt = new DumbTask();
098: for (int i = 0; i < 10; ++i)
099: runner.postRunnable(dt);
100: runner.close(true);
101: Thread.sleep(1000); // not strictly safe, but should be plenty of time to interrupt and be done
102: assertTrue(runner.isDoneAndGone());
103: } catch (InterruptedException e) {
104: e.printStackTrace();
105: fail("Unexpected InterruptedException: " + e);
106: }
107: }
108:
109: public void testDeadlockCase() {
110: try {
111: runner.close(); //we need a different set up...
112: runner = new ThreadPerTaskAsynchronousRunner(5, 1000); //interrupt tasks after 1 sec, consider deadlocked after ~3 secs..
113: DumbTask dt = new DumbTask(true);
114: for (int i = 0; i < 5; ++i)
115: runner.postRunnable(dt);
116: Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
117: assertEquals("running should be done.", 0, runner
118: .getRunningCount());
119: } catch (InterruptedException e) {
120: e.printStackTrace();
121: fail("Unexpected InterruptedException: " + e);
122: }
123: }
124:
125: public void testDeadlockWithPentUpTasks() {
126: try {
127: runner.close(); //we need a different set up...
128: runner = new ThreadPerTaskAsynchronousRunner(5, 1000); //interrupt tasks after 1 sec, consider deadlocked after ~3 secs..
129: //Runnable r = new Runnable() { public synchronized void run() { while (true) { try { this.wait();} catch (Exception e) {} } } };
130: Runnable r = new DumbTask(true);
131: Runnable r2 = new Runnable() {
132: public void run() {
133: System.out.println("done.");
134: }
135: };
136: for (int i = 0; i < 5; ++i)
137: runner.postRunnable(r);
138: for (int i = 0; i < 5; ++i)
139: runner.postRunnable(r2);
140: Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
141: assertEquals("running should be done.", 0, runner
142: .getRunningCount());
143: } catch (InterruptedException e) {
144: e.printStackTrace();
145: fail("Unexpected InterruptedException: " + e);
146: }
147: }
148:
149: class DumbTask implements Runnable {
150: boolean ignore_interrupts;
151:
152: DumbTask() {
153: this (false);
154: }
155:
156: DumbTask(boolean ignore_interrupts) {
157: this .ignore_interrupts = ignore_interrupts;
158: }
159:
160: public void run() {
161: try {
162: synchronized (ThreadPerTaskAsynchronousRunnerJUnitTestCase.this ) {
163: while (no_go) {
164: try {
165: ThreadPerTaskAsynchronousRunnerJUnitTestCase.this
166: .wait();
167: } catch (InterruptedException e) {
168: if (ignore_interrupts)
169: System.err.println(this
170: + ": interrupt ignored!");
171: else {
172: e.fillInStackTrace();
173: throw e;
174: }
175: }
176: }
177: //System.err.println( ++gone );
178: ThreadPerTaskAsynchronousRunnerJUnitTestCase.this
179: .notifyAll();
180: }
181: } catch (Exception e) {
182: e.printStackTrace();
183: }
184: }
185: }
186:
187: public static void main(String[] argv) {
188: junit.textui.TestRunner.run(new TestSuite(
189: ThreadPerTaskAsynchronousRunnerJUnitTestCase.class));
190: //junit.swingui.TestRunner.run( SqlUtilsJUnitTestCase.class );
191: //new SqlUtilsJUnitTestCase().testGoodDebugLoggingOfNestedExceptions();
192: }
193: }
|