001/* 002 * $Id: ThreadPoolTest.java 5863 2018-07-20 11:13:34Z kredel $ 003 */ 004 005//package edu.unima.ky.parallel; 006package edu.jas.util; 007 008import junit.framework.Test; 009import junit.framework.TestCase; 010import junit.framework.TestSuite; 011 012 013 014/** 015 * ThreadPool tests with JUnit. 016 * @author Akitoshi Yoshida 017 * @author Heinz Kredel 018 */ 019public class ThreadPoolTest extends TestCase { 020 021 022/** 023 * main. 024 */ 025 public static void main (String[] args) { 026 junit.textui.TestRunner.run( suite() ); 027 } 028 029 030/** 031 * Constructs a <CODE>ThreadPoolTest</CODE> object. 032 * @param name String. 033 */ 034 public ThreadPoolTest(String name) { 035 super(name); 036 } 037 038 039/** 040 */ 041 public static Test suite() { 042 TestSuite suite= new TestSuite(ThreadPoolTest.class); 043 return suite; 044 } 045 046 047 static final int JOBS = 10; // number of jobs to start 048 049 050 private ThreadPool p1; 051 052 053 protected void setUp() { 054 } 055 056 057 protected void tearDown() { 058 p1.terminate(); 059 } 060 061 062/** 063 * Tests if the created ThreadPool is empty. 064 */ 065 public void testThreadPool1() { 066 p1 = new ThreadPool(0); 067 assertTrue( "not empty pool ", p1.getNumber() == 0 ); 068 } 069 070 071/** 072 * Tests if the created ThreadPool is non empty. 073 */ 074 public void testThreadPool2() { 075 p1 = new ThreadPool(1); 076 //p1.init(); 077 assertTrue( "# empty pool ", p1.getNumber() == 1 ); 078 p1.terminate(); 079 080 p1 = new ThreadPool(); 081 //p1.init(); 082 assertTrue( "# empty pool ", p1.getNumber() == ThreadPool.DEFAULT_SIZE ); 083 p1.terminate(); 084 085 p1 = new ThreadPool(10); 086 //p1.init(); 087 assertTrue( "# empty pool ", p1.getNumber() == 10 ); 088 p1.terminate(); 089 } 090 091 092/** 093 * Tests if the created ThreadPool has no jobs. 094 */ 095 public void testThreadPool3() { 096 p1 = new ThreadPool(); 097 assertFalse( "no jobs ", p1.hasJobs() ); 098 assertFalse( "more than 0 jobs ", p1.hasJobs(0) ); 099 p1.terminate(); 100 } 101 102 103/** 104 * Tests if the created ThreadPool has jobs. 105 */ 106 public void testThreadPool4() { 107 p1 = new ThreadPool(); 108 assertFalse( "no jobs ", p1.hasJobs() ); 109 for (int i = 0; i < JOBS*p1.getNumber(); i++ ) { 110 p1.addJob( new FastWorker() ); 111 } 112 boolean j = p1.hasJobs(); 113 assertTrue( "more than 0 jobs ", (j | true) ); // stupid 114 p1.terminate(); 115 assertFalse( "no jobs ", p1.hasJobs() ); 116 } 117 118 119/** 120 * Tests if the created ThreadPool has many jobs. 121 */ 122 public void testThreadPool5() { 123 p1 = new ThreadPool(); 124 //p1.init(); 125 assertFalse( "no jobs ", p1.hasJobs() ); 126 for (int i = 0; i < JOBS*p1.getNumber(); i++ ) { 127 p1.addJob( new SlowWorker() ); 128 } 129 assertTrue( "more than 10 jobs ", p1.hasJobs(JOBS) ); 130 p1.terminate(); 131 assertFalse( "no jobs ", p1.hasJobs() ); 132 } 133 134 135/** 136 * Tests if the created ThreadPool has correct strategy. 137 */ 138 public void testThreadPool6() { 139 p1 = new ThreadPool(StrategyEnumeration.LIFO); 140 assertTrue( "FIFO strategy ", 141 p1.getStrategy() == StrategyEnumeration.LIFO ); 142 } 143 144 145/** 146 * Tests if the created ThreadPool has jobs and correct strategy. 147 */ 148 public void testThreadPool7() { 149 p1 = new ThreadPool(StrategyEnumeration.LIFO); 150 assertFalse( "no jobs ", p1.hasJobs() ); 151 for (int i = 0; i < JOBS*p1.getNumber(); i++ ) { 152 p1.addJob( new FastWorker() ); 153 } 154 boolean j = p1.hasJobs(); 155 assertTrue( "more than 0 jobs ", (j | true) ); // stupid 156 p1.terminate(); 157 assertFalse( "no jobs ", p1.hasJobs() ); 158 } 159 160} 161 162 163/** 164 * Utility class for ThreadPool Test. 165 */ 166class FastWorker implements Runnable { 167 public void run() { 168 } 169} 170 171/** 172 * Utility class for ThreadPool Test. 173 */ 174class SlowWorker implements Runnable { 175 public void run() { 176 try { 177 Thread.sleep(10); 178 } catch (InterruptedException e ) { 179 } 180 } 181}