KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > stack > LazyRoutingClientTest


1 // $Id: LazyRoutingClientTest.java,v 1.4 2004/07/23 02:29:00 belaban Exp $
2

3 package org.jgroups.tests.stack;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.stack.GossipRouter;
9 import org.jgroups.stack.IpAddress;
10 import org.jgroups.util.Util;
11
12 import java.io.DataInputStream JavaDoc;
13 import java.io.DataOutputStream JavaDoc;
14 import java.io.EOFException JavaDoc;
15 import java.net.Socket JavaDoc;
16 import java.net.SocketException JavaDoc;
17
18
19 /**
20  * Tests a special routing protocol case when a routing client is lazy to send
21  * its request type and group identification. In this situation, the server
22  * should close connection after a timeout. The GossipRouter in question is a
23  * post-2.2.1 one.
24  *
25  * @author Ovidiu Feodorov <ovidiuf@users.sourceforge.net>
26  * @version $Revision: 1.4 $
27  * @since 2.2.1
28  */

29 public class LazyRoutingClientTest extends TestCase {
30
31     private int routerPort=-1;
32
33     private long routingClientReplyTimeout=30000;
34
35     public LazyRoutingClientTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public void setUp() throws Exception JavaDoc {
40         super.setUp();
41         routerPort=
42                 Utilities.startGossipRouter(GossipRouter.EXPIRY_TIME,
43                         GossipRouter.GOSSIP_REQUEST_TIMEOUT,
44                         routingClientReplyTimeout);
45     }
46
47     public void tearDown() throws Exception JavaDoc {
48         super.tearDown();
49         Utilities.stopGossipRouter();
50     }
51
52
53     /**
54      * Tests the situation when a routing client is lazy to send its
55      * request type and group identification. In this situation, the server
56      * should close connection after timeout.
57      */

58     public void testLazyClient() throws Exception JavaDoc {
59
60         int len;
61         byte[] buffer;
62
63         Socket JavaDoc s=new Socket JavaDoc("localhost", routerPort);
64         DataInputStream JavaDoc dis=new DataInputStream JavaDoc(s.getInputStream());
65         DataOutputStream JavaDoc dos=new DataOutputStream JavaDoc(s.getOutputStream());
66
67         // read the IpAddress sent by GossipRouter
68
len=dis.readInt();
69         buffer=new byte[len];
70         dis.readFully(buffer, 0, len);
71         IpAddress localAddr=(IpAddress)Util.objectFromByteBuffer(buffer);
72         assertEquals(localAddr.getIpAddress(), s.getLocalAddress());
73         assertEquals(localAddr.getPort(), s.getLocalPort());
74
75         // send GET request later than GossipRouter's routingClientReplyTimeout
76
Thread.sleep(routingClientReplyTimeout + 500);
77
78         // I expect the socket to be closed by now. I test this in a different
79
// way on Java 1.3 and Java 1.4
80

81         Exception JavaDoc expected13=null;
82         Exception JavaDoc expected14=null;
83
84         try {
85             dos.writeInt(GossipRouter.GET);
86             dos.writeUTF("testgroup");
87         }
88         catch(Exception JavaDoc e) {
89             // on Java 1.4, writing on a closed socket fails
90
expected14=e;
91         }
92
93         try {
94             dis.readInt();
95         }
96         catch(Exception JavaDoc e) {
97             // on Java 1.3, trying to read from the closet socket fails
98
expected13=e;
99         }
100
101         assertTrue(expected14 instanceof SocketException JavaDoc ||
102                 expected13 instanceof EOFException JavaDoc);
103
104         dis.close();
105         dos.close();
106         s.close();
107     }
108
109
110     public static Test suite() {
111         TestSuite s=new TestSuite(LazyRoutingClientTest.class);
112         return s;
113     }
114
115     public static void main(String JavaDoc[] args) {
116         junit.textui.TestRunner.run(suite());
117         System.exit(0);
118     }
119
120 }
121
Popular Tags