001: package org.jgroups.blocks;
002:
003: import org.jgroups.tests.ChannelTestBase;
004:
005: public class RpcDispatcherAnycastMultipleCallsTest extends
006: ChannelTestBase {
007: private RpcDispatcherAnycastServerObject[] targets = null;
008:
009: public void tearDown() throws Exception {
010: if (targets != null) {
011: for (int i = 0; i < targets.length; i++) {
012: if (targets[i] != null)
013: targets[i].shutdown();
014: targets[i] = null;
015: }
016: targets = null;
017: }
018: super .tearDown();
019: }
020:
021: public void test2InstancesAnycastIncludeSelf() throws Exception {
022: performTest(true, 2, false);
023: }
024:
025: public void test3InstancesAnycastIncludeSelf() throws Exception {
026: performTest(true, 3, false);
027: }
028:
029: public void test2InstancesMcastIncludeSelf() throws Exception {
030: performTest(false, 2, false);
031: }
032:
033: public void test3InstancesMcastIncludeSelf() throws Exception {
034: performTest(false, 3, false);
035: }
036:
037: public void test2InstancesAnycastExcludeSelf() throws Exception {
038: performTest(true, 2, true);
039: }
040:
041: public void test3InstancesAnycastExcludeSelf() throws Exception {
042: performTest(true, 3, true);
043: }
044:
045: public void test2InstancesMcastExcludeSelf() throws Exception {
046: performTest(false, 2, true);
047: }
048:
049: public void test3InstancesMcastExcludeSelf() throws Exception {
050: performTest(false, 3, true);
051: }
052:
053: /**
054: * Simple test that creates n instances of {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject}, each one creates a Channel
055: * and registers an RpcDispatcher.
056: *
057: * This test then calls {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject#callRemote(boolean, boolean)} once on each of the n instances
058: * and then tests that the method calls have in fact been executed on each of the n instances.
059: *
060: * @param useAnycast if true, anycast is used for remote calls.
061: * @param n number of instances
062: * @param excludeSelf whether or not to exclude self in rpc calls
063: */
064: private void performTest(boolean useAnycast, int n,
065: boolean excludeSelf) throws Exception {
066: // create n instances
067: targets = new RpcDispatcherAnycastServerObject[n];
068: for (int i = 0; i < n; i++) {
069: targets[i] = new RpcDispatcherAnycastServerObject(
070: createChannel("A"));
071: }
072:
073: // test that the target method has been invoked 0 times on each instance.
074: for (int i = 0; i < n; i++)
075: assertEquals(0, targets[i].i);
076:
077: // if we don't exclude self, the state of all instances should be identical.
078: int value = 0;
079:
080: // if we are excluding self, we need an array of expected values.
081: int[] expectedValues = null;
082:
083: if (excludeSelf) {
084: expectedValues = new int[n];
085: for (int i = 0; i < n; i++)
086: expectedValues[i] = 0;
087: }
088:
089: for (int instances = 0; instances < n; instances++) {
090: targets[instances].callRemote(useAnycast, excludeSelf);
091:
092: // the assertions and how we measure test success is different depending on whether we exclude self or not.
093:
094: if (excludeSelf) {
095: for (int i = 0; i < n; i++) {
096: if (i != instances)
097: expectedValues[i]++;
098: }
099: for (int i = 0; i < n; i++)
100: assertEquals(
101: "Failure when invoking call on instance "
102: + instances
103: + ". Did not reach instance " + i
104: + ".", expectedValues[i],
105: targets[i].i);
106: } else {
107: value++;
108: for (int i = 0; i < n; i++)
109: assertEquals(
110: "Failure when invoking call on instance "
111: + instances
112: + ". Did not reach instance " + i
113: + ".", value, targets[i].i);
114: }
115: }
116:
117: }
118: }
|