001: package org.jgroups.blocks;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.Address;
007: import org.jgroups.stack.IpAddress;
008: import org.jgroups.util.Util;
009:
010: import java.net.InetAddress;
011: import java.net.UnknownHostException;
012:
013: /**
014: * Tests ConnectionTable
015: * @author Bela Ban
016: * @version $Id: ConnectionTableTest.java,v 1.2 2006/09/15 12:06:43 belaban Exp $
017: */
018: public class ConnectionTableTest extends TestCase {
019: private BasicConnectionTable ct1, ct2;
020: static InetAddress loopback_addr = null;
021: static byte[] data = new byte[] { 'b', 'e', 'l', 'a' };
022: Address addr1, addr2;
023: int active_threads = 0;
024:
025: static {
026: try {
027: loopback_addr = InetAddress.getByName("127.0.0.1");
028: } catch (UnknownHostException e) {
029: e.printStackTrace();
030: }
031: }
032:
033: public ConnectionTableTest(String testName) {
034: super (testName);
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: active_threads = Thread.activeCount();
040: System.out.println("active threads before (" + active_threads
041: + "):\n" + Util.activeThreads());
042: addr1 = new IpAddress(loopback_addr, 7500);
043: addr2 = new IpAddress(loopback_addr, 8000);
044: }
045:
046: protected void tearDown() throws Exception {
047: if (ct2 != null) {
048: ct2.stop();
049: ct2 = null;
050: }
051: if (ct1 != null) {
052: ct1.stop();
053: ct1 = null;
054: }
055: super .tearDown();
056: }
057:
058: public void testStopConnectionTable() throws Exception {
059: ct1 = new ConnectionTable(new DummyReceiver(), loopback_addr,
060: null, 7500, 7000, 60000, 120000);
061: ct2 = new ConnectionTable(new DummyReceiver(), loopback_addr,
062: null, 8000, 8000, 60000, 120000);
063: _testStop(ct1, ct2);
064: }
065:
066: public void testStopConnectionTableNIO() throws Exception {
067: ct1 = new ConnectionTableNIO(new DummyReceiver(),
068: loopback_addr, null, 7500, 7500, 60000, 120000, false);
069: ct2 = new ConnectionTableNIO(new DummyReceiver(),
070: loopback_addr, null, 8000, 8000, 60000, 120000, false);
071: ct1.start();
072: ct2.start();
073: _testStop(ct1, ct2);
074: }
075:
076: private void _testStop(BasicConnectionTable table1,
077: BasicConnectionTable table2) throws Exception {
078: table1.send(addr1, data, 0, data.length); // send to self
079: table1.send(addr2, data, 0, data.length); // send to other
080:
081: table2.send(addr2, data, 0, data.length); // send to self
082: table2.send(addr1, data, 0, data.length); // send to other
083:
084: assertEquals(2, table1.getNumConnections());
085: assertEquals(2, table2.getNumConnections());
086:
087: table2.stop();
088: table1.stop();
089: assertEquals(0, table1.getNumConnections());
090: assertEquals(0, table2.getNumConnections());
091:
092: // Util.sleep(1000);
093:
094: int current_active_threads = Thread.activeCount();
095: System.out.println("active threads after ("
096: + current_active_threads + "):\n"
097: + Util.activeThreads());
098: // System.out.println("thread:\n" + dumpThreads());
099: assertEquals(active_threads, current_active_threads);
100: }
101:
102: /* CAUTION: JDK 5 specific code
103:
104:
105: private String dumpThreads() {
106: StringBuffer sb=new StringBuffer();
107: ThreadMXBean bean=ManagementFactory.getThreadMXBean();
108: long[] ids=bean.getAllThreadIds();
109: ThreadInfo[] threads=bean.getThreadInfo(ids, 20);
110: for(int i=0; i < threads.length; i++) {
111: ThreadInfo info=threads[i];
112: if(info == null)
113: continue;
114: sb.append(info.getThreadName()).append(":\n");
115: StackTraceElement[] stack_trace=info.getStackTrace();
116: for(int j=0; j < stack_trace.length; j++) {
117: StackTraceElement el=stack_trace[j];
118: sb.append("at ").append(el.getClassName()).append(".").append(el.getMethodName());
119: sb.append("(").append(el.getFileName()).append(":").append(el.getLineNumber()).append(")");
120: sb.append("\n");
121: }
122: sb.append("\n\n");
123: }
124: return sb.toString();
125: }
126: */
127:
128: public static Test suite() {
129: return new TestSuite(ConnectionTableTest.class);
130: }
131:
132: public static void main(String[] args) {
133: junit.textui.TestRunner.run(ConnectionTableTest.suite());
134: }
135:
136: static class DummyReceiver implements ConnectionTable.Receiver {
137: public void receive(Address sender, byte[] data, int offset,
138: int length) {
139: }
140: }
141:
142: static class DummyReceiverNIO implements
143: ConnectionTableNIO.Receiver {
144: public void receive(Address sender, byte[] data, int offset,
145: int length) {
146: }
147: }
148:
149: }
|