1 24 25 package org.objectweb.tribe.channel; 26 27 import java.net.InetAddress ; 28 29 import junit.framework.TestCase; 30 31 import org.objectweb.tribe.channel.tcp.TcpChannelPool; 32 import org.objectweb.tribe.common.GroupIdentifier; 33 import org.objectweb.tribe.common.IpAddress; 34 import org.objectweb.tribe.exceptions.AlreadyMemberException; 35 import org.objectweb.tribe.exceptions.ChannelException; 36 import org.objectweb.tribe.exceptions.NotConnectedException; 37 import org.objectweb.tribe.gms.GroupMembershipService; 38 import org.objectweb.tribe.gms.discovery.UdpDiscoveryService; 39 40 46 public class ReliableGroupChannelTest extends TestCase 47 { 48 49 private ReliableGroupChannelWithGms channel1; 50 private ReliableGroupChannelWithGms channel2; 51 private final GroupIdentifier gid1 = new GroupIdentifier("group1"); 52 private final String msg1 = "Hello from 1"; 53 private final String msg2 = "Hello from 2"; 54 55 58 protected void setUp() throws Exception 59 { 60 final InetAddress MULTICAST_ADDRESS = InetAddress.getByName("224.7.65.23"); 61 final int MULTICAST_PORT = 2288; 62 final IpAddress MULTICAST_IP = new IpAddress(MULTICAST_ADDRESS, 63 MULTICAST_PORT); 64 final InetAddress REPLY_ADDRESS = InetAddress.getLocalHost(); 65 final int REPLY_PORT = 12345; 66 final IpAddress REPLY_IP = new IpAddress(REPLY_ADDRESS, REPLY_PORT); 67 68 UdpDiscoveryService discovery = new UdpDiscoveryService(MULTICAST_IP, 69 REPLY_IP); 70 GroupMembershipService gms = new GroupMembershipService(REPLY_IP, 71 TcpChannelPool.getChannelPool(), discovery); 72 discovery.start(); 73 channel1 = new ReliableGroupChannelWithGms(gms); 74 channel2 = new ReliableGroupChannelWithGms(gms); 75 } 76 77 80 public void testJoinSendReceiveQuit() 81 { 82 84 try 85 { 86 channel1.join(gid1); 87 } 88 catch (AlreadyMemberException e) 89 { 90 e.printStackTrace(); 91 fail("Failed to join group for channel1"); 92 } 93 catch (ChannelException e) 94 { 95 e.printStackTrace(); 96 fail("Failed to join group for channel1"); 97 } 98 catch (NotConnectedException e) 99 { 100 e.printStackTrace(); 101 fail("Failed to join group for channel1"); 102 } 103 try 104 { 105 channel2.join(gid1); 106 } 107 catch (AlreadyMemberException e) 108 { 109 e.printStackTrace(); 110 fail("Failed to join group for channel1"); 111 } 112 catch (ChannelException e) 113 { 114 e.printStackTrace(); 115 fail("Failed to join group for channel1"); 116 } 117 catch (NotConnectedException e) 118 { 119 e.printStackTrace(); 120 fail("Failed to join group for channel1"); 121 } 122 123 125 try 126 { 127 Thread.sleep(2000); 128 } 129 catch (InterruptedException e4) 130 { 131 e4.printStackTrace(); 132 } 133 134 136 try 137 { 138 channel1.send(msg1); 139 } 140 catch (Exception e1) 141 { 142 e1.printStackTrace(); 143 fail("Unable to send message on channel1"); 144 } 145 try 146 { 147 channel2.send(msg2); 148 } 149 catch (Exception e1) 150 { 151 e1.printStackTrace(); 152 fail("Unable to send message on channel2"); 153 } 154 155 157 try 158 { 159 assertEquals(msg1, channel1.receive()); 160 assertEquals(msg2, channel1.receive()); 161 } 162 catch (Exception e2) 163 { 164 e2.printStackTrace(); 165 fail("Failed to receive message on channel1"); 166 } 167 try 168 { 169 assertEquals(msg1, channel2.receive()); 170 assertEquals(msg2, channel2.receive()); 171 } 172 catch (Exception e2) 173 { 174 e2.printStackTrace(); 175 fail("Failed to receive message on channel2"); 176 } 177 178 180 try 181 { 182 channel1.quit(); 183 } 184 catch (Exception e3) 185 { 186 e3.printStackTrace(); 187 fail("Failed to quit group for channel1"); 188 } 189 try 190 { 191 channel2.quit(); 192 } 193 catch (Exception e3) 194 { 195 e3.printStackTrace(); 196 fail("Failed to quit group for channel2"); 197 } 198 } 199 } | Popular Tags |