KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > Deadlock2Test


1 // $Id: Deadlock2Test.java,v 1.12 2007/07/02 15:48:41 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import java.util.Enumeration JavaDoc;
6 import java.util.Vector JavaDoc;
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 /**
19  * Test the distributed RPC deadlock detection mechanism, using one or two channels.
20  *
21  * @author John Giorgiadis
22  * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
23  * *
24  * @version $Revision: 1.12 $
25  */

26 public class Deadlock2Test extends ChannelTestBase {
27
28     private static boolean DEADLOCK_DETECTION = true;
29
30     private String JavaDoc name = "Deadlock2Test";
31
32
33     public Deadlock2Test(String JavaDoc name) {
34         super(name);
35     }
36
37
38     /**
39      * Tests the deadlock resolution using self-calls on a single channel. The deadlock detection
40      * is turned on so the method call should go straight through. If there is a problem, JUnit will
41      * timeout.
42      *
43      * @throws Exception
44      */

45     public void testOneChannel() throws Exception JavaDoc {
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         // call the nested group method on itself
54
MethodCall call = new MethodCall("outerMethod", new Object JavaDoc[0], new Class JavaDoc[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     /**
71      * Tests the deadlock resolution using two different channels. The deadlock detection
72      * is turned on. It implements the following scenario:
73      *
74      * Channel1 Channel2
75      * | |
76      * + -------------------------------> outerMethod()
77      * | RPC
78      * | |
79      * | |
80      * | |
81      * | <-- innerMethod() <-----------------+ ---------+
82      * | | |
83      * | | <-- innerMethod()
84      *
85      * If there is a deadlock, JUnit will timeout and fail the test.
86      *
87      */

88     public void testTwoChannels() throws Throwable JavaDoc {
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             // call a point-to-point method on Member 2 that triggers a nested distributed RPC
106
MethodCall call = new MethodCall("outerMethod", new Object JavaDoc[0], new Class JavaDoc[0]);
107             log("calling outerMethod() on " + localAddress2);
108             Object JavaDoc 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 JavaDoc {
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 JavaDoc<Address> dests=new Vector JavaDoc<Address>();
134         dests.add(c1.getLocalAddress());
135         dests.add(c2.getLocalAddress());
136
137         try {
138             // call a point-to-point method on Member 2 that triggers a nested distributed RPC
139
MethodCall call = new MethodCall("outerMethod", new Object JavaDoc[0], new Class JavaDoc[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 JavaDoc[] args) {
157         junit.textui.TestRunner.run(suite());
158         System.exit(0);
159     }
160
161
162     static void log(String JavaDoc msg) {
163         System.out.println("[" + Thread.currentThread() + "] " + msg);
164     }
165
166
167     public static class ServerObject {
168         String JavaDoc myName;
169
170         public ServerObject(String JavaDoc name) {
171             this.myName=name;
172         }
173
174         private RpcDispatcher disp;
175
176         /**
177         * The ServerObject keeps a reference to its dispatcher to be able to send nested group
178         * RPCs.
179         */

180         public void setRpcDispatcher(RpcDispatcher rpcDispatcher) {
181             this.disp = rpcDispatcher;
182         }
183
184         public String JavaDoc outerMethod() {
185             log("**** outerMethod() received, calling innerMethod() on all members");
186             MethodCall call = new MethodCall("innerMethod", new Object JavaDoc[0], new Class JavaDoc[0]);
187             // RspList rspList = disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 5000);
188
RspList rspList = disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0);
189             Vector JavaDoc results = rspList.getResults();
190             log("results of calling innerMethod():\n" + rspList);
191             StringBuilder JavaDoc sb=new StringBuilder JavaDoc("outerMethod[");
192             for(Enumeration JavaDoc e = results.elements(); e.hasMoreElements(); ) {
193                 String JavaDoc s = (String JavaDoc)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 JavaDoc innerMethod() {
204             log("**** innerMethod() received, returning result");
205             return "innerMethod";
206         }
207     }
208
209
210
211 }
212
Popular Tags