001: package org.jgroups.tests;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.*;
007: import org.jgroups.util.Util;
008:
009: import java.util.List;
010: import java.util.LinkedList;
011:
012: /**
013: * @author Bela Ban
014: * @version $Id: JoinTest.java,v 1.2 2006/10/25 12:23:55 belaban Exp $
015: */
016: public class JoinTest extends TestCase {
017: Channel c1, c2;
018: static String STACK = "udp.xml";
019:
020: public JoinTest(String name) {
021: super (name);
022: }
023:
024: public void setUp() throws Exception {
025: super .setUp();
026: STACK = System.getProperty("stack", STACK);
027: c1 = new JChannel(STACK);
028: c2 = new JChannel(STACK);
029: }
030:
031: protected void tearDown() throws Exception {
032: super .tearDown();
033: if (c1 != null)
034: c1.close();
035: if (c2 != null)
036: c2.close();
037: }
038:
039: public void testSingleJoin() throws ChannelException {
040: c1.connect("X");
041: View v = c1.getView();
042: assertNotNull(v);
043: assertEquals(1, v.size());
044: }
045:
046: /**
047: * Tests that immediately after a connect(), a getView() returns the correct view
048: * @throws ChannelException
049: */
050: public void testJoinsOnTwoChannels() throws ChannelException {
051: c1.connect("X");
052: c2.connect("X");
053: View v1 = c1.getView(), v2 = c2.getView();
054: System.out.println("v1=" + v1 + ", v2=" + v2);
055: assertNotNull(v1);
056: assertNotNull(v2);
057: assertEquals(2, v1.size());
058: assertEquals(2, v2.size());
059: assertEquals(v1, v2);
060: }
061:
062: public void testJoinsOnTwoChannelsAndSend() throws ChannelException {
063: MyReceiver r1 = new MyReceiver("c1");
064: MyReceiver r2 = new MyReceiver("c2");
065: c1.setReceiver(r1);
066: c2.setReceiver(r2);
067: Message m1 = new Message(null, null, "message-1"), m2 = new Message(
068: null, null, "message-2");
069: c1.connect("X");
070: c2.connect("X");
071: c1.send(m1);
072: c2.send(m2);
073:
074: Util.sleep(200);
075: List c1_list = r1.getMsgs(), c2_list = r2.getMsgs();
076: System.out.println("c1: " + c1_list.size() + " msgs, c2: "
077: + c2_list.size() + " msgs");
078: assertNotNull(c1_list);
079: assertNotNull(c2_list);
080: assertEquals(2, c1_list.size());
081: assertEquals(2, c2_list.size());
082: assertTrue(c1_list.contains("message-1"));
083: assertTrue(c2_list.contains("message-1"));
084: assertTrue(c1_list.contains("message-2"));
085: assertTrue(c2_list.contains("message-2"));
086: }
087:
088: public static Test suite() {
089: return new TestSuite(JoinTest.class);
090: }
091:
092: public static void main(String[] args) {
093: junit.textui.TestRunner.run(JoinTest.suite());
094: }
095:
096: private static class MyReceiver extends ReceiverAdapter {
097: String name;
098: List msgs = new LinkedList();
099:
100: public MyReceiver(String name) {
101: this .name = name;
102: }
103:
104: public List getMsgs() {
105: return msgs;
106: }
107:
108: public void clear() {
109: msgs.clear();
110: }
111:
112: public void receive(Message msg) {
113: String s = (String) msg.getObject();
114: msgs.add(s);
115: System.out.println("[" + name + "] received " + s
116: + " from " + msg.getSrc());
117: }
118:
119: public void viewAccepted(View new_view) {
120: System.out.println("[" + name + "] view: " + new_view);
121: }
122:
123: }
124: }
|