KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > ConnectionCreateTest


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

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 JavaDoc;
15
16 import junit.framework.TestCase;
17
18 /**
19  * TODO: Document me
20  *
21  * @author teck
22  */

23 public class ConnectionCreateTest extends TestCase {
24
25   public void testConnectionCreate() throws Exception JavaDoc {
26     final Random JavaDoc random = new Random JavaDoc();
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 JavaDoc STOP = new Object JavaDoc();
44     final Object JavaDoc work = new Object JavaDoc();
45     final SynchronizedInt failures = new SynchronizedInt(0);
46     final LinkedQueue queue = new LinkedQueue();
47
48     class ConnectTask implements Runnable JavaDoc {
49       public void run() {
50         while (true) {
51           try {
52             Object JavaDoc 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 JavaDoc t) {
60             t.printStackTrace();
61             failures.increment();
62           }
63
64         }
65       }
66     }
67
68     Thread JavaDoc[] threads = new Thread JavaDoc[numThreads];
69     for (int i = 0; i < threads.length; i++) {
70       threads[i] = new Thread JavaDoc(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