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 ; 8 import java.util.Map ; 9 10 15 public class AddDataTest extends TestCase { 16 JChannel ch1, ch2; 17 18 String properties="udp.xml"; 19 20 String 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 name) { 32 super(name); 33 } 34 35 protected void tearDown() throws Exception { 36 super.tearDown(); 37 if(ch2 != null) 38 ch2.close(); 39 if(ch1 != null) 40 ch1.close(); 41 } 42 43 44 47 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 m=new HashMap (); 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 { 116 _testWithProps(this.properties, true); 117 } 118 119 public void testBetweenTwoChannelsUnicast() throws Exception { 120 _testWithProps(this.properties, false); 121 } 122 123 public void testBetweenTwoChannelsWithBundlingMcast() throws Exception { 124 _testWithProps(this.bundlingProperties, true); 125 } 126 127 public void testBetweenTwoChannelsWithBundlingUnicast() throws Exception { 128 _testWithProps(this.bundlingProperties, false); 129 } 130 131 132 133 private void _testWithProps(String props, boolean mcast) 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 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 161 assertNotNull(src); 162 assertNotNull(src.getAdditionalData()); 163 assertEquals(4, src.getAdditionalData().length); 164 } 165 166 167 public static void main(String [] args) { 168 String [] testCaseName={AddDataTest.class.getName()}; 169 junit.textui.TestRunner.main(testCaseName); 170 } 171 172 } 173 | Popular Tags |