KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.tests;
2
3 import junit.framework.TestCase;
4 import org.jgroups.*;
5
6 /**
7  * Tests unicasts to self (loopback of transport protocol)
8  * @author Bela Ban Dec 31 2003
9  * @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
15     public UnicastLoopbackTest(String JavaDoc name) {
16         super(name);
17     }
18
19     protected void setUp() throws Exception JavaDoc {
20         super.setUp();
21         channel=new JChannel();
22         channel.connect("demo-group");
23     }
24
25     protected void tearDown() throws Exception JavaDoc {
26         super.tearDown();
27         if(channel != null) {
28             channel.close();
29             channel=null;
30         }
31     }
32
33
34     public void testUnicastMsgs() throws ChannelClosedException, ChannelNotConnectedException, TimeoutException {
35         int NUM=1000;
36         Address local_addr=channel.getLocalAddress();
37         for(int i=1; i <= NUM; i++) {
38             channel.send(new Message(local_addr, null, new Integer JavaDoc(i)));
39 // try {
40
// Thread.sleep(1);
41
// }
42
// catch(InterruptedException e) {
43
// e.printStackTrace();
44
// }
45
if(i % 100 == 0)
46                 System.out.println("-- sent " + i);
47         }
48         int received=0;
49         while(received < NUM) {
50             Object JavaDoc o=channel.receive(0);
51             if(o instanceof Message) {
52                 Message m=(Message)o;
53                 Integer JavaDoc num=(Integer JavaDoc)m.getObject();
54                 received++;
55                 if(num.intValue() % 100 == 0)
56                     System.out.println("-- received " + num);
57             }
58         }
59         assertEquals(NUM, received);
60     }
61
62     public static void main(String JavaDoc[] args) {
63         String JavaDoc[] testCaseName={UnicastLoopbackTest.class.getName()};
64         junit.textui.TestRunner.main(testCaseName);
65     }
66 }
67
Popular Tags