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