File: RpcDispatcherAnycastMultipleCallsTest.java

package info (click to toggle)
libjgroups-java 2.12.2.Final-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,712 kB
  • sloc: java: 109,098; xml: 9,423; sh: 149; makefile: 2
file content (123 lines) | stat: -rw-r--r-- 4,405 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.jgroups.blocks;

import org.jgroups.Global;
import org.jgroups.JChannel;
import org.jgroups.tests.ChannelTestBase;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test(groups=Global.STACK_DEPENDENT, sequential=true)
public class RpcDispatcherAnycastMultipleCallsTest extends ChannelTestBase {
    private RpcDispatcherAnycastServerObject[] targets=null;
    static final int NUM=3;

    @BeforeClass
    public void init() throws Exception {
        targets=new RpcDispatcherAnycastServerObject[NUM];
        final String GROUP="RpcDispatcherAnycastMultipleCallsTest";
        JChannel first_channel=null;
        for(int i=0; i < NUM; i++) {
            JChannel c=first_channel == null? createChannel(true,NUM) : createChannel(first_channel);
            if(first_channel == null){
                first_channel=c;
            }
            targets[i]=new RpcDispatcherAnycastServerObject(c);
            c.connect(GROUP);
        }
    }

    @AfterMethod
    protected void tearDown() throws Exception {
        if(targets != null) {
            for(int i=0; i < targets.length; i++) {
                if(targets[i] != null) targets[i].i=0;
            }
        }
    }

    @AfterClass
    protected void shutdown() throws Exception {
        if(targets != null) {
            for(int i=0; i < targets.length; i++) {
                if(targets[i] != null) targets[i].shutdown();
                targets[i]=null;
            }
            targets=null;
        }
    }

    public void test2InstancesAnycastIncludeSelf() throws Exception {
        performTest(true, 2, false);
    }

    public void test3InstancesAnycastIncludeSelf() throws Exception {
        performTest(true, 3, false);
    }

    public void test2InstancesMcastIncludeSelf() throws Exception {
        performTest(false, 2, false);
    }

    public void test3InstancesMcastIncludeSelf() throws Exception {
        performTest(false, 3, false);
    }

    public void test2InstancesAnycastExcludeSelf() throws Exception {
        performTest(true, 2, true);
    }

    public void test3InstancesAnycastExcludeSelf() throws Exception {
        performTest(true, 3, true);
    }



    /**
     * Simple test that creates n instances of {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject}, each one creates a Channel
     * and registers an RpcDispatcher.
     * <p/>
     * This test then calls {@link org.jgroups.blocks.RpcDispatcherAnycastServerObject#callRemote(boolean, boolean)} once on each of the n instances
     * and then tests that the method calls have in fact been executed on each of the n instances.
     * @param useAnycast  if true, anycast is used for remote calls.
     * @param n           number of instances
     * @param excludeSelf whether or not  to exclude self in rpc calls
     */
    private void performTest(boolean useAnycast, int n, boolean excludeSelf) throws Exception {

        // test that the target method has been invoked 0 times on each instance.
        for(int i=0; i < n; i++) assertEquals(0, targets[i].i);

        // if we don't exclude self, the state of all instances should be identical.
        int value=0;

        // if we are excluding self, we need an array of expected values.
        int[] expectedValues=null;

        if(excludeSelf) {
            expectedValues=new int[n];
            for(int i=0; i < n; i++) expectedValues[i]=0;
        }

        for(int instances=0; instances < n; instances++) {
            targets[instances].callRemote(useAnycast, excludeSelf);

            // the assertions and how we measure test success is different depending on whether we exclude self or not.

            if(excludeSelf) {
                for(int i=0; i < n; i++) {
                    if(i != instances) expectedValues[i]++;
                }
                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);
            }
            else {
                value++;
                for(int i=0; i < n; i++)
                    assertEquals("Failure when invoking call on instance " + instances + ".  Did not reach instance " + i + ".", value, targets[i].i);
            }
        }

    }
}