KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: GossipClientTest.java,v 1.2 2006/10/11 14:33:53 belaban 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.GossipClient;
10 import org.jgroups.stack.IpAddress;
11
12 import java.util.List JavaDoc;
13
14 /**
15  * Tests Gossip protocol primitives with the new GossipRouter. Since 2.2.1, the
16  * GossipRouter is supposed to answer Gossip requests too.
17  *
18  * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
19  * @author Bela Ban
20  * @version $Revision: 1.2 $
21  * @since 2.2.1
22  */

23 public class GossipClientTest extends TestCase {
24     GossipClient client;
25     private int port=-1;
26     private long expiryTime=1000;
27
28     public GossipClientTest(String JavaDoc name) {
29         super(name);
30     }
31
32     public void setUp() throws Exception JavaDoc {
33         super.setUp();
34         port=Utilities.startGossipRouter(expiryTime, "127.0.0.1");
35         client=new GossipClient(new IpAddress("127.0.0.1", port), expiryTime);
36         client.setRefresherEnabled(false); // don't refresh the registrations
37
}
38
39     public void tearDown() throws Exception JavaDoc {
40         super.tearDown();
41         client.stop();
42         Utilities.stopGossipRouter();
43     }
44
45
46     public void testEmptyGET() throws Exception JavaDoc {
47         String JavaDoc groupName="nosuchgroup";
48         List JavaDoc mbrs=client.getMembers(groupName);
49         assertNotNull(mbrs);
50         assertEquals(0, mbrs.size());
51     }
52
53
54     /**
55      * Registers an address with a group and then sends a GET request for that group.
56      */

57     public void test_REGISTER_GET() throws Exception JavaDoc {
58         String JavaDoc groupName="TESTGROUP";
59         int mbrPort=7777;
60         Address mbr=new IpAddress("127.0.0.1", mbrPort);
61         client.register(groupName, mbr);
62
63         List JavaDoc mbrs=client.getMembers(groupName);
64         assertEquals(1, mbrs.size());
65         assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
66     }
67
68     public void test_REGISTER_UNREGISTER_GET() throws Exception JavaDoc {
69         String JavaDoc groupName="TESTGROUP";
70         int mbrPort=7777;
71         Address mbr=new IpAddress("127.0.0.1", mbrPort);
72         client.register(groupName, mbr);
73
74         List JavaDoc mbrs=client.getMembers(groupName);
75         assertEquals(1, mbrs.size());
76         assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
77
78         client.unregister(groupName, mbr);
79         mbrs=client.getMembers(groupName);
80         assertNotNull(mbrs);
81         assertEquals(0, mbrs.size());
82     }
83
84
85     /**
86      * Test if a member is removed from group after EXPIRY_TIME ms.
87      */

88     public void testSweep() throws Exception JavaDoc {
89         String JavaDoc groupName="TESTGROUP";
90         int mbrPort=7777;
91         Address mbr=new IpAddress("127.0.0.1", mbrPort);
92
93         client.register(groupName, mbr);
94
95         List JavaDoc mbrs=client.getMembers(groupName);
96         assertEquals(1, mbrs.size());
97         assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
98
99         // because the sweep is ran at fixed expiryTime intervals, if
100
// an entry was added immediately after a sweep run, it actually
101
// spends almost 2*expiryTime in cache.
102
Thread.sleep(2 * expiryTime);
103
104         // send a second GET after more than EXPIRY_TIME ms
105
mbrs=client.getMembers(groupName);
106         assertTrue(mbrs == null || mbrs.size() == 0);
107     }
108
109     public static Test suite() {
110         return new TestSuite(GossipClientTest.class);
111     }
112
113     public static void main(String JavaDoc[] args) {
114         junit.textui.TestRunner.run(suite());
115         System.exit(0);
116     }
117
118
119 }
120
Popular Tags