1 4 package com.tc.net.core; 5 6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 7 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef; 9 10 import com.tc.net.TCSocketAddress; 11 import com.tc.net.protocol.EchoSink; 12 import com.tc.net.proxy.TCPProxy; 13 import com.tc.test.TCTestCase; 14 15 import java.io.File ; 16 17 public class SimpleServerTest extends TCTestCase { 18 private final boolean useProxy = false; 19 private TCPProxy proxy = null; 20 private int proxyPort = -1; 21 private TCConnectionManager connMgr; 22 private SimpleServer server; 23 private final SynchronizedRef error = new SynchronizedRef(null); 24 25 protected void setUp() throws Exception { 26 connMgr = new TCConnectionManagerFactory().getInstance(); 27 server = new SimpleServer(new EchoSink(true, new EchoSink.ErrorListener() { 28 public void error(Throwable t) { 29 setError(t); 30 } 31 })); 32 server.start(); 33 34 if (useProxy) { 35 int serverPort = server.getServerAddr().getPort(); 36 proxyPort = serverPort + 1; 37 proxy = new TCPProxy(proxyPort, server.getServerAddr().getAddress(), serverPort, 0, true, new File (System 38 .getProperty("java.io.tmpdir"))); 39 proxy.start(); 40 } 41 } 42 43 private void setError(Throwable t) { 44 t.printStackTrace(); 45 error.set(t); 46 } 47 48 protected void tearDown() throws Exception { 49 if (error.get() != null) { 50 fail(error.get().toString()); 51 } 52 if (proxy != null) { 53 proxy.stop(); 54 } 55 connMgr.shutdown(); 56 server.stop(); 57 } 58 59 public void testLargeMessages() throws Exception { 60 System.out.println("LARGE MESSAGES"); 61 runMultiClient(15, 5, 256, 2, 100, 150); 62 } 63 64 public void testSmallMessages() throws Exception { 65 System.out.println("SMALLEST MESSAGES"); 67 runMultiClient(250, 20, 0, 100, 3, 5); 68 } 69 70 public void testKindaSmallMessages() throws Exception { 71 System.out.println("SMALL MESSAGES"); 73 runMultiClient(75, 10, 1, 100, 3, 7); 74 } 75 76 private void runMultiClient(int numClients, int maxConcurrent, final int dataSize, final int numToSend, 77 final int minDelay, final int maxDelay) throws Exception { 78 long start = System.currentTimeMillis(); 79 80 try { 81 final int numConcurrent = Math.min(maxConcurrent, numClients); 82 PooledExecutor pool = new PooledExecutor(new LinkedQueue(), numConcurrent); 83 pool.setKeepAliveTime(1000); 84 85 final TCSocketAddress addr; 86 if (proxy != null) { 87 addr = new TCSocketAddress(proxyPort); 88 } else { 89 addr = new TCSocketAddress(server.getServerAddr().getPort()); 90 } 91 92 for (int i = 0; i < numClients; i++) { 93 pool.execute(new ClientTask(i, new VerifierClient(connMgr, addr, dataSize, numToSend, minDelay, maxDelay))); 94 } 95 96 pool.shutdownAfterProcessingCurrentlyQueuedTasks(); 97 pool.awaitTerminationAfterShutdown(); 98 } finally { 99 System.err.println("Took " + (System.currentTimeMillis() - start) + " millis for test: " + getName()); 100 } 101 } 102 103 private class ClientTask implements Runnable { 104 private final VerifierClient client; 105 private final int num; 106 107 ClientTask(int num, VerifierClient client) { 108 this.num = num; 109 this.client = client; 110 } 111 112 public void run() { 113 try { 114 client.run(); 115 } catch (Throwable t) { 116 setError(t); 117 } finally { 118 System.err.println(System.currentTimeMillis() + ": client " + num + " finished"); 119 } 120 } 121 122 } 123 124 } | Popular Tags |