1 3 package org.jgroups.tests; 4 5 6 import org.jgroups.Channel; 7 import org.jgroups.Message; 8 import org.jgroups.MessageListener; 9 import org.jgroups.View; 10 import org.jgroups.blocks.PullPushAdapter; 11 import org.jgroups.util.Promise; 12 import org.jgroups.util.Util; 13 14 15 18 public class ConnectTest extends ChannelTestBase { 19 Channel channel; 20 final int TIMES=10; 21 22 public ConnectTest(String name) { 23 super(name); 24 } 25 26 public void tearDown() throws Exception { 27 if(channel != null) { 28 channel.close(); 29 channel = null; 30 } 31 super.tearDown(); 32 } 33 34 void doIt(int times) { 35 for(int i=0; i < times; i++) { 36 System.out.println("\nAttempt #" + (i + 1)); 37 System.out.print("Connecting to channel: "); 38 try { 39 channel.connect("ConnectTest"); 40 System.out.println("-- connected: " + channel.getView() + " --"); 41 } 42 catch(Exception e) { 43 System.out.println("-- connection failed --"); 44 System.err.println(e); 45 } 46 System.out.print("Disconnecting from channel: "); 47 channel.disconnect(); 48 System.out.println("-- disconnected --"); 49 } 50 } 51 52 53 public void testConnectAndDisconnect() throws Exception { 54 System.out.print("Creating channel: "); 55 channel=createChannel(); 56 System.out.println("-- created --"); 57 doIt(TIMES); 58 System.out.print("Closing channel: "); 59 channel.close(); 60 System.out.println("-- closed --"); 61 System.out.println("Remaining threads are:"); 62 Util.printThreads(); 63 } 64 65 public void testDisconnectConnectOne() throws Exception { 66 channel=createChannel(); 67 channel.connect("testgroup1"); 68 channel.disconnect(); 69 channel.connect("testgroup2"); 70 View view=channel.getView(); 71 assertEquals(1, view.size()); 72 assertTrue(view.containsMember(channel.getLocalAddress())); 73 channel.close(); 74 System.out.println("Remaining threads are:"); 75 Util.printThreads(); 76 } 77 78 79 82 public void testDisconnectConnectTwo() throws Exception { 83 View view; 84 Channel coordinator=createChannel("A"); 85 coordinator.connect("testgroup"); 86 view=coordinator.getView(); 87 System.out.println("-- view for coordinator: " + view); 88 89 channel=createChannel("A"); 90 channel.connect("testgroup1"); 91 view=channel.getView(); 92 System.out.println("-- view for channel: " + view); 93 94 channel.disconnect(); 95 96 channel.connect("testgroup"); 97 view=channel.getView(); 98 System.out.println("-- view for channel: " + view); 99 100 assertEquals(2, view.size()); 101 assertTrue(view.containsMember(channel.getLocalAddress())); 102 assertTrue(view.containsMember(coordinator.getLocalAddress())); 103 coordinator.close(); 104 channel.close(); 105 System.out.println("Remaining threads are:"); 106 Util.printThreads(); 107 } 108 109 110 117 public void testDisconnectConnectSendTwo() throws Exception { 118 final Promise msgPromise=new Promise(); 119 Channel coordinator=createChannel("A"); 120 coordinator.connect("testgroup"); 121 PullPushAdapter ppa= 122 new PullPushAdapter(coordinator, 123 new PromisedMessageListener(msgPromise)); 124 ppa.start(); 125 126 channel=createChannel("A"); 127 channel.connect("testgroup1"); 128 channel.disconnect(); 129 channel.connect("testgroup"); 130 channel.send(new Message(null, null, "payload")); 131 Message msg=(Message)msgPromise.getResult(20000); 132 assertTrue(msg != null); 133 assertEquals("payload", msg.getObject()); 134 ppa.stop(); 135 coordinator.close(); 136 channel.close(); 137 System.out.println("Remaining threads are:"); 138 Util.printThreads(); 139 } 140 141 142 143 144 145 146 147 private static class PromisedMessageListener implements MessageListener { 148 149 private Promise promise; 150 151 public PromisedMessageListener(Promise promise) { 152 this.promise=promise; 153 } 154 155 public byte[] getState() { 156 return null; 157 } 158 159 public void receive(Message msg) { 160 promise.setResult(msg); 161 } 162 163 public void setState(byte[] state) { 164 } 165 } 166 167 168 public static void main(String [] args) { 169 String [] testCaseName={ConnectTest.class.getName()}; 170 junit.textui.TestRunner.main(testCaseName); 171 } 172 173 174 } 175 | Popular Tags |