001: package org.jgroups.tests;
002:
003: import junit.framework.TestCase;
004: import org.jgroups.JChannel;
005: import org.jgroups.ReceiverAdapter;
006: import org.jgroups.View;
007: import org.jgroups.util.Util;
008: import org.jgroups.stack.GossipRouter;
009:
010: /**
011: * Tests merging
012: * @author Bela Ban
013: * @version $Id: MergeTest.java,v 1.9 2006/10/04 12:15:37 belaban Exp $
014: */
015: public class MergeTest extends TestCase {
016: JChannel channel;
017: final int TIMES = 10;
018: final int router_port = 12000;
019: final String bind_addr = "127.0.0.1";
020: GossipRouter router;
021: JChannel ch1, ch2;
022: private ViewChecker checker;
023:
024: String props = "TUNNEL(router_port="
025: + router_port
026: + ";router_host="
027: + bind_addr
028: + ";loopback=true):"
029: + "PING(timeout=1000;num_initial_members=2;gossip_host="
030: + bind_addr
031: + ";gossip_port="
032: + router_port
033: + "):"
034: + "MERGE2(min_interval=3000;max_interval=5000):"
035: + "FD(timeout=1000;max_tries=2;shun=false):"
036: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):"
037: + "UNICAST(timeout=600,1200,2400):"
038: + "pbcast.STABLE(desired_avg_gossip=20000):"
039: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
040: + "print_local_addr=false;shun=false)";
041:
042: public MergeTest(String name) {
043: super (name);
044: }
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: startRouter();
049: checker = new ViewChecker();
050: ch1 = new JChannel(props);
051: ch1.setReceiver(checker);
052: ch1.connect("demo");
053: ch2 = new JChannel(props);
054: ch2.setReceiver(checker);
055: ch2.connect("demo");
056: Util.sleep(1000);
057: }
058:
059: public void tearDown() throws Exception {
060: super .tearDown();
061: ch2.close();
062: ch1.close();
063: stopRouter();
064: }
065:
066: public void testPartitionAndSubsequentMerge() throws Exception {
067: partitionAndMerge();
068: }
069:
070: public void testTwoMerges() throws Exception {
071: partitionAndMerge();
072: partitionAndMerge();
073: }
074:
075: private void partitionAndMerge() throws Exception {
076: View v = ch2.getView();
077: System.out.println("view is " + v);
078: assertEquals("channel is supposed to have 2 members", 2, ch2
079: .getView().size());
080:
081: System.out
082: .println("++ simulating network partition by stopping the GossipRouter");
083: stopRouter();
084:
085: System.out.println("sleeping for 10 secs");
086: // Util.sleep(10000);
087: checker.waitForNViews(2, 10000);
088:
089: v = ch1.getView();
090: System.out.println("-- ch1.view: " + v);
091:
092: v = ch2.getView();
093: System.out.println("-- ch2.view: " + v);
094: assertEquals(
095: "view should be 1 (channels should have excluded each other",
096: 1, v.size());
097:
098: System.out
099: .println("++ simulating merge by starting the GossipRouter again");
100: startRouter();
101:
102: System.out.println("sleeping for 30 secs");
103: // Util.sleep(30000);
104: checker.waitForNViews(2, 30000);
105:
106: v = ch1.getView();
107: System.out.println("-- ch1.view: " + v);
108:
109: v = ch2.getView();
110: System.out.println("-- ch2.view: " + v);
111:
112: assertEquals(
113: "channel is supposed to have 2 members again after merge",
114: 2, ch2.getView().size());
115: }
116:
117: private void startRouter() throws Exception {
118: router = new GossipRouter(router_port, bind_addr);
119: router.start();
120: }
121:
122: private void stopRouter() {
123: router.stop();
124: }
125:
126: private static class ViewChecker extends ReceiverAdapter {
127: final Object mutex = new Object();
128: int count = 0;
129:
130: public void viewAccepted(View new_view) {
131: synchronized (mutex) {
132: count++;
133: System.out.println("-- view: " + new_view);
134: mutex.notifyAll();
135: }
136: }
137:
138: public void waitForNViews(int n, long timeout) {
139: long sleep_time = timeout, curr, start;
140: synchronized (mutex) {
141: count = 0;
142: start = System.currentTimeMillis();
143: while (count < n) {
144: try {
145: mutex.wait(sleep_time);
146: } catch (InterruptedException e) {
147: }
148: curr = System.currentTimeMillis();
149: sleep_time -= (curr - start);
150: if (sleep_time <= 0)
151: break;
152: }
153: }
154: }
155: }
156:
157: public static void main(String[] args) {
158: String[] testCaseName = { MergeTest.class.getName() };
159: junit.textui.TestRunner.main(testCaseName);
160: }
161:
162: }
|