Update of /cvsroot/commonjava/commonjava-projects/commonjava-util/src/test/org/commonjava/util
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2943/src/test/org/commonjava/util
Added Files:
ThreadThrottleTest.java
Log Message:
flushed out most of the problems with ThreadThrottle. It should work properly now.
--- NEW FILE: ThreadThrottleTest.java ---
/*
* Created on Feb 10, 2004
*
*/
package org.commonjava.util;
import java.util.EmptyStackException;
import junit.framework.TestCase;
//TODO: Add JavaDoc to ThreadThrottleTest in commonjava-util!
/**
*
* @
*
*/
public class ThreadThrottleTest extends TestCase {
/**
* Constructor for ThreadThrottleTest.
* @param arg0
*/
public ThreadThrottleTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(ThreadThrottleTest.class);
}
public void testPendingExceededInfiniteWait() throws Exception{
ThreadThrottle throttle = new ThreadThrottle(25, 10, -1);
for(int i=0; i<75; i++){
try{
Thread th = throttle.getThread(new TestRunnable(10));
th.setPriority(Thread.currentThread().getPriority()-1);
th.start();
}
catch(Exception e){
e.printStackTrace();
throw e;
}
}
}
public void testPendingExceededSmallWait(){
ThreadThrottle throttle = new ThreadThrottle(25, 5, 500);
for(int i=0; i<75; i++){
try{
Thread th = throttle.getThread(new TestRunnable(5));
th.setPriority(Thread.currentThread().getPriority()-1);
System.out.println("Starting thread " + i);
th.start();
}
catch(EmptyStackException e){
System.out.println("Timeout on thread retrieval from throttle on thread: " + i);
}
}
}
public void testJoinAllMicroPopWaitInfiniteJoinWait(){
ThreadThrottle throttle = new ThreadThrottle(25, 10, 10);
for(int i=0; i<75; i++){
try{
Thread th = throttle.getThread(new TestRunnable(10));
th.setPriority(Thread.currentThread().getPriority()-1);
System.out.println("Starting thread " + i);
th.start();
}
catch(EmptyStackException e){
System.out.println("Timeout on thread retrieval from throttle on thread: " + i);
}
}
System.out.println("joining all threads.");
try {
throttle.joinAll();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("join finished.");
}
public void testJoinAllMicroPopWaitSmallJoinWait(){
ThreadThrottle throttle = new ThreadThrottle(25, 10, 10);
for(int i=0; i<75; i++){
try{
Thread th = throttle.getThread(new TestRunnable(10));
th.setPriority(Thread.currentThread().getPriority()-1);
System.out.println("Starting thread " + i);
th.start();
}
catch(EmptyStackException e){
System.out.println("Timeout on thread retrieval from throttle on thread: " + i);
}
}
System.out.println("joining with 1 sec wait per thread.");
try {
throttle.joinAll(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("join finished.");
}
private static class TestRunnable implements Runnable{
private static int count = 0;
private int maxRunning;
TestRunnable(int max){
maxRunning = max;
}
public void run(){
try {
synchronized(TestRunnable.class){
System.out.println("Thread started.");
System.out.flush();
}
Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
fail("Test runnable should not be interrupted.");
}
synchronized(TestRunnable.class){
System.out.println("Thread finished.");
System.out.flush();
}
}
}
}
|