KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > stack > GossipTest


1 // $Id: GossipTest.java,v 1.4 2004/07/13 01:45:24 ovidiuf Exp $
2

3 package org.jgroups.tests.stack;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.Address;
9 import org.jgroups.stack.GossipData;
10 import org.jgroups.stack.GossipRouter;
11 import org.jgroups.stack.IpAddress;
12
13 import java.io.EOFException JavaDoc;
14 import java.io.ObjectInputStream JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.StreamCorruptedException JavaDoc;
17 import java.net.Socket JavaDoc;
18 import java.util.Vector JavaDoc;
19
20 /**
21  * Tests Gossip protocol primitives with the new GossipRouter. Since 2.2.1, the
22  * GossipRouter is supposed to answer Gossip requests too.
23  * <p/>
24  * It is possible to switch all tests to use an old GossipServer by setting
25  * USE_ROUTER to false;
26  *
27  * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
28  * @version $Revision: 1.4 $
29  * @since 2.2.1
30  */

31 public class GossipTest extends TestCase {
32
33     // configures the test to use the new gossip-enabled Router or the old
34
// GossipServer
35
private final boolean USE_ROUTER=true;
36
37     private int port=-1;
38
39     private long expiryTime=10000;
40
41     public GossipTest(String JavaDoc name) {
42         super(name);
43     }
44
45     public void setUp() throws Exception JavaDoc {
46         super.setUp();
47         if(USE_ROUTER) {
48             port=Utilities.startGossipRouter(expiryTime);
49         }
50         else {
51             port=Utilities.startGossipServer(expiryTime);
52         }
53     }
54
55     public void tearDown() throws Exception JavaDoc {
56         super.tearDown();
57         if(USE_ROUTER) {
58             Utilities.stopGossipRouter();
59         }
60         else {
61             Utilities.stopGossipServer(port);
62         }
63     }
64
65     /**
66      * Sends a Gossip GET request for an inexistent group.
67      */

68     public void testEmptyGET() throws Exception JavaDoc {
69
70         String JavaDoc groupName="nosuchgroup";
71         Socket JavaDoc s=new Socket JavaDoc("localhost", port);
72         ObjectOutputStream JavaDoc oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
73         GossipData greq=
74                 new GossipData(GossipData.GET_REQ, groupName, null, null);
75         oos.writeObject(greq);
76         oos.flush();
77         ObjectInputStream JavaDoc ois=new ObjectInputStream JavaDoc(s.getInputStream());
78         GossipData gres=(GossipData)ois.readObject();
79         assertEquals(GossipData.GET_RSP, gres.getType());
80         assertEquals(groupName, gres.getGroup());
81         Vector JavaDoc mbrs=gres.getMbrs();
82         assertNull(mbrs);
83
84         oos.close();
85         ois.close();
86         s.close();
87
88     }
89
90
91     /**
92      * Tests the situation when the gossip client is late in sending the
93      * serialized GossipData, after it opened the connection. If using a
94      * GossipServer, this shouldn't be an issue. If using the GossipRouter,
95      * the client will get a StreamCorruptedException and the call will fail.
96      */

97     public void testLazyClient() throws Exception JavaDoc {
98
99         String JavaDoc groupName="TESTGROUP";
100         Socket JavaDoc s=new Socket JavaDoc("localhost", port);
101         ObjectOutputStream JavaDoc oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
102         GossipData greq=
103                 new GossipData(GossipData.GET_REQ, groupName, null, null);
104
105         Thread.sleep(GossipRouter.GOSSIP_REQUEST_TIMEOUT + 500);
106
107         oos.writeObject(greq);
108         oos.flush();
109         ObjectInputStream JavaDoc ois=null;
110
111         if(USE_ROUTER) {
112             try {
113                 ois=new ObjectInputStream JavaDoc(s.getInputStream());
114                 fail("Stream creation should have failed");
115             }
116             catch(Exception JavaDoc e) {
117                 assertTrue(e instanceof StreamCorruptedException JavaDoc);
118             }
119         }
120         else {
121             // old GossipServer
122
ois=new ObjectInputStream JavaDoc(s.getInputStream());
123             GossipData gres=(GossipData)ois.readObject();
124             assertEquals(GossipData.GET_RSP, gres.getType());
125             assertEquals(groupName, gres.getGroup());
126             Vector JavaDoc mbrs=gres.getMbrs();
127             assertNull(mbrs);
128             ois.close();
129         }
130
131         oos.close();
132         s.close();
133
134         // send the second request, to make sure the server didn't hang
135

136         s=new Socket JavaDoc("localhost", port);
137         oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
138         greq=new GossipData(GossipData.GET_REQ, groupName, null, null);
139
140         oos.writeObject(greq);
141         oos.flush();
142         ois=new ObjectInputStream JavaDoc(s.getInputStream());
143         GossipData gres=(GossipData)ois.readObject();
144         assertEquals(GossipData.GET_RSP, gres.getType());
145         assertEquals(groupName, gres.getGroup());
146         Vector JavaDoc mbrs=gres.getMbrs();
147         assertNull(mbrs);
148
149         oos.close();
150         ois.close();
151         s.close();
152
153     }
154
155
156     /**
157      * Registers an Address with a group and then sends a GET request for that
158      * group.
159      */

160     public void test_REGISTER_GET() throws Exception JavaDoc {
161
162         String JavaDoc groupName="TESTGROUP";
163         int mbrPort=7777;
164
165         Socket JavaDoc s=new Socket JavaDoc("localhost", port);
166         ObjectOutputStream JavaDoc oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
167         Address mbr=new IpAddress("localhost", mbrPort);
168         GossipData greq = new GossipData(GossipData.REGISTER_REQ, groupName, mbr, null);
169         oos.writeObject(greq);
170         oos.flush();
171         
172         // test for end of stream
173
try {
174             s.getInputStream();
175         }
176         catch(EOFException JavaDoc e) {
177             if(!(e instanceof EOFException JavaDoc)) {
178                 fail("The input stream was supposed to throw EOFException");
179             }
180         }
181
182         oos.close();
183         s.close();
184
185         // send GET
186
s=new Socket JavaDoc("localhost", port);
187         oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
188         greq=new GossipData(GossipData.GET_REQ, groupName, null, null);
189         oos.writeObject(greq);
190         oos.flush();
191         ObjectInputStream JavaDoc ois=new ObjectInputStream JavaDoc(s.getInputStream());
192         GossipData gres=(GossipData)ois.readObject();
193         assertEquals(GossipData.GET_RSP, gres.getType());
194         assertEquals(groupName, gres.getGroup());
195         Vector JavaDoc mbrs=gres.getMbrs();
196         assertEquals(1, mbrs.size());
197         assertEquals(new IpAddress("localhost", mbrPort), mbrs.get(0));
198
199         oos.close();
200         ois.close();
201         s.close();
202
203     }
204
205     /**
206      * Test if a member is removed from group after EXPIRY_TIME ms.
207      */

208     public void testSweep() throws Exception JavaDoc {
209
210         String JavaDoc groupName="TESTGROUP";
211         int mbrPort=7777;
212
213         Socket JavaDoc s=new Socket JavaDoc("localhost", port);
214         ObjectOutputStream JavaDoc oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
215         Address mbr=new IpAddress("localhost", mbrPort);
216         GossipData greq=
217                 new GossipData(GossipData.REGISTER_REQ, groupName, mbr, null);
218         oos.writeObject(greq);
219         oos.flush();
220         oos.close();
221         s.close();
222
223         // send GET
224
s=new Socket JavaDoc("localhost", port);
225         oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
226         greq=new GossipData(GossipData.GET_REQ, groupName, null, null);
227         oos.writeObject(greq);
228         oos.flush();
229         ObjectInputStream JavaDoc ois=new ObjectInputStream JavaDoc(s.getInputStream());
230         GossipData gres=(GossipData)ois.readObject();
231         assertEquals(GossipData.GET_RSP, gres.getType());
232         assertEquals(groupName, gres.getGroup());
233         Vector JavaDoc mbrs=gres.getMbrs();
234         assertEquals(1, mbrs.size());
235         assertEquals(new IpAddress("localhost", mbrPort), mbrs.get(0));
236
237         oos.close();
238         ois.close();
239         s.close();
240
241         // because the sweep is ran at fixed expiryTime intervals, if
242
// an entry was added immediately after a sweep run, it actually
243
// spends almost 2*expiryTime in cache.
244
Thread.sleep(2 * expiryTime);
245
246         // send a second GET after more than EXPIRY_TIME ms
247

248         s=new Socket JavaDoc("localhost", port);
249         oos=new ObjectOutputStream JavaDoc(s.getOutputStream());
250         greq=new GossipData(GossipData.GET_REQ, groupName, null, null);
251         oos.writeObject(greq);
252         oos.flush();
253         ois=new ObjectInputStream JavaDoc(s.getInputStream());
254         gres=(GossipData)ois.readObject();
255         assertEquals(GossipData.GET_RSP, gres.getType());
256         assertEquals(groupName, gres.getGroup());
257         mbrs=gres.getMbrs();
258         assertEquals(0, mbrs.size());
259
260         oos.close();
261         ois.close();
262         s.close();
263
264     }
265
266     public static Test suite() {
267         TestSuite s=new TestSuite(GossipTest.class);
268         return s;
269     }
270
271     public static void main(String JavaDoc[] args) {
272         junit.textui.TestRunner.run(suite());
273         System.exit(0);
274     }
275
276
277 }
278
Popular Tags