KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > MaxConnectionTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
8
9 import com.tc.cluster.Cluster;
10 import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
11 import com.tc.config.schema.setup.L2TVSConfigurationSetupManager;
12 import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
13 import com.tc.exception.TCRuntimeException;
14 import com.tc.lang.TCThreadGroup;
15 import com.tc.lang.ThrowableHandler;
16 import com.tc.logging.TCLogging;
17 import com.tc.net.protocol.transport.ConnectionPolicy;
18 import com.tc.net.protocol.transport.ConnectionPolicyImpl;
19 import com.tc.object.BaseDSOTestCase;
20 import com.tc.object.DistributedObjectClient;
21 import com.tc.object.bytecode.MockClassProvider;
22 import com.tc.object.bytecode.NullManager;
23 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
24 import com.tc.object.config.DSOClientConfigHelper;
25 import com.tc.object.config.StandardDSOClientConfigHelper;
26 import com.tc.objectserver.impl.DistributedObjectServer;
27 import com.tc.server.TCServerImpl;
28 import com.tc.util.concurrent.ThreadUtil;
29
30 /**
31  * Test to make sure that the maximum number of connected DSO clients may be limited. This isn't a transparency test,
32  * but it is a system test which is why it's in the com.tctest package. It could, perhaps, be moved somewhere more
33  * appropriate.
34  */

35 public class MaxConnectionTest extends BaseDSOTestCase {
36
37   private TCServerImpl server;
38
39   private DistributedObjectClient newClient() throws Exception JavaDoc {
40     L1TVSConfigurationSetupManager manager = super.createL1ConfigManager();
41     DSOClientConfigHelper configHelper = new StandardDSOClientConfigHelper(manager);
42
43     PreparedComponentsFromL2Connection components = new PreparedComponentsFromL2Connection(manager);
44     return new DistributedObjectClient(configHelper, new TCThreadGroup(new ThrowableHandler(TCLogging
45         .getLogger(DistributedObjectClient.class))), new MockClassProvider(), components, NullManager.getInstance(),
46                                        new Cluster());
47   }
48
49   public void testsMaxConnectionLimitAndClientDisconnectAccounting() throws Exception JavaDoc {
50
51     ConnectionPolicy connectionPolicy = new ConnectionPolicyImpl(2);
52     TestTVSConfigurationSetupManagerFactory factory = createDistributedConfigFactory();
53     L2TVSConfigurationSetupManager l2Manager = factory.createL2TVSConfigurationSetupManager(null);
54     server = new TCServerImpl(l2Manager, new TCThreadGroup(new ThrowableHandler(TCLogging
55         .getLogger(DistributedObjectServer.class))), connectionPolicy);
56     server.start();
57
58     makeClientUsePort(server.getDSOListenPort());
59
60     DistributedObjectClient client1 = newClient();
61
62     client1.start();
63     newClient().start();
64
65     final boolean[] done = new boolean[] { false };
66
67     new Thread JavaDoc() {
68       public void run() {
69         try {
70           newClient().start();
71           if (!done[0]) fail("Expected a MaxConnectionsExceededException");
72           else System.out.println("proceeded now that someone was killed");
73         } catch (Exception JavaDoc e) {
74           throw new AssertionError JavaDoc("oops");
75         }
76       }
77     }.start();// with the maximum number of clients connected, make sure that a new client can't connect.
78

79     // disconnect one of the clients
80
LinkedQueue stopQueue = new LinkedQueue();
81     long timeout = 5 * 1000;
82     ThreadUtil.reallySleep(3000);
83     done[0] = true;
84     new Thread JavaDoc(new ClientStopper(stopQueue, client1)).start();
85     if (stopQueue.poll(timeout) == null) {
86       fail("Client failed to stop within timeout: " + timeout + " ms.");
87     }
88
89   }
90
91   private static final class ClientStopper implements Runnable JavaDoc {
92     private final LinkedQueue queue;
93     private final DistributedObjectClient myClient;
94
95     private ClientStopper(LinkedQueue queue, DistributedObjectClient client) {
96       this.queue = queue;
97       this.myClient = client;
98     }
99
100     public void run() {
101       myClient.stop();
102       try {
103         queue.put(new Object JavaDoc());
104       } catch (InterruptedException JavaDoc e) {
105         throw new TCRuntimeException(e);
106       }
107     }
108   }
109
110   public void tearDown() throws Exception JavaDoc {
111     if (server != null) {
112       server.stop();
113     }
114   }
115 }
116
Popular Tags