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.SynchronizedInt; 8 9 import com.tc.net.TCSocketAddress; 10 import com.tc.net.protocol.NullProtocolAdaptor; 11 import com.tc.net.protocol.ProtocolAdaptorFactory; 12 import com.tc.net.protocol.TCProtocolAdaptor; 13 14 import java.util.Random ; 15 16 import junit.framework.TestCase; 17 18 23 public class ConnectionCreateTest extends TestCase { 24 25 public void testConnectionCreate() throws Exception { 26 final Random random = new Random (); 27 final TCConnectionManager clientConnMgr; 28 final TCConnectionManager serverConnMgr; 29 final TCSocketAddress addr; 30 clientConnMgr = new TCConnectionManagerFactory().getInstance(); 31 serverConnMgr = new TCConnectionManagerFactory().getInstance(); 32 33 TCListener lsnr = serverConnMgr.createListener(new TCSocketAddress(0), new ProtocolAdaptorFactory() { 34 public TCProtocolAdaptor getInstance() { 35 return new NullProtocolAdaptor(); 36 } 37 }); 38 39 addr = lsnr.getBindSocketAddress(); 40 41 final int numClients = 100; 42 final int numThreads = 5; 43 final Object STOP = new Object (); 44 final Object work = new Object (); 45 final SynchronizedInt failures = new SynchronizedInt(0); 46 final LinkedQueue queue = new LinkedQueue(); 47 48 class ConnectTask implements Runnable { 49 public void run() { 50 while (true) { 51 try { 52 Object o = queue.take(); 53 if (o == STOP) { return; } 54 TCConnection conn = clientConnMgr.createConnection(new NullProtocolAdaptor()); 55 conn.connect(addr, 3000); 56 Thread.sleep(random.nextInt(100)); 57 conn.close(3000); 58 return; 59 } catch (Throwable t) { 60 t.printStackTrace(); 61 failures.increment(); 62 } 63 64 } 65 } 66 } 67 68 Thread [] threads = new Thread [numThreads]; 69 for (int i = 0; i < threads.length; i++) { 70 threads[i] = new Thread (new ConnectTask(), "Connect thread " + i); 71 threads[i].start(); 72 } 73 74 for (int i = 0; i < numClients; i++) { 75 queue.put(work); 76 } 77 78 for (int i = 0; i < threads.length; i++) { 79 queue.put(STOP); 80 } 81 82 for (int i = 0; i < threads.length; i++) { 83 threads[i].join(); 84 } 85 86 int errors = failures.get(); 87 assertTrue("Failure count = " + errors, errors == 0); 88 } 89 90 } | Popular Tags |