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 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(); } 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); } 69 catch (InterruptedException e) 70 { 71 e.printStackTrace(); 72 fail("Unexpected InterruptedException: " + e); 73 } 74 } 75 76 class DumbTask implements Runnable 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 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 ThreadPoolAsynchronousRunnerJUnitTestCase.this.notifyAll(); 108 } 109 } 110 catch ( Exception e ) 111 { e.printStackTrace(); } 112 } 113 } 114 115 public static void main(String [] argv) 116 { 117 junit.textui.TestRunner.run( new TestSuite( ThreadPoolAsynchronousRunnerJUnitTestCase.class ) ); 118 } 121 } | Popular Tags |