1 3 package org.jgroups.tests; 4 5 import java.util.Enumeration ; 6 import java.util.Vector ; 7 8 import junit.framework.Test; 9 import junit.framework.TestSuite; 10 11 import org.jgroups.Address; 12 import org.jgroups.Channel; 13 import org.jgroups.blocks.GroupRequest; 14 import org.jgroups.blocks.MethodCall; 15 import org.jgroups.blocks.RpcDispatcher; 16 import org.jgroups.util.RspList; 17 18 26 public class Deadlock2Test extends ChannelTestBase { 27 28 private static boolean DEADLOCK_DETECTION = true; 29 30 private String name = "Deadlock2Test"; 31 32 33 public Deadlock2Test(String name) { 34 super(name); 35 } 36 37 38 45 public void testOneChannel() throws Exception { 46 Channel channel = createChannel(); 47 ServerObject serverObject = new ServerObject("obj1"); 48 RpcDispatcher disp=new RpcDispatcher(channel, null, null, serverObject, DEADLOCK_DETECTION); 49 serverObject.setRpcDispatcher(disp); 50 channel.connect(name); 51 Address localAddress = channel.getLocalAddress(); 52 53 MethodCall call = new MethodCall("outerMethod", new Object [0], new Class [0]); 55 log("calling outerMethod() on all members"); 56 RspList rspList = disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0); 57 log("results of outerMethod(): " + rspList); 58 59 assertEquals(1, rspList.size()); 60 assertEquals("outerMethod[innerMethod]", rspList.getValue(localAddress)); 61 assertTrue(rspList.isReceived(localAddress)); 62 assertFalse(rspList.isSuspected(localAddress)); 63 64 channel.disconnect(); 65 channel.close(); 66 67 } 68 69 70 88 public void testTwoChannels() throws Throwable { 89 ServerObject obj1, obj2 = null; 90 91 Channel c1 = createChannel("A"); 92 obj1 = new ServerObject("obj1"); 93 RpcDispatcher disp1=new RpcDispatcher(c1, null, null, obj1, DEADLOCK_DETECTION); 94 obj1.setRpcDispatcher(disp1); 95 c1.connect(name); 96 97 Channel c2 = createChannel("A"); 98 obj2 = new ServerObject("obj2"); 99 RpcDispatcher disp2=new RpcDispatcher(c2, null, null, obj2, DEADLOCK_DETECTION); 100 obj2.setRpcDispatcher(disp2); 101 c2.connect(name); 102 Address localAddress2 = c2.getLocalAddress(); 103 104 try { 105 MethodCall call = new MethodCall("outerMethod", new Object [0], new Class [0]); 107 log("calling outerMethod() on " + localAddress2); 108 Object retval = disp1.callRemoteMethod(localAddress2, call, GroupRequest.GET_ALL, 0); 109 log("results of outerMethod(): " + retval); 110 } 111 finally { 112 c2.close(); 113 c1.close(); 114 } 115 } 116 117 118 public void testTwoChannelsWithInitialMulticast() throws Exception { 119 ServerObject obj1, obj2 = null; 120 121 Channel c1 = createChannel("A"); 122 obj1 = new ServerObject("obj1"); 123 RpcDispatcher disp1=new RpcDispatcher(c1, null, null, obj1, DEADLOCK_DETECTION); 124 obj1.setRpcDispatcher(disp1); 125 c1.connect(name); 126 127 Channel c2 = createChannel("A"); 128 obj2 = new ServerObject("obj2"); 129 RpcDispatcher disp2=new RpcDispatcher(c2, null, null, obj2, DEADLOCK_DETECTION); 130 obj2.setRpcDispatcher(disp2); 131 c2.connect(name); 132 133 Vector <Address> dests=new Vector <Address>(); 134 dests.add(c1.getLocalAddress()); 135 dests.add(c2.getLocalAddress()); 136 137 try { 138 MethodCall call = new MethodCall("outerMethod", new Object [0], new Class [0]); 140 log("calling outerMethod() on all members"); 141 RspList rsps = disp1.callRemoteMethods(dests, call, GroupRequest.GET_ALL, 0); 142 log("results of outerMethod():\n" + rsps); 143 assertEquals(2, rsps.size()); 144 } 145 finally { 146 c2.close(); 147 c1.close(); 148 } 149 } 150 151 152 public static Test suite() { 153 return new TestSuite(Deadlock2Test.class); 154 } 155 156 public static void main(String [] args) { 157 junit.textui.TestRunner.run(suite()); 158 System.exit(0); 159 } 160 161 162 static void log(String msg) { 163 System.out.println("[" + Thread.currentThread() + "] " + msg); 164 } 165 166 167 public static class ServerObject { 168 String myName; 169 170 public ServerObject(String name) { 171 this.myName=name; 172 } 173 174 private RpcDispatcher disp; 175 176 180 public void setRpcDispatcher(RpcDispatcher rpcDispatcher) { 181 this.disp = rpcDispatcher; 182 } 183 184 public String outerMethod() { 185 log("**** outerMethod() received, calling innerMethod() on all members"); 186 MethodCall call = new MethodCall("innerMethod", new Object [0], new Class [0]); 187 RspList rspList = disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0); 189 Vector results = rspList.getResults(); 190 log("results of calling innerMethod():\n" + rspList); 191 StringBuilder sb=new StringBuilder ("outerMethod["); 192 for(Enumeration e = results.elements(); e.hasMoreElements(); ) { 193 String s = (String )e.nextElement(); 194 sb.append(s); 195 if (e.hasMoreElements()) { 196 sb.append(";"); 197 } 198 } 199 sb.append("]"); 200 return sb.toString(); 201 } 202 203 public static String innerMethod() { 204 log("**** innerMethod() received, returning result"); 205 return "innerMethod"; 206 } 207 } 208 209 210 211 } 212 | Popular Tags |