KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > channel > ReliableGroupChannelTest


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.channel;
26
27 import java.net.InetAddress JavaDoc;
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 /**
41  * This class defines a ReliableGroupChannelTest
42  *
43  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
44  * @version 1.0
45  */

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 JavaDoc msg1 = "Hello from 1";
53   private final String JavaDoc msg2 = "Hello from 2";
54
55   /*
56    * @see TestCase#setUp()
57    */

58   protected void setUp() throws Exception JavaDoc
59   {
60     final InetAddress JavaDoc 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 JavaDoc 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   /**
78    * Test the group join/send/receive/quit primitives
79    */

80   public void testJoinSendReceiveQuit()
81   {
82     // Join
83

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     // Sleep 2 seconds
124

125     try
126     {
127       Thread.sleep(2000);
128     }
129     catch (InterruptedException JavaDoc e4)
130     {
131       e4.printStackTrace();
132     }
133
134     // Send
135

136     try
137     {
138       channel1.send(msg1);
139     }
140     catch (Exception JavaDoc 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 JavaDoc e1)
150     {
151       e1.printStackTrace();
152       fail("Unable to send message on channel2");
153     }
154
155     // Receive
156

157     try
158     {
159       assertEquals(msg1, channel1.receive());
160       assertEquals(msg2, channel1.receive());
161     }
162     catch (Exception JavaDoc 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 JavaDoc e2)
173     {
174       e2.printStackTrace();
175       fail("Failed to receive message on channel2");
176     }
177
178     // Quit
179

180     try
181     {
182       channel1.quit();
183     }
184     catch (Exception JavaDoc e3)
185     {
186       e3.printStackTrace();
187       fail("Failed to quit group for channel1");
188     }
189     try
190     {
191       channel2.quit();
192     }
193     catch (Exception JavaDoc e3)
194     {
195       e3.printStackTrace();
196       fail("Failed to quit group for channel2");
197     }
198   }
199 }
Popular Tags