1 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 ; 13 14 23 public class GossipClientTest extends TestCase { 24 GossipClient client; 25 private int port=-1; 26 private long expiryTime=1000; 27 28 public GossipClientTest(String name) { 29 super(name); 30 } 31 32 public void setUp() throws Exception { 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); } 38 39 public void tearDown() throws Exception { 40 super.tearDown(); 41 client.stop(); 42 Utilities.stopGossipRouter(); 43 } 44 45 46 public void testEmptyGET() throws Exception { 47 String groupName="nosuchgroup"; 48 List mbrs=client.getMembers(groupName); 49 assertNotNull(mbrs); 50 assertEquals(0, mbrs.size()); 51 } 52 53 54 57 public void test_REGISTER_GET() throws Exception { 58 String groupName="TESTGROUP"; 59 int mbrPort=7777; 60 Address mbr=new IpAddress("127.0.0.1", mbrPort); 61 client.register(groupName, mbr); 62 63 List 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 { 69 String groupName="TESTGROUP"; 70 int mbrPort=7777; 71 Address mbr=new IpAddress("127.0.0.1", mbrPort); 72 client.register(groupName, mbr); 73 74 List 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 88 public void testSweep() throws Exception { 89 String 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 mbrs=client.getMembers(groupName); 96 assertEquals(1, mbrs.size()); 97 assertEquals(new IpAddress("127.0.0.1", mbrPort), mbrs.get(0)); 98 99 Thread.sleep(2 * expiryTime); 103 104 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 [] args) { 114 junit.textui.TestRunner.run(suite()); 115 System.exit(0); 116 } 117 118 119 } 120 | Popular Tags |