01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.net.core;
06:
07: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
08: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
09:
10: import com.tc.net.TCSocketAddress;
11: import com.tc.net.protocol.NullProtocolAdaptor;
12: import com.tc.net.protocol.ProtocolAdaptorFactory;
13: import com.tc.net.protocol.TCProtocolAdaptor;
14:
15: import java.util.Random;
16:
17: import junit.framework.TestCase;
18:
19: /**
20: * TODO: Document me
21: *
22: * @author teck
23: */
24: public class ConnectionCreateTest extends TestCase {
25:
26: public void testConnectionCreate() throws Exception {
27: final Random random = new Random();
28: final TCConnectionManager clientConnMgr;
29: final TCConnectionManager serverConnMgr;
30: final TCSocketAddress addr;
31: clientConnMgr = new TCConnectionManagerJDK14();
32: serverConnMgr = new TCConnectionManagerJDK14();
33:
34: TCListener lsnr = serverConnMgr.createListener(
35: new TCSocketAddress(0), new ProtocolAdaptorFactory() {
36: public TCProtocolAdaptor getInstance() {
37: return new NullProtocolAdaptor();
38: }
39: });
40:
41: addr = lsnr.getBindSocketAddress();
42:
43: final int numClients = 100;
44: final int numThreads = 5;
45: final Object STOP = new Object();
46: final Object work = new Object();
47: final SynchronizedInt failures = new SynchronizedInt(0);
48: final LinkedQueue queue = new LinkedQueue();
49:
50: class ConnectTask implements Runnable {
51: public void run() {
52: while (true) {
53: try {
54: Object o = queue.take();
55: if (o == STOP) {
56: return;
57: }
58: TCConnection conn = clientConnMgr
59: .createConnection(new NullProtocolAdaptor());
60: conn.connect(addr, 3000);
61: Thread.sleep(random.nextInt(100));
62: conn.close(3000);
63: return;
64: } catch (Throwable t) {
65: t.printStackTrace();
66: failures.increment();
67: }
68:
69: }
70: }
71: }
72:
73: Thread[] threads = new Thread[numThreads];
74: for (int i = 0; i < threads.length; i++) {
75: threads[i] = new Thread(new ConnectTask(),
76: "Connect thread " + i);
77: threads[i].start();
78: }
79:
80: for (int i = 0; i < numClients; i++) {
81: queue.put(work);
82: }
83:
84: for (int i = 0; i < threads.length; i++) {
85: queue.put(STOP);
86: }
87:
88: for (int i = 0; i < threads.length; i++) {
89: threads[i].join();
90: }
91:
92: int errors = failures.get();
93: assertTrue("Failure count = " + errors, errors == 0);
94: }
95:
96: }
|