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 ; 11 import java.net.UnknownHostException ; 12 import java.util.concurrent.BlockingQueue ; 13 import java.util.concurrent.LinkedBlockingQueue ; 14 15 16 21 public class ConnectionTableTest extends TestCase { 22 private BasicConnectionTable ct1, ct2; 23 static InetAddress 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 e) { 33 e.printStackTrace(); 34 } 35 } 36 37 38 public ConnectionTableTest(String testName) { 39 super(testName); 40 } 41 42 43 protected void setUp() throws Exception { 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 { 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 queue=new LinkedBlockingQueue (); 67 68 Thread taker=new Thread () { 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 e) { 77 } 78 } 79 }; 80 taker.start(); 81 82 Util.sleep(500); 83 84 queue.clear(); Util.interruptAndWaitToDie(taker); 86 assertFalse("taker: " + taker, taker.isAlive()); 87 } 88 89 90 public void testStopConnectionTableNoSendQueues() throws Exception { 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 { 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 { 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 { 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 { 126 table1.send(addr1, data, 0, data.length); assertEquals(0, table1.getNumConnections()); table1.send(addr2, data, 0, data.length); 130 table2.send(addr2, data, 0, data.length); table2.send(addr1, data, 0, data.length); 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 [] 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 |