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