001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.net.core;
006:
007: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
008: import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
009: import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef;
010:
011: import com.tc.net.TCSocketAddress;
012: import com.tc.net.protocol.EchoSink;
013: import com.tc.net.proxy.TCPProxy;
014: import com.tc.test.TCTestCase;
015:
016: import java.io.File;
017:
018: public class SimpleServerTest extends TCTestCase {
019: private final boolean useProxy = false;
020: private TCPProxy proxy = null;
021: private int proxyPort = -1;
022: private TCConnectionManager connMgr;
023: private SimpleServer server;
024: private final SynchronizedRef error = new SynchronizedRef(null);
025:
026: protected void setUp() throws Exception {
027: connMgr = new TCConnectionManagerJDK14();
028: server = new SimpleServer(new EchoSink(true,
029: new EchoSink.ErrorListener() {
030: public void error(Throwable t) {
031: setError(t);
032: }
033: }));
034: server.start();
035:
036: if (useProxy) {
037: int serverPort = server.getServerAddr().getPort();
038: proxyPort = serverPort + 1;
039: proxy = new TCPProxy(proxyPort, server.getServerAddr()
040: .getAddress(), serverPort, 0, true, new File(System
041: .getProperty("java.io.tmpdir")));
042: proxy.start();
043: }
044: }
045:
046: private void setError(Throwable t) {
047: t.printStackTrace();
048: error.set(t);
049: }
050:
051: protected void tearDown() throws Exception {
052: if (error.get() != null) {
053: fail(error.get().toString());
054: }
055: if (proxy != null) {
056: proxy.stop();
057: }
058: connMgr.shutdown();
059: server.stop();
060: }
061:
062: public void testLargeMessages() throws Exception {
063: System.out.println("LARGE MESSAGES");
064: runMultiClient(15, 5, 256, 2, 100, 150);
065: }
066:
067: public void testSmallMessages() throws Exception {
068: // these messages only take one buffer
069: System.out.println("SMALLEST MESSAGES");
070: runMultiClient(250, 20, 0, 100, 3, 5);
071: }
072:
073: public void testKindaSmallMessages() throws Exception {
074: // these messages span at least two byte buffers
075: System.out.println("SMALL MESSAGES");
076: runMultiClient(75, 10, 1, 100, 3, 7);
077: }
078:
079: private void runMultiClient(int numClients, int maxConcurrent,
080: final int dataSize, final int numToSend,
081: final int minDelay, final int maxDelay) throws Exception {
082: long start = System.currentTimeMillis();
083:
084: try {
085: final int numConcurrent = Math.min(maxConcurrent,
086: numClients);
087: PooledExecutor pool = new PooledExecutor(new LinkedQueue(),
088: numConcurrent);
089: pool.setKeepAliveTime(1000);
090:
091: final TCSocketAddress addr;
092: if (proxy != null) {
093: addr = new TCSocketAddress(proxyPort);
094: } else {
095: addr = new TCSocketAddress(server.getServerAddr()
096: .getPort());
097: }
098:
099: for (int i = 0; i < numClients; i++) {
100: pool.execute(new ClientTask(i, new VerifierClient(
101: connMgr, addr, dataSize, numToSend, minDelay,
102: maxDelay)));
103: }
104:
105: pool.shutdownAfterProcessingCurrentlyQueuedTasks();
106: pool.awaitTerminationAfterShutdown();
107: } finally {
108: System.err.println("Took "
109: + (System.currentTimeMillis() - start)
110: + " millis for test: " + getName());
111: }
112: }
113:
114: private class ClientTask implements Runnable {
115: private final VerifierClient client;
116: private final int num;
117:
118: ClientTask(int num, VerifierClient client) {
119: this .num = num;
120: this .client = client;
121: }
122:
123: public void run() {
124: try {
125: client.run();
126: } catch (Throwable t) {
127: setError(t);
128: } finally {
129: System.err.println(System.currentTimeMillis()
130: + ": client " + num + " finished");
131: }
132: }
133:
134: }
135:
136: }
|