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