|
6 | 6 | import static org.junit.Assert.fail; |
7 | 7 |
|
8 | 8 | import java.io.IOException; |
| 9 | +import java.net.ServerSocket; |
| 10 | +import java.net.SocketTimeoutException; |
9 | 11 | import java.net.URI; |
10 | 12 | import java.net.URISyntaxException; |
| 13 | +import java.time.Duration; |
| 14 | +import java.time.Instant; |
11 | 15 | import java.util.HashMap; |
12 | 16 | import java.util.Map; |
13 | 17 |
|
@@ -180,17 +184,20 @@ public void allowUrlWithNoDBAndNoPassword() { |
180 | 184 | } |
181 | 185 |
|
182 | 186 | @Test |
183 | | - public void uriWithDBindexShouldUseTimeout() throws URISyntaxException { |
184 | | - URI uri = new URI("redis://fakehost:6378/1"); |
185 | | - long startTime = System.nanoTime(); |
186 | | - try (Jedis j = new Jedis(uri, 5000)) { |
187 | | - j.ping(); |
188 | | - } catch (Exception ex) { |
189 | | - assertEquals(JedisConnectionException.class, ex.getClass()); |
190 | | - assertEquals(java.net.UnknownHostException.class, ex.getCause().getClass()); |
| 187 | + public void uriWithDBindexShouldUseTimeout() throws URISyntaxException, IOException { |
| 188 | + int fakePort = 6378; |
| 189 | + int timeoutMillis = 3250; |
| 190 | + int deltaMillis = 500; |
| 191 | + URI uri = new URI(String.format("redis://localhost:%d/1", fakePort)); |
| 192 | + Instant start = Instant.now(); |
| 193 | + |
| 194 | + try (ServerSocket server = new ServerSocket(fakePort); |
| 195 | + Jedis jedis = new Jedis(uri, timeoutMillis)) { |
| 196 | + fail("Jedis should fail to connect to a fake port"); |
| 197 | + } catch (JedisConnectionException ex) { |
| 198 | + assertEquals(SocketTimeoutException.class, ex.getCause().getClass()); |
| 199 | + assertEquals(timeoutMillis, Duration.between(start, Instant.now()).toMillis(), deltaMillis); |
191 | 200 | } |
192 | | - long stopTime = System.nanoTime(); |
193 | | - assertTrue(stopTime - startTime > 4000); |
194 | 201 | } |
195 | 202 |
|
196 | 203 | @Test |
|
0 commit comments