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.JChannel; 7 import org.jgroups.Message; 8 import org.jgroups.ReceiverAdapter; 9 import org.jgroups.protocols.DISCARD_PAYLOAD; 10 import org.jgroups.stack.ProtocolStack; 11 import org.jgroups.util.Util; 12 13 import java.util.LinkedList ; 14 import java.util.List ; 15 16 21 public class NAKACK_OOB_Test extends TestCase { 22 JChannel ch1, ch2, ch3; 23 final String props="udp.xml"; 24 25 public NAKACK_OOB_Test(String name) { 26 super(name); 27 } 28 29 public void setUp() throws Exception { 30 super.setUp(); 31 ch1=new JChannel(props); 32 ch2=new JChannel(props); 33 ch3=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 if(ch3 != null) 42 ch3.close(); 43 super.tearDown(); 44 } 45 46 47 55 public void testOutOfBandMessages() throws Exception { 56 NAKACK_OOB_Test.MyReceiver receiver1=new NAKACK_OOB_Test.MyReceiver(); 57 NAKACK_OOB_Test.MyReceiver receiver2=new NAKACK_OOB_Test.MyReceiver(); 58 NAKACK_OOB_Test.MyReceiver receiver3=new NAKACK_OOB_Test.MyReceiver(); 59 ch1.setReceiver(receiver1); 60 ch2.setReceiver(receiver2); 61 ch3.setReceiver(receiver3); 62 63 ch1.getProtocolStack().insertProtocol(new DISCARD_PAYLOAD(), ProtocolStack.BELOW, "NAKACK"); 65 ch2.getProtocolStack().insertProtocol(new DISCARD_PAYLOAD(), ProtocolStack.BELOW, "NAKACK"); 66 ch3.getProtocolStack().insertProtocol(new DISCARD_PAYLOAD(), ProtocolStack.BELOW, "NAKACK"); 67 68 ch1.connect("x"); 69 ch2.connect("x"); 70 ch3.connect("x"); 71 72 assertEquals(3, ch3.getView().getMembers().size()); 73 74 for(int i=1; i <=5; i++) { 75 Message msg=new Message(null, null, new Long (i)); 76 if(i == 4) 77 msg.setFlag(Message.OOB); 78 System.out.println("-- sending message #" + i); 79 ch1.send(msg); 80 Util.sleep(100); 81 } 82 83 Util.sleep(5000); 85 List seqnos1=receiver1.getSeqnos(); 86 List seqnos2=receiver2.getSeqnos(); 87 List seqnos3=receiver3.getSeqnos(); 88 89 System.out.println("sequence numbers:"); 90 System.out.println("ch1: " + seqnos1); 91 System.out.println("ch2: " + seqnos2); 92 System.out.println("ch3: " + seqnos3); 93 94 Long [] expected_seqnos=new Long []{new Long (1), new Long (2), new Long (4), new Long (3), new Long (5)}; 96 for(int i=0; i < expected_seqnos.length; i++) { 97 Long expected_seqno=expected_seqnos[i]; 98 99 Long received_seqno=(Long )seqnos1.get(i); 100 assertEquals(expected_seqno, received_seqno); 101 received_seqno=(Long )seqnos2.get(i); 102 assertEquals(expected_seqno, received_seqno); 103 received_seqno=(Long )seqnos3.get(i); 104 assertEquals(expected_seqno, received_seqno); 105 } 106 } 107 108 109 public static class MyReceiver extends ReceiverAdapter { 110 111 List seqnos=new LinkedList (); 112 113 public MyReceiver() { 114 } 115 116 public List getSeqnos() { 117 return seqnos; 118 } 119 120 public void receive(Message msg) { 121 if(msg != null) { 122 Long num=(Long )msg.getObject(); 123 seqnos.add(num); 124 } 125 } 126 } 127 128 public static Test suite() { 129 return new TestSuite(NAKACK_OOB_Test.class); 130 } 131 132 public static void main(String [] args) { 133 junit.textui.TestRunner.run(NAKACK_OOB_Test.suite()); 134 } 135 } 136 | Popular Tags |