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