KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > RpcDispatcherInterruptTest


1 package org.jgroups.blocks;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.jgroups.Channel;
6 import org.jgroups.tests.ChannelTestBase;
7 import org.jgroups.util.Rsp;
8 import org.jgroups.util.RspList;
9 import org.jgroups.util.Util;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * Tests interruption of a blocked call with the timeout and a thread pool
16  * @author Bela Ban
17  * @version $Id: RpcDispatcherInterruptTest.java,v 1.1 2007/06/18 08:42:16 belaban Exp $
18  */

19 public class RpcDispatcherInterruptTest extends ChannelTestBase {
20     RpcDispatcher disp, disp2;
21     Channel ch, ch2;
22
23     public void setUp() throws Exception JavaDoc {
24         super.setUp();
25
26         ch=createChannel("A");
27         ServerObject obj=new ServerObject();
28         disp=new RpcDispatcher(ch, null, null, obj);
29         ch.connect("demo");
30
31         ch2=createChannel("A");
32         ServerObject obj2=new ServerObject();
33         disp2=new RpcDispatcher(ch2, null, null, obj2);
34         ch2.connect("demo");
35     }
36
37     public void tearDown() throws Exception JavaDoc {
38         super.tearDown();
39         ch2.close();
40         disp2.stop();
41         ch.close();
42         disp.stop();
43     }
44
45
46     public void testMethodCallWithTimeoutNoInterrupt() {
47         long timeout, block_time;
48         RspList rsps;
49
50         timeout=0;
51         block_time=0;
52         rsps=call(timeout, block_time);
53         checkResults(rsps, 2, true);
54
55         timeout=0;
56         block_time=1000L;
57         rsps=call(timeout, block_time);
58         checkResults(rsps, 2, true);
59
60         timeout=1000;
61         block_time=0L;
62         rsps=call(timeout, block_time);
63         checkResults(rsps, 2, true);
64
65         timeout=1000;
66         block_time=10000L;
67         rsps=call(timeout, block_time);
68         checkResults(rsps, 2, false);
69     }
70
71
72
73     private RspList call(long timeout, long block_time) {
74         long start, stop, diff;
75         System.out.println("calling with timeout=" + timeout + ", block_time=" + block_time);
76         start=System.currentTimeMillis();
77         RspList retval=disp.callRemoteMethods(null, "foo", new Object JavaDoc[]{block_time}, new Class JavaDoc[]{long.class}, GroupRequest.GET_ALL, timeout);
78         stop=System.currentTimeMillis();
79         diff=stop-start;
80         System.out.println("rsps (in " + diff + "ms:)\n" + retval);
81         return retval;
82     }
83
84     private void checkResults(RspList rsps, int num, boolean received) {
85         assertEquals("responses: " + rsps, num, rsps.size());
86         Map.Entry JavaDoc entry;
87         for(Iterator JavaDoc it=rsps.entrySet().iterator(); it.hasNext();) {
88             entry=(Map.Entry JavaDoc)it.next();
89             Rsp rsp=(Rsp)entry.getValue();
90             assertEquals("rsp: " + rsp, rsp.wasReceived(), received);
91         }
92     }
93
94
95     static class ServerObject {
96
97         public void foo(long timeout) {
98             System.out.println("-- received foo(), blocking for " + timeout + " ms");
99             Util.sleep(timeout);
100             System.out.println("-- returning");
101         }
102     }
103
104
105     public static Test suite() {
106         return new TestSuite(RpcDispatcherAnycastTest.class);
107     }
108
109
110     public static void main(String JavaDoc[] args) {
111         junit.textui.TestRunner.run(RpcDispatcherAnycastTest.suite());
112     }
113 }
Popular Tags