001: // $Id: GossipClientTest.java,v 1.2.2.2 2007/03/09 07:50:44 belaban Exp $
002:
003: package org.jgroups.tests.stack;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.Address;
009: import org.jgroups.stack.GossipClient;
010: import org.jgroups.stack.IpAddress;
011:
012: import java.util.List;
013:
014: /**
015: * Tests Gossip protocol primitives with the new GossipRouter. Since 2.2.1, the
016: * GossipRouter is supposed to answer Gossip requests too.
017: *
018: * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
019: * @author Bela Ban
020: * @version $Revision: 1.2.2.2 $
021: * @since 2.2.1
022: */
023: public class GossipClientTest extends TestCase {
024: GossipClient client;
025: private int port = -1;
026: private long expiryTime = 1000;
027:
028: public GossipClientTest(String name) {
029: super (name);
030: }
031:
032: public void setUp() throws Exception {
033: super .setUp();
034: port = Utilities.startGossipRouter(expiryTime, "127.0.0.1");
035: client = new GossipClient(new IpAddress("127.0.0.1", port),
036: expiryTime);
037: client.setRefresherEnabled(false); // don't refresh the registrations
038: }
039:
040: public void tearDown() throws Exception {
041: super .tearDown();
042: client.stop();
043: Utilities.stopGossipRouter();
044: }
045:
046: public void testEmptyGET() throws Exception {
047: String groupName = "nosuchgroup";
048: List mbrs = client.getMembers(groupName);
049: assertNotNull(mbrs);
050: assertEquals(0, mbrs.size());
051: }
052:
053: /**
054: * Registers an address with a group and then sends a GET request for that group.
055: */
056: public void test_REGISTER_GET() throws Exception {
057: String groupName = "TESTGROUP";
058: int mbrPort = 7777;
059: Address mbr = new IpAddress("127.0.0.1", mbrPort);
060: client.register(groupName, mbr);
061:
062: List mbrs = client.getMembers(groupName);
063: assertEquals(1, mbrs.size());
064: assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
065: }
066:
067: public void test_REGISTER_UNREGISTER_GET() throws Exception {
068: String groupName = "TESTGROUP";
069: int mbrPort = 7777;
070: Address mbr = new IpAddress("127.0.0.1", mbrPort);
071: client.register(groupName, mbr);
072:
073: List mbrs = client.getMembers(groupName);
074: assertEquals(1, mbrs.size());
075: assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
076:
077: client.unregister(groupName, mbr);
078: mbrs = client.getMembers(groupName);
079: assertNotNull(mbrs);
080: assertEquals(0, mbrs.size());
081: }
082:
083: /**
084: * Test if a member is removed from group after EXPIRY_TIME ms.
085: */
086: public void testSweep() throws Exception {
087: String groupName = "TESTGROUP";
088: int mbrPort = 7777;
089: Address mbr = new IpAddress("127.0.0.1", mbrPort);
090:
091: client.register(groupName, mbr);
092:
093: List mbrs = client.getMembers(groupName);
094: assertEquals(1, mbrs.size());
095: assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0));
096:
097: // because the sweep is ran at fixed expiryTime intervals, if
098: // an entry was added immediately after a sweep run, it actually
099: // spends almost 2*expiryTime in cache.
100: Thread.sleep(3 * expiryTime);
101:
102: // send a second GET after more than EXPIRY_TIME ms
103: mbrs = client.getMembers(groupName);
104: assertTrue("mbrs=" + mbrs + ", should be empty", mbrs == null
105: || mbrs.isEmpty());
106: }
107:
108: public static Test suite() {
109: return new TestSuite(GossipClientTest.class);
110: }
111:
112: public static void main(String[] args) {
113: junit.textui.TestRunner.run(suite());
114: System.exit(0);
115: }
116:
117: }
|