KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > NAKACK_OOB_Test


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 JavaDoc;
14 import java.util.List JavaDoc;
15
16 /**
17  * Tests the NAKACK protocol for OOB msgs, tests http://jira.jboss.com/jira/browse/JGRP-379
18  * @author Bela Ban
19  * @version $Id: NAKACK_OOB_Test.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
20  */

21 public class NAKACK_OOB_Test extends TestCase {
22     JChannel ch1, ch2, ch3;
23     final String JavaDoc props="udp.xml";
24
25     public NAKACK_OOB_Test(String JavaDoc name) {
26         super(name);
27     }
28
29     public void setUp() throws Exception JavaDoc {
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 JavaDoc {
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     /**
48      * Tests http://jira.jboss.com/jira/browse/JGRP-379: we send 1, 2, 3, 4(OOB) and 5 to the cluster.
49      * Message with seqno 3 is discarded two times, so retransmission will make the receivers receive it *after* 4.
50      * Because 4 is marked as OOB, we will deliver 4 *immediately* (before 3 and 5), so the sequence of the messages
51      * at the receivers is 1 - 2 - 4 -3 - 5.
52      * Note that OOB messages *destroys* FIFO ordering (or whatever ordering properties are set) !
53      * @throws Exception
54      */

55     public void testOutOfBandMessages() throws Exception JavaDoc {
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         // all channels will discard messages with seqno #3 two times, the let them pass
64
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 JavaDoc(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); // wait until retransmission of seqno #3 happens, so that 4 and 5 are received as well
84

85         List JavaDoc seqnos1=receiver1.getSeqnos();
86         List JavaDoc seqnos2=receiver2.getSeqnos();
87         List JavaDoc 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         // expected sequence is: 1 2 4 3 5 ! Reason: 4 is sent OOB, does *not* wait until 3 has been retransmitted !!
95
Long JavaDoc[] expected_seqnos=new Long JavaDoc[]{new Long JavaDoc(1), new Long JavaDoc(2), new Long JavaDoc(4), new Long JavaDoc(3), new Long JavaDoc(5)};
96         for(int i=0; i < expected_seqnos.length; i++) {
97             Long JavaDoc expected_seqno=expected_seqnos[i];
98
99             Long JavaDoc received_seqno=(Long JavaDoc)seqnos1.get(i);
100             assertEquals(expected_seqno, received_seqno);
101             received_seqno=(Long JavaDoc)seqnos2.get(i);
102             assertEquals(expected_seqno, received_seqno);
103             received_seqno=(Long JavaDoc)seqnos3.get(i);
104             assertEquals(expected_seqno, received_seqno);
105         }
106     }
107
108
109     public static class MyReceiver extends ReceiverAdapter {
110         /** List<Long> of unicast sequence numbers */
111         List JavaDoc seqnos=new LinkedList JavaDoc();
112
113         public MyReceiver() {
114         }
115
116         public List JavaDoc getSeqnos() {
117             return seqnos;
118         }
119
120         public void receive(Message msg) {
121             if(msg != null) {
122                 Long JavaDoc num=(Long JavaDoc)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 JavaDoc[] args) {
133         junit.textui.TestRunner.run(NAKACK_OOB_Test.suite());
134     }
135 }
136
Popular Tags