KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FragTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import org.jgroups.Address;
10 import org.jgroups.Event;
11 import org.jgroups.Message;
12 import org.jgroups.debug.ProtocolTester;
13 import org.jgroups.stack.IpAddress;
14 import org.jgroups.stack.Protocol;
15 import org.jgroups.util.Util;
16
17
18 /**
19  * Class to test FRAG protocol. It uses ProtocolTester to assemble a minimal stack which only consists of
20  * FRAG and LOOPBACK (messages are immediately resent up the stack). Sends NUM_MSGS with MSG_SIZE size down
21  * the stack, they should be received as well.
22  *
23  * @author Bela Ban
24  */

25 public class FragTest extends TestCase {
26     public static final long NUM_MSGS=10;
27     public static final int MSG_SIZE=100000;
28     public static final int FRAG_SIZE=24000;
29
30
31     public FragTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36
37
38     private Message createBigMessage(int size) {
39         byte[] buf=new byte[size];
40         for(int i=0; i < buf.length; i++) buf[i]=(byte)'x';
41         return new Message(null, null, buf);
42     }
43
44
45     public void test0() throws Exception JavaDoc {
46         Object JavaDoc mutex=new Object JavaDoc();
47         FragReceiver frag_receiver=new FragReceiver(this, mutex);
48         ProtocolTester t=new ProtocolTester("FRAG(frag_size=" + FRAG_SIZE + ')', frag_receiver);
49         Message big_msg;
50         IpAddress local_addr=new IpAddress(5555);
51
52         System.out.println("\nProtocol for protocol tester: " + t.getProtocolSpec() + '\n');
53
54         synchronized(mutex) {
55             for(int i=0; i < NUM_MSGS; i++) {
56                 big_msg=createBigMessage(MSG_SIZE);
57                 big_msg.setSrc(local_addr);
58                 System.out.println("sending msg #" + i + " [" + big_msg.getLength() + " bytes]");
59                 frag_receiver.down(new Event(Event.MSG, big_msg));
60                 Util.sleep(10);
61             }
62         }
63         t.stop();
64     }
65
66
67     public static Test suite() {
68         return new TestSuite(FragTest.class);
69     }
70
71     public static void main(String JavaDoc[] args) {
72         junit.textui.TestRunner.run(suite());
73     }
74
75
76     private static class FragReceiver extends Protocol {
77         long num_msgs=0;
78         FragTest t=null;
79         Object JavaDoc mut=null;
80
81         FragReceiver(FragTest t, Object JavaDoc mut) {
82             this.t=t;
83             this.mut=mut;
84         }
85
86         public String JavaDoc getName() {
87             return "FragReceiver";
88         }
89
90
91         public Object JavaDoc up(Event evt) {
92             Message msg=null;
93             Address sender;
94
95             if(evt == null || evt.getType() != Event.MSG)
96                 return null;
97             msg=(Message)evt.getArg();
98             sender=msg.getSrc();
99             if(sender == null) {
100                 log.error("FragTest.FragReceiver.up(): sender is null; discarding msg");
101                 return null;
102             }
103             System.out.println("Received msg from " + sender + " [" + msg.getLength() + " bytes]");
104             return null;
105         }
106
107     }
108
109
110 }
111
112
113
Popular Tags