1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.AbstractMuleTestCase;
14
15 import java.io.IOException;
16 import java.io.InterruptedIOException;
17 import java.net.MalformedURLException;
18 import java.net.ServerSocket;
19 import java.net.Socket;
20 import java.net.SocketException;
21
22
23
24
25
26
27
28
29 public class TimedSocketTestCase extends AbstractMuleTestCase
30 {
31 private static final String REACHABLE_HOSTNAME = "127.0.0.1";
32 private static final int REACHABLE_PORT = 3333;
33 private static final String UNREACHABLE_HOSTNAME = "4.7.1.1";
34 private static final int UNREACHABLE_PORT = 4711;
35
36 private static final int TEST_TIMEOUT = 1000;
37 private static final int TEST_TIMEOUT_DELTA = 300;
38
39 public void testWorkingConnection() throws Exception
40 {
41 Socket client = null;
42 ServerSocket server = null;
43
44 try
45 {
46 server = new ServerSocket(REACHABLE_PORT);
47 assertNotNull(server);
48 client = TimedSocket.createSocket(REACHABLE_HOSTNAME, REACHABLE_PORT, TEST_TIMEOUT);
49 assertNotNull(client);
50 }
51 catch (InterruptedIOException iioe)
52 {
53 fail("Server timed out");
54 }
55 catch (SocketException se)
56 {
57 fail("Client/Server socket exception");
58 }
59 catch (IOException ioe)
60 {
61 fail("Client/Server network I/O error - " + ioe);
62 }
63 finally
64 {
65 try
66 {
67 if (client != null)
68 {
69 client.close();
70 }
71 }
72 catch (Exception ignore)
73 {
74 fail("Error closing client connection");
75 }
76 finally
77 {
78 try
79 {
80 if (server != null)
81 {
82 server.close();
83 }
84 }
85 catch (Exception ignore)
86 {
87 fail("Error closing server connection");
88 }
89 }
90 }
91 }
92
93 public void testConnectionTimeoutInterruptionOnReachableHostnameAndUnreachablePort() throws Exception
94 {
95 Socket client = null;
96
97 try
98 {
99 client = TimedSocket.createSocket(REACHABLE_HOSTNAME, UNREACHABLE_PORT, TEST_TIMEOUT);
100 fail("Socket exception is expected");
101 }
102 catch (InterruptedIOException iioe)
103 {
104 assertNull(client);
105 }
106 catch (MalformedURLException mue)
107 {
108 fail("Invalid URL");
109 }
110 catch (SocketException se)
111 {
112 assertNull(client);
113 }
114 catch (IOException ioe)
115 {
116 fail("Network I/O error - " + ioe);
117 }
118 finally
119 {
120 try
121 {
122 if (client != null)
123 {
124 client.close();
125 }
126 }
127 catch (Exception ignore)
128 {
129 fail("Error closing connection");
130 }
131 }
132 }
133
134 public void testConnectionTimeoutInterruptionOnUnreachableHostnameAndPost() throws Exception
135 {
136 Socket client = null;
137 long startTime = 0;
138 long stopTime = 0;
139 try
140 {
141 startTime = System.currentTimeMillis();
142 client = TimedSocket.createSocket(UNREACHABLE_HOSTNAME, UNREACHABLE_PORT, TEST_TIMEOUT);
143 fail("Timeout is expected");
144 }
145 catch (InterruptedIOException iioe)
146 {
147 stopTime = System.currentTimeMillis();
148 assertTrue("Remote host timeout was longer than expected. Expected: " + TEST_TIMEOUT + ", but was" + stopTime, (stopTime - startTime) > (TEST_TIMEOUT - TEST_TIMEOUT_DELTA));
149 }
150 catch (MalformedURLException mue)
151 {
152 fail("Invalid URL");
153 }
154 catch (SocketException se)
155 {
156 fail("Socket exception");
157 }
158 catch (IOException ioe)
159 {
160 fail("Network I/O error - " + ioe);
161 }
162 finally
163 {
164 try
165 {
166 if (client != null)
167 {
168 client.close();
169 }
170 }
171 catch (Exception ignore)
172 {
173 fail("Error closing connection");
174 }
175 }
176 }
177 }