001: // $Id: DeadlockTest.java,v 1.7 2006/02/28 16:32:11 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import org.jgroups.*;
006: import org.jgroups.blocks.GroupRequest;
007: import org.jgroups.blocks.MethodCall;
008: import org.jgroups.blocks.RpcDispatcher;
009:
010: /**
011: * Class which tests deadlock detection in RequestCorrelator.
012: * @author John Giorgiadis
013: */
014: public class DeadlockTest {
015: private static final Class[] TYPES = new Class[] {};
016: private static final Object[] ARGS = new Object[] {};
017:
018: public class InRpc {
019: public void rpc_1() {
020: _in_rpc_1();
021: }
022:
023: public void rpc_2() {
024: _in_rpc_2();
025: }
026: }
027:
028: private static class Handler implements MessageListener,
029: MembershipListener {
030: public Handler() {
031: super ();
032: }
033:
034: // MessageListener
035: public byte[] getState() {
036: return (null);
037: }
038:
039: public void setState(byte[] state) {
040: }
041:
042: public void receive(Message msg) {
043: }
044:
045: // MembershipListener
046: public void block() {
047: }
048:
049: public void suspect(Address suspect) {
050: }
051:
052: public void viewAccepted(View view) {
053: }
054: }
055:
056: // .......................................................................
057:
058: private String name = "JG";
059: private String stack = null; // default stack config
060: private JChannel channel;
061: private RpcDispatcher disp;
062:
063: private void _in_rpc_1() {
064: System.out.println("In rpc_1()");
065: cast_call("rpc_2", ARGS, TYPES);
066: System.out.println("Exiting rpc_1()");
067: }
068:
069: private void _in_rpc_2() {
070: System.out.println("In rpc_2()");
071: System.out.println("Exiting rpc_2()");
072: }
073:
074: private void cast_call(String method, Object[] args, Class[] types) {
075: MethodCall call;
076: call = new MethodCall(method, args, types);
077: disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0);
078: }
079:
080: // .......................................................................
081:
082: public DeadlockTest(boolean use_deadlock_detection) {
083: Handler handler = new Handler();
084: InRpc in_rpc = new InRpc();
085:
086: try {
087: channel = new JChannel(stack);
088: disp = new RpcDispatcher(channel, handler, handler, in_rpc,
089: use_deadlock_detection);
090: channel.connect(name);
091: } catch (ChannelClosedException ex) {
092: ex.printStackTrace();
093: } catch (ChannelException ex) {
094: ex.printStackTrace();
095: }
096:
097: // Call rpc_1 which in turn calls rpc_2
098: System.out.println("Calling rpc_1()");
099: if (!use_deadlock_detection)
100: System.out
101: .println("** Not using deadlock detection -- recursive call will hang !");
102: else
103: System.out
104: .println("** Using deadlock detection -- recursive call will succeed");
105: cast_call("rpc_1", ARGS, TYPES);
106: System.out.println("Out of rpc_1()");
107: channel.disconnect();
108: channel.close();
109: System.out.println("Disconnected");
110: }
111:
112: // .......................................................................
113:
114: public static void main(String[] args) {
115: if (args.length != 1) {
116: System.out
117: .println("DeadlockTest <true|false (use_deadlock_detection)>");
118: return;
119: }
120: new DeadlockTest(Boolean.valueOf(args[0]).booleanValue());
121: }
122: }
|