1 package org.jgroups.tests; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jgroups.Address; 7 import org.jgroups.JChannel; 8 import org.jgroups.Message; 9 import org.jgroups.ReceiverAdapter; 10 import org.jgroups.protocols.DISCARD_PAYLOAD; 11 import org.jgroups.stack.ProtocolStack; 12 import org.jgroups.util.Util; 13 14 import java.util.LinkedList ; 15 import java.util.List ; 16 17 22 public class UNICAST_OOB_Test extends TestCase { 23 JChannel ch1, ch2; 24 final String props="udp.xml"; 25 26 public UNICAST_OOB_Test(String name) { 27 super(name); 28 } 29 30 public void setUp() throws Exception { 31 super.setUp(); 32 ch1=new JChannel(props); 33 ch2=new JChannel(props); 34 } 35 36 public void tearDown() throws Exception { 37 if(ch1 != null) 38 ch1.close(); 39 if(ch2 != null) 40 ch2.close(); 41 super.tearDown(); 42 } 43 44 45 public void testRegularMessages() throws Exception { 46 sendMessages(false); 47 } 48 49 public void testOutOfBandMessages() throws Exception { 50 sendMessages(true); 51 } 52 53 54 56 private void sendMessages(boolean oob) throws Exception { 57 DISCARD_PAYLOAD prot1=new DISCARD_PAYLOAD(); 58 MyReceiver receiver=new MyReceiver(); 59 ch2.setReceiver(receiver); 60 61 ch2.getProtocolStack().insertProtocol(prot1, ProtocolStack.BELOW, "UNICAST"); 63 64 ch1.connect("x"); 65 ch2.connect("x"); 66 assertEquals(2, ch2.getView().getMembers().size()); 67 68 Address dest=ch2.getLocalAddress(); 69 for(int i=1; i <=5; i++) { 70 Message msg=new Message(dest, null, new Long (i)); 71 if(i == 4 && oob) 72 msg.setFlag(Message.OOB); 73 System.out.println("-- sending message #" + i); 74 ch1.send(msg); 75 Util.sleep(100); 76 } 77 78 Util.sleep(5000); 80 List seqnos=receiver.getSeqnos(); 81 System.out.println("sequence numbers: " + seqnos); 82 83 Long [] expected_seqnos=oob? 85 new Long []{new Long (1), new Long (2), new Long (4), new Long (3), new Long (5)} : new Long []{new Long (1), new Long (2), new Long (3), new Long (4), new Long (5)}; for(int i=0; i < expected_seqnos.length; i++) { 88 Long expected_seqno=expected_seqnos[i]; 89 Long received_seqno=(Long )seqnos.get(i); 90 assertEquals(expected_seqno, received_seqno); 91 } 92 } 93 94 95 96 97 public static class MyReceiver extends ReceiverAdapter { 98 99 List seqnos=new LinkedList (); 100 101 public MyReceiver() { 102 } 103 104 public List getSeqnos() { 105 return seqnos; 106 } 107 108 public void receive(Message msg) { 109 if(msg != null) { 110 Long num=(Long )msg.getObject(); 111 seqnos.add(num); 112 } 113 } 114 } 115 116 public static Test suite() { 117 return new TestSuite(UNICAST_OOB_Test.class); 118 } 119 120 public static void main(String [] args) { 121 junit.textui.TestRunner.run(UNICAST_OOB_Test.suite()); 122 } 123 } 124 | Popular Tags |