KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > AddDataTest


1 package org.jgroups.tests;
2
3 import junit.framework.TestCase;
4 import org.jgroups.*;
5 import org.jgroups.stack.IpAddress;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 /**
11  *
12  * @author Bela Ban
13  * @version $Id: AddDataTest.java,v 1.9 2007/03/06 09:07:27 belaban Exp $
14  */

15 public class AddDataTest extends TestCase {
16     JChannel ch1, ch2;
17     
18     String JavaDoc properties="udp.xml";
19
20     String JavaDoc bundlingProperties="UDP(mcast_addr=228.1.2.3;mcast_port=45566;ip_ttl=32;" +
21             "enable_bundling=true;max_bundle_size=3000;max_bundle_timeout=500):" +
22             "PING(timeout=2000;num_initial_members=2):" +
23             "pbcast.NAKACK(gc_lag=10;retransmit_timeout=600,1200,2400,4800):" +
24             "UNICAST(timeout=600,1200,2400,4800):" +
25             "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
26             "shun=true;print_local_addr=true)";
27
28
29
30
31     public AddDataTest(String JavaDoc name) {
32         super(name);
33     }
34
35     protected void tearDown() throws Exception JavaDoc {
36         super.tearDown();
37         if(ch2 != null)
38             ch2.close();
39         if(ch1 != null)
40             ch1.close();
41     }
42
43
44     /**
45      * Uncomment to test shunning/reconnecting (using CTRL-Z and fg)
46      */

47 // public void testAdditionalDataWithShun() {
48
// try {
49
// JChannel c=new JChannel(props);
50
// Map m=new HashMap();
51
// m.put("additional_data", new byte[]{'b', 'e', 'l', 'a'});
52
// c.down(new Event(Event.CONFIG, m));
53
// c.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
54
// c.setChannelListener(new ChannelListener() {
55
// public void channelDisconnected(Channel channel) {
56
// System.out.println("channel disconnected");
57
// }
58
//
59
// public void channelShunned() {
60
// System.out.println("channel shunned");
61
// }
62
//
63
// public void channelReconnected(Address addr) {
64
// System.out.println("channel reconnected");
65
// }
66
//
67
// public void channelConnected(Channel channel) {
68
// System.out.println("channel connected");
69
// }
70
//
71
// public void channelClosed(Channel channel) {
72
// System.out.println("channel closed");
73
// }
74
// });
75
// System.out.println("CONNECTING");
76
// c.connect("bla");
77
// System.out.println("CONNECTING: done");
78
// IpAddress addr=(IpAddress)c.getLocalAddress();
79
// System.out.println("address is " + addr);
80
// assertNotNull(addr.getAdditionalData());
81
// assertEquals(addr.getAdditionalData()[0], 'b');
82
// Util.sleep(600000);
83
// c.close();
84
// }
85
// catch(ChannelException e) {
86
// e.printStackTrace();
87
// fail(e.toString());
88
// }
89
// }
90

91
92     public void testAdditionalData() {
93         try {
94             for(int i=1; i <= 5; i++) {
95                 System.out.println("-- attempt # " + i + "/10");
96                 JChannel c=new JChannel(properties);
97                 Map JavaDoc m=new HashMap JavaDoc();
98                 m.put("additional_data", new byte[]{'b', 'e', 'l', 'a'});
99                 c.down(new Event(Event.CONFIG, m));
100                 c.connect("bla");
101                 IpAddress addr=(IpAddress)c.getLocalAddress();
102                 System.out.println("address is " + addr);
103                 assertNotNull(addr.getAdditionalData());
104                 assertEquals('b', addr.getAdditionalData()[0]);
105                 c.close();
106             }
107         }
108         catch(ChannelException e) {
109             e.printStackTrace();
110             fail(e.toString());
111         }
112     }
113
114
115     public void testBetweenTwoChannelsMcast() throws Exception JavaDoc {
116         _testWithProps(this.properties, true);
117     }
118
119     public void testBetweenTwoChannelsUnicast() throws Exception JavaDoc {
120         _testWithProps(this.properties, false);
121     }
122
123     public void testBetweenTwoChannelsWithBundlingMcast() throws Exception JavaDoc {
124         _testWithProps(this.bundlingProperties, true);
125     }
126
127     public void testBetweenTwoChannelsWithBundlingUnicast() throws Exception JavaDoc {
128         _testWithProps(this.bundlingProperties, false);
129     }
130
131
132
133     private void _testWithProps(String JavaDoc props, boolean mcast) throws Exception JavaDoc {
134         Map JavaDoc m=new HashMap JavaDoc();
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
141         ch2=new JChannel(props);
142         ch2.down(new Event(Event.CONFIG, m));
143         ch1.connect("group");
144         ch2.connect("group");
145         while(ch2.peek(10) != null) {
146             System.out.println("-- received " + ch2.receive(100));
147         }
148         if(mcast)
149             ch1.send(new Message(null, null, buf));
150         else {
151             Address dest=ch2.getLocalAddress();
152             ch1.send(new Message(dest, null, buf));
153         }
154         Message msg=(Message)ch2.receive(10000);
155         System.out.println("received " + msg);
156         IpAddress SRC=(IpAddress)msg.getSrc();
157         System.out.println("src=" + src);
158
159         // Thread.sleep(600000); // todo: remove
160

161         assertNotNull(src);
162         assertNotNull(src.getAdditionalData());
163         assertEquals(4, src.getAdditionalData().length);
164     }
165
166
167     public static void main(String JavaDoc[] args) {
168         String JavaDoc[] testCaseName={AddDataTest.class.getName()};
169         junit.textui.TestRunner.main(testCaseName);
170     }
171
172 }
173
Popular Tags