1 package org.jgroups.tests; 2 3 import java.util.ArrayList ; 4 import java.util.Collections ; 5 import java.util.List ; 6 7 import junit.framework.Test; 8 import junit.framework.TestSuite; 9 10 import org.jgroups.Channel; 11 import org.jgroups.ChannelException; 12 import org.jgroups.Message; 13 import org.jgroups.ReceiverAdapter; 14 import org.jgroups.View; 15 import org.jgroups.util.Util; 16 17 18 22 public class JoinTest extends ChannelTestBase { 23 Channel c1, c2; 24 25 public JoinTest(String name) { 26 super(name); 27 } 28 29 30 public void setUp() throws Exception { 31 super.setUp(); 32 c1=createChannel("A"); 33 c2=createChannel("A"); 34 } 35 36 37 public void tearDown() throws Exception { 38 if(c2 != null) 39 c2.close(); 40 if(c1 != null) 41 c1.close(); 42 super.tearDown(); 43 } 44 45 public void testSingleJoin() throws ChannelException { 46 c1.connect("X"); 47 View v=c1.getView(); 48 assertNotNull(v); 49 assertEquals(1, v.size()); 50 } 51 52 53 57 public void testJoinsOnTwoChannels() throws ChannelException { 58 c1.connect("X"); 59 c2.connect("X"); 60 61 Util.sleep(2000); 63 64 View v1=c1.getView(), v2=c2.getView(); 65 System.out.println("v1=" + v1 + ", v2=" + v2); 66 assertNotNull(v1); 67 assertNotNull(v2); 68 assertEquals(2, v1.size()); 69 assertEquals(2, v2.size()); 70 assertEquals(v1, v2); 71 } 72 73 74 public void testJoinsOnTwoChannelsAndSend() throws ChannelException { 75 MyReceiver r1=new MyReceiver("c1"); 76 MyReceiver r2=new MyReceiver("c2"); 77 c1.setReceiver(r1); 78 c2.setReceiver(r2); 79 Message m1=new Message(null, null, "message-1"), m2=new Message(null, null, "message-2"); 80 c1.connect("X"); 81 View view=c1.getView(); 82 assertEquals("c1's view: " + view, 1, view.size()); 83 c2.connect("X"); 84 view=c2.getView(); 85 assertEquals("c2's view: " + view, 2, view.size()); 86 Util.sleep(200); 87 view=c1.getView(); 88 assertEquals("c1's view: " + view, 2, view.size()); 89 90 c1.send(m1); 91 c2.send(m2); 92 93 Util.sleep(1500); 94 List c1_list=r1.getMsgs(), c2_list=r2.getMsgs(); 95 System.out.println("c1: " + c1_list.size() + " msgs, c2: " + c2_list.size() + " msgs"); 96 assertNotNull(c1_list); 97 assertNotNull(c2_list); 98 assertEquals("cl_list: " + c1_list, 2, c1_list.size()); 99 assertEquals("c2_list: " + c2_list, 2, c2_list.size()); 100 assertTrue(c1_list.contains("message-1")); 101 assertTrue(c2_list.contains("message-1")); 102 assertTrue(c1_list.contains("message-2")); 103 assertTrue(c2_list.contains("message-2")); 104 } 105 106 107 public static Test suite() { 108 return new TestSuite(JoinTest.class); 109 } 110 111 public static void main(String [] args) { 112 junit.textui.TestRunner.run(JoinTest.suite()); 113 } 114 115 116 private static class MyReceiver extends ReceiverAdapter { 117 private final String name; 118 private final List <String > msgs; 119 120 public MyReceiver(String name) { 121 this.name=name; 122 msgs = Collections.synchronizedList(new ArrayList <String >()); 123 } 124 125 public List getMsgs() { 126 return msgs; 127 } 128 129 public void clear() {msgs.clear();} 130 131 public void receive(Message msg) { 132 String s=(String )msg.getObject(); 133 msgs.add(s); 134 System.out.println("[" + name + "] received " + s + " from " + msg.getSrc()); 135 } 136 137 public void viewAccepted(View new_view) { 138 System.out.println("[" + name + "] view: " + new_view); 139 } 140 } 141 } 142 | Popular Tags |