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.GossipData; 10 import org.jgroups.stack.GossipRouter; 11 import org.jgroups.stack.IpAddress; 12 13 import java.io.EOFException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.StreamCorruptedException ; 17 import java.net.Socket ; 18 import java.util.Vector ; 19 20 31 public class GossipTest extends TestCase { 32 33 private final boolean USE_ROUTER=true; 36 37 private int port=-1; 38 39 private long expiryTime=10000; 40 41 public GossipTest(String name) { 42 super(name); 43 } 44 45 public void setUp() throws Exception { 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 { 56 super.tearDown(); 57 if(USE_ROUTER) { 58 Utilities.stopGossipRouter(); 59 } 60 else { 61 Utilities.stopGossipServer(port); 62 } 63 } 64 65 68 public void testEmptyGET() throws Exception { 69 70 String groupName="nosuchgroup"; 71 Socket s=new Socket ("localhost", port); 72 ObjectOutputStream oos=new ObjectOutputStream (s.getOutputStream()); 73 GossipData greq= 74 new GossipData(GossipData.GET_REQ, groupName, null, null); 75 oos.writeObject(greq); 76 oos.flush(); 77 ObjectInputStream ois=new ObjectInputStream (s.getInputStream()); 78 GossipData gres=(GossipData)ois.readObject(); 79 assertEquals(GossipData.GET_RSP, gres.getType()); 80 assertEquals(groupName, gres.getGroup()); 81 Vector mbrs=gres.getMbrs(); 82 assertNull(mbrs); 83 84 oos.close(); 85 ois.close(); 86 s.close(); 87 88 } 89 90 91 97 public void testLazyClient() throws Exception { 98 99 String groupName="TESTGROUP"; 100 Socket s=new Socket ("localhost", port); 101 ObjectOutputStream oos=new ObjectOutputStream (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 ois=null; 110 111 if(USE_ROUTER) { 112 try { 113 ois=new ObjectInputStream (s.getInputStream()); 114 fail("Stream creation should have failed"); 115 } 116 catch(Exception e) { 117 assertTrue(e instanceof StreamCorruptedException ); 118 } 119 } 120 else { 121 ois=new ObjectInputStream (s.getInputStream()); 123 GossipData gres=(GossipData)ois.readObject(); 124 assertEquals(GossipData.GET_RSP, gres.getType()); 125 assertEquals(groupName, gres.getGroup()); 126 Vector mbrs=gres.getMbrs(); 127 assertNull(mbrs); 128 ois.close(); 129 } 130 131 oos.close(); 132 s.close(); 133 134 136 s=new Socket ("localhost", port); 137 oos=new ObjectOutputStream (s.getOutputStream()); 138 greq=new GossipData(GossipData.GET_REQ, groupName, null, null); 139 140 oos.writeObject(greq); 141 oos.flush(); 142 ois=new ObjectInputStream (s.getInputStream()); 143 GossipData gres=(GossipData)ois.readObject(); 144 assertEquals(GossipData.GET_RSP, gres.getType()); 145 assertEquals(groupName, gres.getGroup()); 146 Vector mbrs=gres.getMbrs(); 147 assertNull(mbrs); 148 149 oos.close(); 150 ois.close(); 151 s.close(); 152 153 } 154 155 156 160 public void test_REGISTER_GET() throws Exception { 161 162 String groupName="TESTGROUP"; 163 int mbrPort=7777; 164 165 Socket s=new Socket ("localhost", port); 166 ObjectOutputStream oos=new ObjectOutputStream (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 try { 174 s.getInputStream(); 175 } 176 catch(EOFException e) { 177 if(!(e instanceof EOFException )) { 178 fail("The input stream was supposed to throw EOFException"); 179 } 180 } 181 182 oos.close(); 183 s.close(); 184 185 s=new Socket ("localhost", port); 187 oos=new ObjectOutputStream (s.getOutputStream()); 188 greq=new GossipData(GossipData.GET_REQ, groupName, null, null); 189 oos.writeObject(greq); 190 oos.flush(); 191 ObjectInputStream ois=new ObjectInputStream (s.getInputStream()); 192 GossipData gres=(GossipData)ois.readObject(); 193 assertEquals(GossipData.GET_RSP, gres.getType()); 194 assertEquals(groupName, gres.getGroup()); 195 Vector 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 208 public void testSweep() throws Exception { 209 210 String groupName="TESTGROUP"; 211 int mbrPort=7777; 212 213 Socket s=new Socket ("localhost", port); 214 ObjectOutputStream oos=new ObjectOutputStream (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 s=new Socket ("localhost", port); 225 oos=new ObjectOutputStream (s.getOutputStream()); 226 greq=new GossipData(GossipData.GET_REQ, groupName, null, null); 227 oos.writeObject(greq); 228 oos.flush(); 229 ObjectInputStream ois=new ObjectInputStream (s.getInputStream()); 230 GossipData gres=(GossipData)ois.readObject(); 231 assertEquals(GossipData.GET_RSP, gres.getType()); 232 assertEquals(groupName, gres.getGroup()); 233 Vector 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 Thread.sleep(2 * expiryTime); 245 246 248 s=new Socket ("localhost", port); 249 oos=new ObjectOutputStream (s.getOutputStream()); 250 greq=new GossipData(GossipData.GET_REQ, groupName, null, null); 251 oos.writeObject(greq); 252 oos.flush(); 253 ois=new ObjectInputStream (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 [] args) { 272 junit.textui.TestRunner.run(suite()); 273 System.exit(0); 274 } 275 276 277 } 278 | Popular Tags |