Skip to content

Commit 82252b5

Browse files
committed
modify uriWithDBindexShouldUseTimeout test
1 parent 1af8844 commit 82252b5

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/test/java/redis/clients/jedis/tests/JedisTest.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import static org.junit.Assert.fail;
77

88
import java.io.IOException;
9+
import java.net.ServerSocket;
10+
import java.net.SocketTimeoutException;
911
import java.net.URI;
1012
import java.net.URISyntaxException;
13+
import java.time.Duration;
14+
import java.time.Instant;
1115
import java.util.HashMap;
1216
import java.util.Map;
1317

@@ -180,17 +184,20 @@ public void allowUrlWithNoDBAndNoPassword() {
180184
}
181185

182186
@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);
191200
}
192-
long stopTime = System.nanoTime();
193-
assertTrue(stopTime - startTime > 4000);
194201
}
195202

196203
@Test

0 commit comments

Comments
 (0)