KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.blocks;
2
3 import org.jgroups.tests.ChannelTestBase;
4
5 public class RpcDispatcherAnycastMultipleCallsTest extends ChannelTestBase
6 {
7    private RpcDispatcherAnycastServerObject[] targets = null;
8
9    protected void tearDown() throws Exception JavaDoc {
10        if(targets != null) {
11            for(int i=0; i < targets.length; i++) {
12                if(targets[i] != null) targets[i].shutdown();
13                targets[i]=null;
14            }
15            targets=null;
16        }
17        super.tearDown();
18    }
19
20    public void test2InstancesAnycastIncludeSelf() throws Exception JavaDoc
21    {
22       performTest(true, 2, false);
23    }
24
25    public void test3InstancesAnycastIncludeSelf() throws Exception JavaDoc
26    {
27       performTest(true, 3, false);
28    }
29
30    public void test2InstancesMcastIncludeSelf() throws Exception JavaDoc
31    {
32       performTest(false, 2, false);
33    }
34
35    public void test3InstancesMcastIncludeSelf() throws Exception JavaDoc
36    {
37       performTest(false, 3, false);
38    }
39
40    public void test2InstancesAnycastExcludeSelf() throws Exception JavaDoc
41    {
42       performTest(true, 2, true);
43    }
44
45    public void test3InstancesAnycastExcludeSelf() throws Exception JavaDoc
46    {
47       performTest(true, 3, true);
48    }
49
50    public void test2InstancesMcastExcludeSelf() throws Exception JavaDoc
51    {
52       performTest(false, 2, true);
53    }
54
55    public void test3InstancesMcastExcludeSelf() throws Exception JavaDoc
56    {
57       performTest(false, 3, true);
58    }
59    
60
61    /**
62     * Simple test that creates n instances of {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject}, each one creates a Channel
63     * and registers an RpcDispatcher.
64     *
65     * This test then calls {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject#callRemote(boolean, boolean)} once on each of the n instances
66     * and then tests that the method calls have in fact been executed on each of the n instances.
67     *
68     * @param useAnycast if true, anycast is used for remote calls.
69     * @param n number of instances
70     * @param excludeSelf whether or not to exclude self in rpc calls
71     */

72    private void performTest(boolean useAnycast, int n, boolean excludeSelf) throws Exception JavaDoc
73    {
74       // create n instances
75
targets = new RpcDispatcherAnycastServerObject[n];
76       for (int i=0; i<n; i++){
77          targets[i] = new RpcDispatcherAnycastServerObject(createChannel("A"));
78       }
79
80       // test that the target method has been invoked 0 times on each instance.
81
for (int i=0; i<n; i++) assertEquals(0, targets[i].i);
82
83       // if we don't exclude self, the state of all instances should be identical.
84
int value = 0;
85
86       // if we are excluding self, we need an array of expected values.
87
int[] expectedValues = null;
88
89       if (excludeSelf)
90       {
91          expectedValues = new int[n];
92          for (int i=0; i<n; i++) expectedValues[i] = 0;
93       }
94
95       for (int instances = 0; instances < n; instances++)
96       {
97          targets[instances].callRemote(useAnycast, excludeSelf);
98
99          // the assertions and how we measure test success is different depending on whether we exclude self or not.
100

101          if (excludeSelf)
102          {
103             for (int i=0; i<n; i++)
104             {
105                if (i != instances) expectedValues[i]++;
106             }
107             for (int i=0; i<n; i++) assertEquals("Failure when invoking call on instance " + instances + ". Did not reach instance " + i + ".", expectedValues[i], targets[i].i);
108          }
109          else
110          {
111             value++;
112             for (int i=0; i<n; i++) assertEquals("Failure when invoking call on instance " + instances + ". Did not reach instance " + i + ".", value, targets[i].i);
113          }
114       }
115
116    }
117 }
118
Popular Tags