KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > ConnectionTableTest


1 package org.jgroups.blocks;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jgroups.Address;
7 import org.jgroups.stack.IpAddress;
8 import org.jgroups.util.Util;
9
10 import java.net.InetAddress JavaDoc;
11 import java.net.UnknownHostException JavaDoc;
12 import java.util.concurrent.BlockingQueue JavaDoc;
13 import java.util.concurrent.LinkedBlockingQueue JavaDoc;
14
15
16 /**
17  * Tests ConnectionTable
18  * @author Bela Ban
19  * @version $Id: ConnectionTableTest.java,v 1.1 2007/07/04 07:29:37 belaban Exp $
20  */

21 public class ConnectionTableTest extends TestCase {
22     private BasicConnectionTable ct1, ct2;
23     static InetAddress JavaDoc loopback_addr=null;
24     static byte[] data=new byte[]{'b', 'e', 'l', 'a'};
25     Address addr1, addr2;
26     int active_threads=0;
27
28     static {
29         try {
30             loopback_addr=InetAddress.getByName("127.0.0.1");
31         }
32         catch(UnknownHostException JavaDoc e) {
33             e.printStackTrace();
34         }
35     }
36
37
38     public ConnectionTableTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42
43     protected void setUp() throws Exception JavaDoc {
44         super.setUp();
45         active_threads=Thread.activeCount();
46         System.out.println("active threads before (" + active_threads + "):\n" + Util.activeThreads());
47         addr1=new IpAddress(loopback_addr, 7500);
48         addr2=new IpAddress(loopback_addr, 8000);
49     }
50
51
52     protected void tearDown() throws Exception JavaDoc {
53         if(ct2 != null) {
54             ct2.stop();
55             ct2=null;
56         }
57         if(ct1 != null) {
58             ct1.stop();
59             ct1=null;
60         }
61         super.tearDown();
62     }
63
64
65     public void testBlockingQueue() {
66         final BlockingQueue JavaDoc queue=new LinkedBlockingQueue JavaDoc();
67
68         Thread JavaDoc taker=new Thread JavaDoc() {
69
70             public void run() {
71                 try {
72                     System.out.println("taking an element from the queue");
73                     queue.take();
74                     System.out.println("clear");
75                 }
76                 catch(InterruptedException JavaDoc e) {
77                 }
78             }
79         };
80         taker.start();
81
82         Util.sleep(500);
83
84         queue.clear(); // does this release the taker thread ?
85
Util.interruptAndWaitToDie(taker);
86         assertFalse("taker: " + taker, taker.isAlive());
87     }
88
89
90     public void testStopConnectionTableNoSendQueues() throws Exception JavaDoc {
91         ct1=new ConnectionTable(new DummyReceiver(), loopback_addr, null, 7500, 7500, 60000, 120000);
92         ct1.setUseSendQueues(false);
93         ct2=new ConnectionTable(new DummyReceiver(), loopback_addr, null, 8000, 8000, 60000, 120000);
94         ct2.setUseSendQueues(false);
95         _testStop(ct1, ct2);
96     }
97
98     public void testStopConnectionTableWithSendQueues() throws Exception JavaDoc {
99         ct1=new ConnectionTable(new DummyReceiver(), loopback_addr, null, 7500, 7500, 60000, 120000);
100         ct2=new ConnectionTable(new DummyReceiver(), loopback_addr, null, 8000, 8000, 60000, 120000);
101         _testStop(ct1, ct2);
102     }
103
104
105     public void testStopConnectionTableNIONoSendQueues() throws Exception JavaDoc {
106         ct1=new ConnectionTableNIO(new DummyReceiver(), loopback_addr, null, 7500, 7500, 60000, 120000, false);
107         ct1.setUseSendQueues(false);
108         ct2=new ConnectionTableNIO(new DummyReceiver(), loopback_addr, null, 8000, 8000, 60000, 120000, false);
109         ct2.setUseSendQueues(false);
110         ct1.start();
111         ct2.start();
112         _testStop(ct1, ct2);
113     }
114
115
116     public void testStopConnectionTableNIOWithSendQueues() throws Exception JavaDoc {
117         ct1=new ConnectionTableNIO(new DummyReceiver(), loopback_addr, null, 7500, 7500, 60000, 120000, false);
118         ct2=new ConnectionTableNIO(new DummyReceiver(), loopback_addr, null, 8000, 8000, 60000, 120000, false);
119         ct1.start();
120         ct2.start();
121         _testStop(ct1, ct2);
122     }
123
124
125     private void _testStop(BasicConnectionTable table1, BasicConnectionTable table2) throws Exception JavaDoc {
126         table1.send(addr1, data, 0, data.length); // send to self
127
assertEquals(0, table1.getNumConnections()); // sending to self should not create a connection
128
table1.send(addr2, data, 0, data.length); // send to other
129

130         table2.send(addr2, data, 0, data.length); // send to self
131
table2.send(addr1, data, 0, data.length); // send to other
132

133
134         System.out.println("table1:\n" + table1 + "\ntable2:\n" + table2);
135
136         assertEquals(1, table1.getNumConnections());
137         assertEquals(1, table2.getNumConnections());
138
139         table2.stop();
140         table1.stop();
141         assertEquals(0, table1.getNumConnections());
142         assertEquals(0, table2.getNumConnections());
143         int current_active_threads=Thread.activeCount();
144         System.out.println("active threads after (" + current_active_threads + "):\n" + Util.activeThreads());
145         assertEquals("threads:\n" + Util.dumpThreads(), active_threads, current_active_threads);
146     }
147
148
149
150     public static Test suite() {
151         return new TestSuite(ConnectionTableTest.class);
152     }
153
154
155     public static void main(String JavaDoc[] args) {
156         junit.textui.TestRunner.run(ConnectionTableTest.suite());
157     }
158
159     static class DummyReceiver implements ConnectionTable.Receiver {
160         public void receive(Address sender, byte[] data, int offset, int length) {
161             System.out.println("-- received " + length + " bytes from " + sender);
162         }
163     }
164
165     static class DummyReceiverNIO implements ConnectionTableNIO.Receiver {
166         public void receive(Address sender, byte[] data, int offset, int length) {
167             System.out.println("-- received " + length + " bytes from " + sender);
168         }
169     }
170
171 }
172
Popular Tags