1 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 35 public class MaxConnectionTest extends BaseDSOTestCase { 36 37 private TCServerImpl server; 38 39 private DistributedObjectClient newClient() throws Exception { 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 { 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 () { 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 e) { 74 throw new AssertionError ("oops"); 75 } 76 } 77 }.start(); 79 LinkedQueue stopQueue = new LinkedQueue(); 81 long timeout = 5 * 1000; 82 ThreadUtil.reallySleep(3000); 83 done[0] = true; 84 new Thread (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 { 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 ()); 104 } catch (InterruptedException e) { 105 throw new TCRuntimeException(e); 106 } 107 } 108 } 109 110 public void tearDown() throws Exception { 111 if (server != null) { 112 server.stop(); 113 } 114 } 115 } 116 | Popular Tags |