001: package org.jgroups.tests;
002:
003: import junit.framework.TestCase;
004: import org.jgroups.*;
005: import org.jgroups.stack.IpAddress;
006:
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: /**
011: *
012: * @author Bela Ban
013: * @version $Id: AddDataTest.java,v 1.7 2005/04/19 12:11:41 belaban Exp $
014: */
015: public class AddDataTest extends TestCase {
016: JChannel ch1, ch2;
017:
018: String properties = "UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=32;down_thread=false;up_thread=false):"
019: + "PING(timeout=2000;num_initial_members=2;down_thread=false;up_thread=false):"
020: + "pbcast.NAKACK(gc_lag=10;retransmit_timeout=600,1200,2400,4800;down_thread=false;up_thread=false):"
021: + "UNICAST(timeout=600,1200,2400,4800;down_thread=false;up_thread=false):"
022: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
023: + "shun=true;print_local_addr=true;down_thread=false;up_thread=false)";
024:
025: String bundlingProperties = "UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=32;"
026: + "enable_bundling=true;max_bundle_size=3000;max_bundle_timeout=500;down_thread=false;up_thread=false):"
027: + "PING(timeout=2000;num_initial_members=2;down_thread=false;up_thread=false):"
028: + "pbcast.NAKACK(gc_lag=10;retransmit_timeout=600,1200,2400,4800;down_thread=false;up_thread=false):"
029: + "UNICAST(timeout=600,1200,2400,4800;down_thread=false;up_thread=false):"
030: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
031: + "shun=true;print_local_addr=true;down_thread=false;up_thread=false)";
032:
033: public AddDataTest(String name) {
034: super (name);
035: }
036:
037: protected void tearDown() throws Exception {
038: super .tearDown();
039: if (ch2 != null)
040: ch2.close();
041: if (ch1 != null)
042: ch1.close();
043: }
044:
045: /**
046: * Uncomment to test shunning/reconnecting (using CTRL-Z and fg)
047: */
048: // public void testAdditionalDataWithShun() {
049: // try {
050: // JChannel c=new JChannel(props);
051: // Map m=new HashMap();
052: // m.put("additional_data", new byte[]{'b', 'e', 'l', 'a'});
053: // c.down(new Event(Event.CONFIG, m));
054: // c.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
055: // c.setChannelListener(new ChannelListener() {
056: // public void channelDisconnected(Channel channel) {
057: // System.out.println("channel disconnected");
058: // }
059: //
060: // public void channelShunned() {
061: // System.out.println("channel shunned");
062: // }
063: //
064: // public void channelReconnected(Address addr) {
065: // System.out.println("channel reconnected");
066: // }
067: //
068: // public void channelConnected(Channel channel) {
069: // System.out.println("channel connected");
070: // }
071: //
072: // public void channelClosed(Channel channel) {
073: // System.out.println("channel closed");
074: // }
075: // });
076: // System.out.println("CONNECTING");
077: // c.connect("bla");
078: // System.out.println("CONNECTING: done");
079: // IpAddress addr=(IpAddress)c.getLocalAddress();
080: // System.out.println("address is " + addr);
081: // assertNotNull(addr.getAdditionalData());
082: // assertEquals(addr.getAdditionalData()[0], 'b');
083: // Util.sleep(600000);
084: // c.close();
085: // }
086: // catch(ChannelException e) {
087: // e.printStackTrace();
088: // fail(e.toString());
089: // }
090: // }
091:
092: public void testAdditionalData() {
093: try {
094: for (int i = 1; i <= 5; i++) {
095: System.out.println("-- attempt # " + i + "/10");
096: JChannel c = new JChannel(properties);
097: Map m = new HashMap();
098: m.put("additional_data", new byte[] { 'b', 'e', 'l',
099: 'a' });
100: c.down(new Event(Event.CONFIG, m));
101: c.connect("bla");
102: IpAddress addr = (IpAddress) c.getLocalAddress();
103: System.out.println("address is " + addr);
104: assertNotNull(addr.getAdditionalData());
105: assertEquals(addr.getAdditionalData()[0], 'b');
106: c.close();
107: }
108: } catch (ChannelException e) {
109: e.printStackTrace();
110: fail(e.toString());
111: }
112: }
113:
114: public void testBetweenTwoChannelsMcast() throws Exception {
115: _testWithProps(this .properties, true);
116: }
117:
118: public void testBetweenTwoChannelsUnicast() throws Exception {
119: _testWithProps(this .properties, false);
120: }
121:
122: public void testBetweenTwoChannelsWithBundlingMcast()
123: throws Exception {
124: _testWithProps(this .bundlingProperties, true);
125: }
126:
127: public void testBetweenTwoChannelsWithBundlingUnicast()
128: throws Exception {
129: _testWithProps(this .bundlingProperties, false);
130: }
131:
132: private void _testWithProps(String props, boolean mcast)
133: throws Exception {
134: Map m = new HashMap();
135: m.put("additional_data", new byte[] { 'b', 'e', 'l', 'a' });
136: byte[] buf = new byte[1000];
137:
138: ch1 = new JChannel(props);
139: ch1.down(new Event(Event.CONFIG, m));
140: ch2 = new JChannel(props);
141: ch1.connect("group");
142: ch2.connect("group");
143: while (ch2.peek(10) != null) {
144: System.out.println("-- received " + ch2.receive(100));
145: }
146: if (mcast)
147: ch1.send(new Message(null, null, buf));
148: else {
149: Address dest = ch2.getLocalAddress();
150: ch1.send(new Message(dest, null, buf));
151: }
152: Message msg = (Message) ch2.receive(10000);
153: System.out.println("received " + msg);
154: IpAddress src = (IpAddress) msg.getSrc();
155: System.out.println("src=" + src);
156:
157: // Thread.sleep(600000); // todo: remove
158:
159: assertNotNull(src);
160: assertNotNull(src.getAdditionalData());
161: assertEquals(4, src.getAdditionalData().length);
162: }
163:
164: public static void main(String[] args) {
165: String[] testCaseName = { AddDataTest.class.getName() };
166: junit.textui.TestRunner.main(testCaseName);
167: }
168:
169: }
|