KickJava   Java API By Example, From Geeks To Geeks.

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


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 ThreadPerTaskAsynchronousRunnerJUnitTestCase extends TestCase
30 {
31     ThreadPerTaskAsynchronousRunner runner;
32
33     boolean no_go = true;
34     int gone = 0;
35
36     protected void setUp()
37     {
38     runner = new ThreadPerTaskAsynchronousRunner(5);
39     }
40
41     protected void tearDown()
42     {
43     runner.close();
44     go(); //get any interrupt ignorers going...
45
}
46
47     private synchronized void go()
48     {
49     no_go = false;
50     this.notifyAll();
51     }
52
53     public void testBasicBehavior()
54     {
55     try
56         {
57         DumbTask dt = new DumbTask();
58         for( int i = 0; i < 10; ++i )
59             runner.postRunnable( dt );
60         Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to the wait loop...
61
assertEquals( "running count should be 5", 5, runner.getRunningCount() );
62         assertEquals( "waiting count should be 5", 5, runner.getWaitingCount() );
63         go();
64         Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to finish...
65
assertEquals( "running should be done.", 0, runner.getRunningCount() );
66         assertEquals( "waiting should be done.", 0, runner.getWaitingCount() );
67         }
68     catch (InterruptedException JavaDoc e)
69         {
70         e.printStackTrace();
71         fail("Unexpected InterruptedException: " + e);
72         }
73     }
74
75     public void testBasicBehaviorFastNoSkipClose()
76     {
77     try
78         {
79         DumbTask dt = new DumbTask();
80         for( int i = 0; i < 10; ++i )
81             runner.postRunnable( dt );
82         runner.close( false );
83         Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to the wait loop...
84
assertEquals( "running count should be 5", 5, runner.getRunningCount() );
85         assertEquals( "waiting count should be 5", 5, runner.getWaitingCount() );
86         go();
87         Thread.sleep(1000); // not strictly safe, but should be plenty of time to get our tasks to finish...
88
assertEquals( "running should be done.", 0, runner.getRunningCount() );
89         assertEquals( "waiting should be done.", 0, runner.getWaitingCount() );
90         assertTrue( runner.isDoneAndGone() );
91         }
92     catch (InterruptedException JavaDoc e)
93         {
94         e.printStackTrace();
95         fail("Unexpected InterruptedException: " + e);
96         }
97     }
98
99     public void testBasicBehaviorFastSkipClose()
100     {
101     try
102         {
103         DumbTask dt = new DumbTask();
104         for( int i = 0; i < 10; ++i )
105             runner.postRunnable( dt );
106         runner.close( true );
107         Thread.sleep(1000); // not strictly safe, but should be plenty of time to interrupt and be done
108
assertTrue( runner.isDoneAndGone() );
109         }
110     catch (InterruptedException JavaDoc e)
111         {
112         e.printStackTrace();
113         fail("Unexpected InterruptedException: " + e);
114         }
115     }
116
117     public void testDeadlockCase()
118     {
119     try
120         {
121         runner.close(); //we need a different set up...
122
runner = new ThreadPerTaskAsynchronousRunner(5, 1000); //interrupt tasks after 1 sec, consider deadlocked after ~3 secs..
123
DumbTask dt = new DumbTask( true );
124         for( int i = 0; i < 5; ++i )
125             runner.postRunnable( dt );
126         Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
127
assertEquals( "running should be done.", 0, runner.getRunningCount() );
128         }
129     catch (InterruptedException JavaDoc e)
130         {
131         e.printStackTrace();
132         fail("Unexpected InterruptedException: " + e);
133         }
134     }
135
136     
137
138     public void testDeadlockWithPentUpTasks()
139     {
140     try
141         {
142         runner.close(); //we need a different set up...
143
runner = new ThreadPerTaskAsynchronousRunner(5, 1000); //interrupt tasks after 1 sec, consider deadlocked after ~3 secs..
144
//Runnable r = new Runnable() { public synchronized void run() { while (true) { try { this.wait();} catch (Exception e) {} } } };
145
Runnable JavaDoc r = new DumbTask( true );
146         Runnable JavaDoc r2 = new Runnable JavaDoc() { public void run() { System.out.println("done."); } };
147         for( int i = 0; i < 5; ++i )
148             runner.postRunnable( r );
149         for( int i = 0; i < 5; ++i )
150             runner.postRunnable( r2 );
151         Thread.sleep(10000); // not strictly safe, but should be plenty of time to interrupt and be done
152
assertEquals( "running should be done.", 0, runner.getRunningCount() );
153         }
154     catch (InterruptedException JavaDoc e)
155         {
156         e.printStackTrace();
157         fail("Unexpected InterruptedException: " + e);
158         }
159     }
160
161     
162
163     class DumbTask implements Runnable JavaDoc
164     {
165     boolean ignore_interrupts;
166
167     DumbTask()
168     { this( false ); }
169
170     DumbTask(boolean ignore_interrupts)
171     { this.ignore_interrupts = ignore_interrupts; }
172
173     public void run()
174     {
175         try
176         {
177             synchronized (ThreadPerTaskAsynchronousRunnerJUnitTestCase.this)
178             {
179                 while (no_go)
180                 {
181                     try { ThreadPerTaskAsynchronousRunnerJUnitTestCase.this.wait(); }
182                     catch (InterruptedException JavaDoc e)
183                     {
184                         if (ignore_interrupts)
185                         System.err.println(this + ": interrupt ignored!");
186                         else
187                         {
188                             e.fillInStackTrace();
189                             throw e;
190                         }
191                     }
192                 }
193                 //System.err.println( ++gone );
194
ThreadPerTaskAsynchronousRunnerJUnitTestCase.this.notifyAll();
195             }
196         }
197         catch ( Exception JavaDoc e )
198         { e.printStackTrace(); }
199     }
200     }
201
202     public static void main(String JavaDoc[] argv)
203     {
204     junit.textui.TestRunner.run( new TestSuite( ThreadPerTaskAsynchronousRunnerJUnitTestCase.class ) );
205     //junit.swingui.TestRunner.run( SqlUtilsJUnitTestCase.class );
206
//new SqlUtilsJUnitTestCase().testGoodDebugLoggingOfNestedExceptions();
207
}
208 }
Popular Tags