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.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
008:
009: import com.tc.cluster.Cluster;
010: import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
011: import com.tc.config.schema.setup.L2TVSConfigurationSetupManager;
012: import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
013: import com.tc.exception.TCRuntimeException;
014: import com.tc.lang.TCThreadGroup;
015: import com.tc.lang.ThrowableHandler;
016: import com.tc.logging.TCLogging;
017: import com.tc.net.protocol.transport.ConnectionPolicy;
018: import com.tc.net.protocol.transport.ConnectionPolicyImpl;
019: import com.tc.object.BaseDSOTestCase;
020: import com.tc.object.DistributedObjectClient;
021: import com.tc.object.bytecode.MockClassProvider;
022: import com.tc.object.bytecode.NullManager;
023: import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
024: import com.tc.object.config.DSOClientConfigHelper;
025: import com.tc.object.config.StandardDSOClientConfigHelperImpl;
026: import com.tc.server.TCServerImpl;
027: import com.tc.util.concurrent.ThreadUtil;
028:
029: /**
030: * Test to make sure that the maximum number of connected DSO clients may be limited. This isn't a transparency test,
031: * but it is a system test which is why it's in the com.tctest package. It could, perhaps, be moved somewhere more
032: * appropriate.
033: */
034: public class MaxConnectionTest extends BaseDSOTestCase {
035:
036: private TCServerImpl server;
037:
038: private DistributedObjectClient newClient() throws Exception {
039: L1TVSConfigurationSetupManager manager = super
040: .createL1ConfigManager();
041: DSOClientConfigHelper configHelper = new StandardDSOClientConfigHelperImpl(
042: manager);
043:
044: PreparedComponentsFromL2Connection components = new PreparedComponentsFromL2Connection(
045: manager);
046: return new DistributedObjectClient(configHelper,
047: new TCThreadGroup(new ThrowableHandler(TCLogging
048: .getLogger(DistributedObjectClient.class))),
049: new MockClassProvider(), components, NullManager
050: .getInstance(), new Cluster());
051: }
052:
053: public void testsMaxConnectionLimitAndClientDisconnectAccounting()
054: throws Exception {
055:
056: ConnectionPolicy connectionPolicy = new ConnectionPolicyImpl(2);
057: TestTVSConfigurationSetupManagerFactory factory = createDistributedConfigFactory();
058: L2TVSConfigurationSetupManager l2Manager = factory
059: .createL2TVSConfigurationSetupManager(null);
060: server = new TCServerImpl(l2Manager, new TCThreadGroup(
061: new ThrowableHandler(TCLogging
062: .getLogger(TCServerImpl.class))),
063: connectionPolicy);
064: server.start();
065:
066: makeClientUsePort(server.getDSOListenPort());
067:
068: DistributedObjectClient client1 = newClient();
069:
070: client1.start();
071: newClient().start();
072:
073: final boolean[] done = new boolean[] { false };
074:
075: new Thread() {
076: public void run() {
077: try {
078: newClient().start();
079: if (!done[0])
080: fail("Expected a MaxConnectionsExceededException");
081: else
082: System.out
083: .println("proceeded now that someone was killed");
084: } catch (Exception e) {
085: throw new AssertionError("oops");
086: }
087: }
088: }.start();// with the maximum number of clients connected, make sure that a new client can't connect.
089:
090: // disconnect one of the clients
091: LinkedQueue stopQueue = new LinkedQueue();
092: long timeout = 5 * 1000;
093: ThreadUtil.reallySleep(3000);
094: done[0] = true;
095: new Thread(new ClientStopper(stopQueue, client1)).start();
096: if (stopQueue.poll(timeout) == null) {
097: fail("Client failed to stop within timeout: " + timeout
098: + " ms.");
099: }
100:
101: }
102:
103: private static final class ClientStopper implements Runnable {
104: private final LinkedQueue queue;
105: private final DistributedObjectClient myClient;
106:
107: private ClientStopper(LinkedQueue queue,
108: DistributedObjectClient client) {
109: this .queue = queue;
110: this .myClient = client;
111: }
112:
113: public void run() {
114: myClient.stop();
115: try {
116: queue.put(new Object());
117: } catch (InterruptedException e) {
118: throw new TCRuntimeException(e);
119: }
120: }
121: }
122:
123: public void tearDown() throws Exception {
124: if (server != null) {
125: server.stop();
126: }
127: }
128: }
|