KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > async > junit > ThreadPoolAsynchronousRunnerJUnitTestCase


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.async.junit;
25
26 import junit.framework.*;
27 import com.mchange.v2.async.*;
28
29 public class ThreadPoolAsynchronousRunnerJUnitTestCase extends TestCase
30 {
31     ThreadPoolAsynchronousRunner runner;
32
33     boolean no_go = true;
34     int gone = 0;
35
36     protected void setUp()
37     {
38     runner = new ThreadPoolAsynchronousRunner( 3,
39                            true,
40                            1000,
41                            3 * 1000,
42                            3 * 1000);
43     }
44
45     protected void tearDown()
46     {
47     runner.close();
48     go(); //get any interrupt ignorers going...
49
}
50
51     private synchronized void go()
52     {
53     no_go = false;
54     this.notifyAll();
55     }
56
57     public void testDeadlockCase()
58     {
59     try
60         {
61         DumbTask dt = new DumbTask( true );
62         for( int i = 0; i < 5; ++i )
63             runner.postRunnable( dt );
64         Thread.sleep(500);
65         assertEquals("we should have three running tasks", 3, runner.getActiveCount() );
66         assertEquals("we should have two pending tasks", 2, runner.getPendingTaskCount() );
67         Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
68
}
69     catch (InterruptedException JavaDoc e)
70         {
71         e.printStackTrace();
72         fail("Unexpected InterruptedException: " + e);
73         }
74     }
75
76     class DumbTask implements Runnable JavaDoc
77     {
78     boolean ignore_interrupts;
79
80     DumbTask()
81     { this( false ); }
82
83     DumbTask(boolean ignore_interrupts)
84     { this.ignore_interrupts = ignore_interrupts; }
85
86     public void run()
87     {
88         try
89         {
90             synchronized (ThreadPoolAsynchronousRunnerJUnitTestCase.this)
91             {
92                 while (no_go)
93                 {
94                     try { ThreadPoolAsynchronousRunnerJUnitTestCase.this.wait(); }
95                     catch (InterruptedException JavaDoc e)
96                     {
97                         if (ignore_interrupts)
98                         System.err.println(this + ": interrupt ignored!");
99                         else
100                         {
101                             e.fillInStackTrace();
102                             throw e;
103                         }
104                     }
105                 }
106                 //System.err.println( ++gone );
107
ThreadPoolAsynchronousRunnerJUnitTestCase.this.notifyAll();
108             }
109         }
110         catch ( Exception JavaDoc e )
111         { e.printStackTrace(); }
112     }
113     }
114
115     public static void main(String JavaDoc[] argv)
116     {
117     junit.textui.TestRunner.run( new TestSuite( ThreadPoolAsynchronousRunnerJUnitTestCase.class ) );
118     //junit.swingui.TestRunner.run( SqlUtilsJUnitTestCase.class );
119
//new SqlUtilsJUnitTestCase().testGoodDebugLoggingOfNestedExceptions();
120
}
121 }
Popular Tags