KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.jgroups.tests;
4
5
6 import junit.framework.TestCase;
7 import org.jgroups.Address;
8 import org.jgroups.blocks.ConnectionTable;
9
10
11 /**
12  */

13 public class ConnectionTableUnitTest extends TestCase {
14     ConnectionTable ct1, ct2;
15     final int port1=5555, port2=6666;
16
17
18
19     public ConnectionTableUnitTest(String JavaDoc name) {
20         super(name);
21     }
22
23     protected void setUp() throws Exception JavaDoc {
24         super.setUp();
25         ct1=new ConnectionTable(port1);
26         ct1.setUseSendQueues(false);
27         log("address of ct1: " + ct1.getLocalAddress());
28         ct2=new ConnectionTable(port2);
29         ct2.setUseSendQueues(false);
30         log("address of ct2: " + ct2.getLocalAddress());
31     }
32
33     public void tearDown() throws Exception JavaDoc {
34         super.tearDown();
35         if(ct1 != null) {
36             ct1.stop();
37             ct1=null;
38         }
39         if(ct2 != null) {
40             ct2.stop();
41             ct2=null;
42         }
43     }
44
45     public void testSetup() {
46         assertNotSame(ct1.getLocalAddress(), ct2.getLocalAddress());
47     }
48
49     public void testSendToNullReceiver() throws Exception JavaDoc {
50         byte[] data=new byte[0];
51         ct1.send(null, data, 0, data.length);
52     }
53
54     public void testSendEmptyData() throws Exception JavaDoc {
55         byte[] data=new byte[0];
56         Address myself=ct1.getLocalAddress();
57         ct1.send(myself, data, 0, data.length);
58     }
59
60     public void testSendNullData() throws Exception JavaDoc {
61         Address myself=ct1.getLocalAddress();
62         ct1.send(myself, null, 0, 0);
63     }
64
65
66     public void testSendToSelf() throws Exception JavaDoc {
67         long NUM=1000, total_time;
68         Address myself=ct1.getLocalAddress();
69         MyReceiver r=new MyReceiver(ct1, NUM, false);
70         byte[] data=new byte[] {'b', 'e', 'l', 'a'};
71
72         ct1.setReceiver(r);
73
74         for(int i=0; i < NUM; i++) {
75             ct1.send(myself, data, 0, 0);
76         }
77         log("sent " + NUM + " msgs");
78         r.waitForCompletion();
79         total_time=r.stop_time - r.start_time;
80         log("number expected=" + r.getNumExpected() + ", number received=" + r.getNumReceived() +
81             ", total time=" + total_time + " (" + (double)total_time / r.getNumReceived() + " ms/msg)");
82
83         assertEquals(r.getNumExpected(), r.getNumReceived());
84     }
85
86     public void testSendToOther() throws Exception JavaDoc {
87         long NUM=1000, total_time;
88         Address other=ct2.getLocalAddress();
89         MyReceiver r=new MyReceiver(ct2, NUM, false);
90         byte[] data=new byte[] {'b', 'e', 'l', 'a'};
91
92         ct2.setReceiver(r);
93
94         for(int i=0; i < NUM; i++) {
95             ct1.send(other, data, 0, 0);
96         }
97         log("sent " + NUM + " msgs");
98         r.waitForCompletion();
99         total_time=r.stop_time - r.start_time;
100         log("number expected=" + r.getNumExpected() + ", number received=" + r.getNumReceived() +
101             ", total time=" + total_time + " (" + (double)total_time / r.getNumReceived() + " ms/msg)");
102
103         assertEquals(r.getNumExpected(), r.getNumReceived());
104     }
105
106
107     public void testSendToOtherGetResponse() throws Exception JavaDoc {
108         long NUM=1000, total_time;
109         Address other=ct2.getLocalAddress();
110         MyReceiver r1=new MyReceiver(ct1, NUM, false);
111         MyReceiver r2=new MyReceiver(ct2, NUM, true); // send response
112
byte[] data=new byte[] {'b', 'e', 'l', 'a'};
113
114         ct1.setReceiver(r1);
115         ct2.setReceiver(r2);
116
117         for(int i=0; i < NUM; i++) {
118             ct1.send(other, data, 0, 0);
119         }
120         log("sent " + NUM + " msgs");
121         r1.waitForCompletion();
122         total_time=r1.stop_time - r1.start_time;
123         log("number expected=" + r1.getNumExpected() + ", number received=" + r1.getNumReceived() +
124             ", total time=" + total_time + " (" + (double)total_time / r1.getNumReceived() + " ms/msg)");
125
126         assertEquals(r1.getNumExpected(), r1.getNumReceived());
127     }
128
129
130     void log(String JavaDoc msg) {
131         System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
132     }
133
134
135     public static void main(String JavaDoc[] args) {
136         String JavaDoc[] testCaseName={ConnectionTableUnitTest.class.getName()};
137         junit.textui.TestRunner.main(testCaseName);
138     }
139
140
141
142     class MyReceiver implements ConnectionTable.Receiver {
143         long num_expected=0, num_received=0, start_time=0, stop_time=0;
144         boolean done=false, send_response=false;
145         long modulo;
146         ConnectionTable ct;
147
148         MyReceiver(ConnectionTable ct, long num_expected, boolean send_response) {
149             this.ct=ct;
150             this.num_expected=num_expected;
151             this.send_response=send_response;
152             start_time=System.currentTimeMillis();
153             modulo=num_expected / 10;
154         }
155
156
157         public long getNumReceived() {
158             return num_received;
159         }
160
161         public long getNumExpected() {
162             return num_expected;
163         }
164
165
166         public void receive(Address sender, byte[] data, int offset, int length) {
167             num_received++;
168             if(num_received % modulo == 0)
169                 log("received msg# " + num_received);
170             if(send_response) {
171                 if(ct != null) {
172                     try {
173                         byte[] rsp=new byte[0];
174                         ct.send(sender, rsp, 0, 0);
175                     }
176                     catch(Exception JavaDoc e) {
177                         e.printStackTrace();
178                     }
179                 }
180             }
181             if(num_received >= num_expected) {
182                 synchronized(this) {
183                     if(!done) {
184                         done=true;
185                         stop_time=System.currentTimeMillis();
186                         notifyAll();
187                     }
188                 }
189             }
190         }
191
192
193         public void waitForCompletion() {
194             synchronized(this) {
195                 while(!done) {
196                     try {
197                         wait();
198                     }
199                     catch(InterruptedException JavaDoc e) {
200                     }
201                 }
202             }
203         }
204
205
206     }
207
208 }
209
Popular Tags