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