01: package org.jgroups.tests;
02:
03: import junit.framework.TestCase;
04: import org.jgroups.*;
05:
06: /**
07: * Tests unicasts to self (loopback of transport protocol)
08: * @author Bela Ban Dec 31 2003
09: * @version $Id: UnicastLoopbackTest.java,v 1.7 2005/01/28 22:08:07 belaban Exp $
10: */
11: public class UnicastLoopbackTest extends TestCase {
12: JChannel channel = null;
13:
14: public UnicastLoopbackTest(String name) {
15: super (name);
16: }
17:
18: protected void setUp() throws Exception {
19: super .setUp();
20: channel = new JChannel();
21: channel.connect("demo-group");
22: }
23:
24: protected void tearDown() throws Exception {
25: super .tearDown();
26: if (channel != null) {
27: channel.close();
28: channel = null;
29: }
30: }
31:
32: public void testUnicastMsgs() throws ChannelClosedException,
33: ChannelNotConnectedException, TimeoutException {
34: int NUM = 1000;
35: Address local_addr = channel.getLocalAddress();
36: for (int i = 1; i <= NUM; i++) {
37: channel.send(new Message(local_addr, null, new Integer(i)));
38: // try {
39: // Thread.sleep(1);
40: // }
41: // catch(InterruptedException e) {
42: // e.printStackTrace();
43: // }
44: if (i % 100 == 0)
45: System.out.println("-- sent " + i);
46: }
47: int received = 0;
48: while (received < NUM) {
49: Object o = channel.receive(0);
50: if (o instanceof Message) {
51: Message m = (Message) o;
52: Integer num = (Integer) m.getObject();
53: received++;
54: if (num.intValue() % 100 == 0)
55: System.out.println("-- received " + num);
56: }
57: }
58: assertEquals(NUM, received);
59: }
60:
61: public static void main(String[] args) {
62: String[] testCaseName = { UnicastLoopbackTest.class.getName() };
63: junit.textui.TestRunner.main(testCaseName);
64: }
65: }
|