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