KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: InexistentRouterTest.java,v 1.4 2004/07/05 14:15:04 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.ChannelException;
9 import org.jgroups.JChannel;
10
11 import java.io.IOException JavaDoc;
12 import java.io.InterruptedIOException JavaDoc;
13 import java.net.ServerSocket JavaDoc;
14 import java.net.Socket JavaDoc;
15
16
17 /**
18  * The test was relevant for version 2.0.5. Since 2.0.6, the way JChannel
19  * handles connect() changed, and the test stays here for historical reasons
20  * only.
21  *
22  * @author Ovidiu Feodorov <ovidiu@feodorov.com>
23  * @version $Revision: 1.4 $
24  */

25 public class InexistentRouterTest extends TestCase {
26     private int SERVER_SOCKET_TIMEOUT=20000;
27
28     private int port;
29     private ServerSocket JavaDoc ss;
30     private JChannel channel;
31     private String JavaDoc action;
32
33     public InexistentRouterTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public void setUp() throws Exception JavaDoc {
38
39         super.setUp();
40
41         // find an unused port
42
ss=new ServerSocket JavaDoc(0);
43         port=ss.getLocalPort();
44         ss.close();
45
46         debug("ROUTER PORT: " + port);
47         String JavaDoc props="TUNNEL(router_host=127.0.0.1;router_port=" + port + ')';
48         channel=new JChannel(props);
49
50     }
51
52     public void tearDown() throws Exception JavaDoc {
53
54         super.tearDown();
55         ss.close();
56         channel.close();
57     }
58
59
60     /**
61      * The channel is configured with a router port on which nobody is
62      * listening. The test will connect the channel, will wait for the connect
63      * to fail and will start a server socket on the port the router was
64      * supposed to listen on. If the server socket gets a connection attempt,
65      * the test fails - that means the TUNNEL receiver thread is still trying
66      * to reconnect the RouterStub. If the server socket doesn't receive
67      * anything in 20 secs (RouterStub.RECONNECT_TIMEOUT is 5 secs by default)
68      * the test is considered successful.
69      */

70     public void testReconnect() throws Exception JavaDoc {
71
72         try {
73             channel.connect("GROUP");
74         }
75         catch(ChannelException e) {
76         }
77
78         startServerSocket();
79
80     }
81
82
83     private void startServerSocket() {
84
85         try {
86             debug("Starting server on " + port);
87             ss=new ServerSocket JavaDoc(port);
88             ss.setSoTimeout(SERVER_SOCKET_TIMEOUT);
89             Socket JavaDoc s=ss.accept();
90             fail("The Server Socket received a connection attempt.");
91         }
92         catch(InterruptedIOException JavaDoc e) {
93             // timeout, no connection - ok
94
}
95         catch(IOException JavaDoc e) {
96             debug("Socket exception:", e);
97             fail("Not suposed to get exception here.");
98         }
99     }
100
101
102     private void debug(String JavaDoc msg) {
103         System.out.println(msg);
104     }
105
106     private void debug(String JavaDoc msg, Throwable JavaDoc t) {
107         System.out.println(msg + ", exception=" + t);
108     }
109
110
111     public static Test suite() {
112         TestSuite s=new TestSuite(InexistentRouterTest.class);
113         return s;
114     }
115
116     public static void main(String JavaDoc[] args) {
117         junit.textui.TestRunner.run(suite());
118     }
119
120
121 }
122
Popular Tags