1 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(); } 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); 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); assertEquals( "running should be done.", 0, runner.getRunningCount() ); 66 assertEquals( "waiting should be done.", 0, runner.getWaitingCount() ); 67 } 68 catch (InterruptedException 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); 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); 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 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); assertTrue( runner.isDoneAndGone() ); 109 } 110 catch (InterruptedException e) 111 { 112 e.printStackTrace(); 113 fail("Unexpected InterruptedException: " + e); 114 } 115 } 116 117 public void testDeadlockCase() 118 { 119 try 120 { 121 runner.close(); runner = new ThreadPerTaskAsynchronousRunner(5, 1000); DumbTask dt = new DumbTask( true ); 124 for( int i = 0; i < 5; ++i ) 125 runner.postRunnable( dt ); 126 Thread.sleep(10000); assertEquals( "running should be done.", 0, runner.getRunningCount() ); 128 } 129 catch (InterruptedException 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(); runner = new ThreadPerTaskAsynchronousRunner(5, 1000); Runnable r = new DumbTask( true ); 146 Runnable r2 = new Runnable () { 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); assertEquals( "running should be done.", 0, runner.getRunningCount() ); 153 } 154 catch (InterruptedException e) 155 { 156 e.printStackTrace(); 157 fail("Unexpected InterruptedException: " + e); 158 } 159 } 160 161 162 163 class DumbTask implements Runnable 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 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 ThreadPerTaskAsynchronousRunnerJUnitTestCase.this.notifyAll(); 195 } 196 } 197 catch ( Exception e ) 198 { e.printStackTrace(); } 199 } 200 } 201 202 public static void main(String [] argv) 203 { 204 junit.textui.TestRunner.run( new TestSuite( ThreadPerTaskAsynchronousRunnerJUnitTestCase.class ) ); 205 } 208 } | Popular Tags |