KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > UNICAST_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.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 JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * Tests the UNICAST protocol for OOB msgs, tests http://jira.jboss.com/jira/browse/JGRP-377
19  * @author Bela Ban
20  * @version $Id: UNICAST_OOB_Test.java,v 1.4 2006/12/13 12:02:42 belaban Exp $
21  */

22 public class UNICAST_OOB_Test extends TestCase {
23     JChannel ch1, ch2;
24     final String JavaDoc props="udp.xml";
25
26     public UNICAST_OOB_Test(String JavaDoc name) {
27         super(name);
28     }
29
30     public void setUp() throws Exception JavaDoc {
31         super.setUp();
32         ch1=new JChannel(props);
33         ch2=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         super.tearDown();
42     }
43
44
45     public void testRegularMessages() throws Exception JavaDoc {
46         sendMessages(false);
47     }
48
49     public void testOutOfBandMessages() throws Exception JavaDoc {
50         sendMessages(true);
51     }
52
53
54     /**
55      */

56     private void sendMessages(boolean oob) throws Exception JavaDoc {
57         DISCARD_PAYLOAD prot1=new DISCARD_PAYLOAD();
58         MyReceiver receiver=new MyReceiver();
59         ch2.setReceiver(receiver);
60
61         // the second channel will discard the unicast messages with seqno #3 two times, the let them pass
62
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 JavaDoc(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); // wait until retransmission of seqno #3 happens, so that 4 and 5 are received as well
79

80         List JavaDoc seqnos=receiver.getSeqnos();
81         System.out.println("sequence numbers: " + seqnos);
82
83         // expected sequence is: 1 2 4 3 5 ! Reason: 4 is sent OOB, does *not* wait until 3 has been retransmitted !!
84
Long JavaDoc[] expected_seqnos=oob?
85                 new Long JavaDoc[]{new Long JavaDoc(1), new Long JavaDoc(2), new Long JavaDoc(4), new Long JavaDoc(3), new Long JavaDoc(5)} : // OOB
86
new Long JavaDoc[]{new Long JavaDoc(1), new Long JavaDoc(2), new Long JavaDoc(3), new Long JavaDoc(4), new Long JavaDoc(5)}; // regular
87
for(int i=0; i < expected_seqnos.length; i++) {
88             Long JavaDoc expected_seqno=expected_seqnos[i];
89             Long JavaDoc received_seqno=(Long JavaDoc)seqnos.get(i);
90             assertEquals(expected_seqno, received_seqno);
91         }
92     }
93
94
95
96
97     public static class MyReceiver extends ReceiverAdapter {
98         /** List<Long> of unicast sequence numbers */
99         List JavaDoc seqnos=new LinkedList JavaDoc();
100
101         public MyReceiver() {
102         }
103
104         public List JavaDoc getSeqnos() {
105             return seqnos;
106         }
107
108         public void receive(Message msg) {
109             if(msg != null) {
110                 Long JavaDoc num=(Long JavaDoc)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 JavaDoc[] args) {
121         junit.textui.TestRunner.run(UNICAST_OOB_Test.suite());
122     }
123 }
124
Popular Tags