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 ThreadPoolAsynchronousRunnerJUnitTestCase extends TestCase {
029: ThreadPoolAsynchronousRunner runner;
030:
031: boolean no_go = true;
032: int gone = 0;
033:
034: protected void setUp() {
035: runner = new ThreadPoolAsynchronousRunner(3, true, 1000,
036: 3 * 1000, 3 * 1000);
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 testDeadlockCase() {
050: try {
051: DumbTask dt = new DumbTask(true);
052: for (int i = 0; i < 5; ++i)
053: runner.postRunnable(dt);
054: Thread.sleep(500);
055: assertEquals("we should have three running tasks", 3,
056: runner.getActiveCount());
057: assertEquals("we should have two pending tasks", 2, runner
058: .getPendingTaskCount());
059: Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
060: } catch (InterruptedException e) {
061: e.printStackTrace();
062: fail("Unexpected InterruptedException: " + e);
063: }
064: }
065:
066: class DumbTask implements Runnable {
067: boolean ignore_interrupts;
068:
069: DumbTask() {
070: this (false);
071: }
072:
073: DumbTask(boolean ignore_interrupts) {
074: this .ignore_interrupts = ignore_interrupts;
075: }
076:
077: public void run() {
078: try {
079: synchronized (ThreadPoolAsynchronousRunnerJUnitTestCase.this ) {
080: while (no_go) {
081: try {
082: ThreadPoolAsynchronousRunnerJUnitTestCase.this
083: .wait();
084: } catch (InterruptedException e) {
085: if (ignore_interrupts)
086: System.err.println(this
087: + ": interrupt ignored!");
088: else {
089: e.fillInStackTrace();
090: throw e;
091: }
092: }
093: }
094: //System.err.println( ++gone );
095: ThreadPoolAsynchronousRunnerJUnitTestCase.this
096: .notifyAll();
097: }
098: } catch (Exception e) {
099: e.printStackTrace();
100: }
101: }
102: }
103:
104: public static void main(String[] argv) {
105: junit.textui.TestRunner.run(new TestSuite(
106: ThreadPoolAsynchronousRunnerJUnitTestCase.class));
107: //junit.swingui.TestRunner.run( SqlUtilsJUnitTestCase.class );
108: //new SqlUtilsJUnitTestCase().testGoodDebugLoggingOfNestedExceptions();
109: }
110: }
|