import junit.framework.*; import java.io.*; import java.net.*; //same as ThreadedServer but checks to make sure too many threads aren't running public class BoundedThreadedServerTestCase extends TestCase { private class SlowServerCaller extends Thread { private String sendString; private StringBuffer output; public SlowServerCaller(String sendString,StringBuffer output) { this.sendString = sendString; this.output = output; } private void copySrcToBuf(Reader src, StringBuffer out) throws java.io.IOException { char[] tempBuf = new char[1024]; int charsRead = 1; do { charsRead = src.read(tempBuf, 0, 1024); if (charsRead > 0) out.append(tempBuf, 0, charsRead); } while (charsRead >= 0); } public void run() { try { URL url = new URL(sendString); URLConnection urlConnection = url.openConnection(); Reader in = new InputStreamReader(urlConnection.getInputStream()); copySrcToBuf(in,output); in.close(); } catch (Exception e) { } } } private class RunServer extends Thread { BoundedThreadedServer server; RunServer(BoundedThreadedServer server) { this.server = server; } public void run() { server.run(); } } public BoundedThreadedServerTestCase(String name) { super(name); } public void test1() throws Exception //similar to one of the RemoteGet....TestCase tests { Process process = Runtime.getRuntime().exec("java -Dport=10000 SlowServer"); Thread.sleep(500); BoundedThreadedServer server = new BoundedThreadedServer(23381,5); server.attach(new RemoteGetFileSyncHandler(HandlerOrder.NO_PREFERENCE)); Thread serverThread = new RunServer(server); serverThread.start(); Thread.sleep(500); java.util.Vector threads = new java.util.Vector(); String [] speeds = { "fast","snail","turtle","slug","turtle","turtle","fast","slug","fast" }; final int numIter = 20; java.util.Vector outputs = new java.util.Vector(); for( int i = 0; i < numIter; i++ ) { String speed = speeds[i % speeds.length]; String request = "http://localhost:23381/remoteGetFile/localhost:10000/" + speed; outputs.addElement(new StringBuffer()); Thread t = new SlowServerCaller(request,(StringBuffer)outputs.elementAt(i)); threads.addElement(t); t.start(); } boolean threadRun = true; while( threadRun ) { threadRun = false; for( int i = 0; i < threads.size(); i++ ) if( ((Thread)threads.elementAt(i)).isAlive() ) threadRun = true; //i don't say <= 5 because there may be several other threads running (such as the thread we're in) assertTrue(Thread.activeCount() < 10); Thread.yield(); } serverThread.destroy(); process.destroy(); for( int i = 0; i < numIter; i++ ) { String speed = speeds[i % speeds.length]; String expectedString = "(" + speed + ")"; assertTrue(((StringBuffer)outputs.elementAt(i)).toString().equals(expectedString)); } } public void test2() throws Exception { Process process = Runtime.getRuntime().exec("java -Dport=10000 SlowServer"); Thread.sleep(500); BoundedThreadedServer server = new BoundedThreadedServer(23381,5); java.util.HashMap sessionMap = new java.util.HashMap(); java.util.HashMap logMap = new java.util.HashMap(); server.attach(new StartSessionSyncHandler(sessionMap,HandlerOrder.NO_PREFERENCE)); server.attach(new EndSessionSyncHandler(sessionMap,logMap,HandlerOrder.NO_PREFERENCE)); server.attach(new RemoteGetFileSyncHandler(HandlerOrder.NO_PREFERENCE)); Thread serverThread = new RunServer(server); serverThread.start(); Thread.sleep(500); java.util.Vector threads = new java.util.Vector(); String [] speeds = { "fast","snail","turtle","slug","turtle","turtle","fast","slug","fast" }; final int numIter = 20; java.util.Vector outputs = new java.util.Vector(); StringBuffer junk = new StringBuffer(); new SlowServerCaller("http://localhost:23381/startSession/sessionname",junk).run(); for( int i = 0; i < numIter; i++ ) { String speed = speeds[i % speeds.length]; String request = "http://localhost:23381/remoteGetFile/localhost:10000/" + speed; outputs.addElement(new StringBuffer()); Thread t = new SlowServerCaller(request,(StringBuffer)outputs.elementAt(i)); threads.addElement(t); t.start(); } boolean threadRun = true; while( threadRun ) { threadRun = false; for( int i = 0; i < threads.size(); i++ ) if( ((Thread)threads.elementAt(i)).isAlive() ) threadRun = true; //i don't say <= 5 because there may be several other threads running (such as the thread we're in) assertTrue(Thread.activeCount() < 10); Thread.yield(); } new SlowServerCaller("http://localhost:23381/endSession/sessionname/logname",junk).run(); StringBuffer output = new StringBuffer(); new SlowServerCaller("http://localhost:23381/summary/logname",output).run(); serverThread.destroy(); process.destroy(); assertTrue(output.toString().indexOf("Session Times") >= 0); } public static Test suite() { return new TestSuite(ThreadedServerTestCase.class); } public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }