001: // $Id: ConnectionTableUnitTest.java,v 1.6 2005/09/20 14:31:48 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.TestCase;
006: import org.jgroups.Address;
007: import org.jgroups.blocks.ConnectionTable;
008:
009: /**
010: */
011: public class ConnectionTableUnitTest extends TestCase {
012: ConnectionTable ct1, ct2;
013: final int port1 = 5555, port2 = 6666;
014:
015: public ConnectionTableUnitTest(String name) {
016: super (name);
017: }
018:
019: protected void setUp() throws Exception {
020: super .setUp();
021: ct1 = new ConnectionTable(port1);
022: ct1.setUseSendQueues(false);
023: log("address of ct1: " + ct1.getLocalAddress());
024: ct2 = new ConnectionTable(port2);
025: ct2.setUseSendQueues(false);
026: log("address of ct2: " + ct2.getLocalAddress());
027: }
028:
029: public void tearDown() throws Exception {
030: super .tearDown();
031: if (ct1 != null) {
032: ct1.stop();
033: ct1 = null;
034: }
035: if (ct2 != null) {
036: ct2.stop();
037: ct2 = null;
038: }
039: }
040:
041: public void testSetup() {
042: assertNotSame(ct1.getLocalAddress(), ct2.getLocalAddress());
043: }
044:
045: public void testSendToNullReceiver() throws Exception {
046: byte[] data = new byte[0];
047: ct1.send(null, data, 0, data.length);
048: }
049:
050: public void testSendEmptyData() throws Exception {
051: byte[] data = new byte[0];
052: Address myself = ct1.getLocalAddress();
053: ct1.send(myself, data, 0, data.length);
054: }
055:
056: public void testSendNullData() throws Exception {
057: Address myself = ct1.getLocalAddress();
058: ct1.send(myself, null, 0, 0);
059: }
060:
061: public void testSendToSelf() throws Exception {
062: long NUM = 1000, total_time;
063: Address myself = ct1.getLocalAddress();
064: MyReceiver r = new MyReceiver(ct1, NUM, false);
065: byte[] data = new byte[] { 'b', 'e', 'l', 'a' };
066:
067: ct1.setReceiver(r);
068:
069: for (int i = 0; i < NUM; i++) {
070: ct1.send(myself, data, 0, 0);
071: }
072: log("sent " + NUM + " msgs");
073: r.waitForCompletion();
074: total_time = r.stop_time - r.start_time;
075: log("number expected=" + r.getNumExpected()
076: + ", number received=" + r.getNumReceived()
077: + ", total time=" + total_time + " ("
078: + (double) total_time / r.getNumReceived() + " ms/msg)");
079:
080: assertEquals(r.getNumExpected(), r.getNumReceived());
081: }
082:
083: public void testSendToOther() throws Exception {
084: long NUM = 1000, total_time;
085: Address other = ct2.getLocalAddress();
086: MyReceiver r = new MyReceiver(ct2, NUM, false);
087: byte[] data = new byte[] { 'b', 'e', 'l', 'a' };
088:
089: ct2.setReceiver(r);
090:
091: for (int i = 0; i < NUM; i++) {
092: ct1.send(other, data, 0, 0);
093: }
094: log("sent " + NUM + " msgs");
095: r.waitForCompletion();
096: total_time = r.stop_time - r.start_time;
097: log("number expected=" + r.getNumExpected()
098: + ", number received=" + r.getNumReceived()
099: + ", total time=" + total_time + " ("
100: + (double) total_time / r.getNumReceived() + " ms/msg)");
101:
102: assertEquals(r.getNumExpected(), r.getNumReceived());
103: }
104:
105: public void testSendToOtherGetResponse() throws Exception {
106: long NUM = 1000, total_time;
107: Address other = ct2.getLocalAddress();
108: MyReceiver r1 = new MyReceiver(ct1, NUM, false);
109: MyReceiver r2 = new MyReceiver(ct2, NUM, true); // send response
110: byte[] data = new byte[] { 'b', 'e', 'l', 'a' };
111:
112: ct1.setReceiver(r1);
113: ct2.setReceiver(r2);
114:
115: for (int i = 0; i < NUM; i++) {
116: ct1.send(other, data, 0, 0);
117: }
118: log("sent " + NUM + " msgs");
119: r1.waitForCompletion();
120: total_time = r1.stop_time - r1.start_time;
121: log("number expected=" + r1.getNumExpected()
122: + ", number received=" + r1.getNumReceived()
123: + ", total time=" + total_time + " ("
124: + (double) total_time / r1.getNumReceived()
125: + " ms/msg)");
126:
127: assertEquals(r1.getNumExpected(), r1.getNumReceived());
128: }
129:
130: void log(String msg) {
131: System.out.println("-- [" + Thread.currentThread() + "]: "
132: + msg);
133: }
134:
135: public static void main(String[] args) {
136: String[] testCaseName = { ConnectionTableUnitTest.class
137: .getName() };
138: junit.textui.TestRunner.main(testCaseName);
139: }
140:
141: class MyReceiver implements ConnectionTable.Receiver {
142: long num_expected = 0, num_received = 0, start_time = 0,
143: stop_time = 0;
144: boolean done = false, send_response = false;
145: long modulo;
146: ConnectionTable ct;
147:
148: MyReceiver(ConnectionTable ct, long num_expected,
149: boolean send_response) {
150: this .ct = ct;
151: this .num_expected = num_expected;
152: this .send_response = send_response;
153: start_time = System.currentTimeMillis();
154: modulo = num_expected / 10;
155: }
156:
157: public long getNumReceived() {
158: return num_received;
159: }
160:
161: public long getNumExpected() {
162: return num_expected;
163: }
164:
165: public void receive(Address sender, byte[] data, int offset,
166: 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: } catch (Exception e) {
176: e.printStackTrace();
177: }
178: }
179: }
180: if (num_received >= num_expected) {
181: synchronized (this ) {
182: if (!done) {
183: done = true;
184: stop_time = System.currentTimeMillis();
185: notifyAll();
186: }
187: }
188: }
189: }
190:
191: public void waitForCompletion() {
192: synchronized (this ) {
193: while (!done) {
194: try {
195: wait();
196: } catch (InterruptedException e) {
197: }
198: }
199: }
200: }
201:
202: }
203:
204: }
|