|
29 | 29 | import io.r2dbc.spi.Result; |
30 | 30 | import oracle.jdbc.OracleConnection; |
31 | 31 | import oracle.jdbc.datasource.OracleDataSource; |
32 | | -import oracle.jdbc.driver.DatabaseError; |
33 | | -import oracle.r2dbc.test.DatabaseConfig; |
34 | 32 | import org.junit.jupiter.api.Test; |
35 | | -import reactor.core.publisher.Flux; |
36 | 33 | import reactor.core.publisher.Mono; |
37 | 34 |
|
38 | 35 | import java.io.IOException; |
|
58 | 55 | import static io.r2dbc.spi.ConnectionFactoryOptions.PORT; |
59 | 56 | import static io.r2dbc.spi.ConnectionFactoryOptions.STATEMENT_TIMEOUT; |
60 | 57 | import static io.r2dbc.spi.ConnectionFactoryOptions.USER; |
61 | | - |
62 | 58 | import static oracle.r2dbc.test.DatabaseConfig.connectTimeout; |
63 | 59 | import static oracle.r2dbc.test.DatabaseConfig.host; |
64 | 60 | import static oracle.r2dbc.test.DatabaseConfig.password; |
|
67 | 63 | import static oracle.r2dbc.test.DatabaseConfig.sharedConnection; |
68 | 64 | import static oracle.r2dbc.test.DatabaseConfig.sqlTimeout; |
69 | 65 | import static oracle.r2dbc.test.DatabaseConfig.user; |
70 | | - |
71 | 66 | import static oracle.r2dbc.util.Awaits.awaitError; |
72 | 67 | import static oracle.r2dbc.util.Awaits.awaitExecution; |
73 | 68 | import static oracle.r2dbc.util.Awaits.awaitNone; |
|
78 | 73 | import static org.junit.jupiter.api.Assertions.assertEquals; |
79 | 74 | import static org.junit.jupiter.api.Assertions.assertThrows; |
80 | 75 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 76 | +import static org.junit.jupiter.api.Assertions.fail; |
81 | 77 |
|
82 | 78 | /** |
83 | 79 | * Verifies that |
@@ -333,7 +329,19 @@ public void testConnectTimeout() |
333 | 329 | .option(HOST, "localhost") |
334 | 330 | .option(PORT, listeningChannel.socket().getLocalPort()) |
335 | 331 | .option(DATABASE, serviceName()) |
336 | | - .option(CONNECT_TIMEOUT, Duration.ofMillis(500L)) |
| 332 | + .option(CONNECT_TIMEOUT, Duration.ofSeconds(2)) |
| 333 | + .build()); |
| 334 | + |
| 335 | + verifyConnectTimeout(listeningChannel, ConnectionFactoryOptions.parse( |
| 336 | + "r2dbc:oracle://localhost:" + listeningChannel.socket().getLocalPort() |
| 337 | + + "?connectTimeout=PT2S")); // The value is parsed as a Duration |
| 338 | + |
| 339 | + verifyConnectTimeout(listeningChannel, ConnectionFactoryOptions.builder() |
| 340 | + .option(DRIVER, "oracle") |
| 341 | + .option(HOST, "localhost") |
| 342 | + .option(PORT, listeningChannel.socket().getLocalPort()) |
| 343 | + .option(DATABASE, serviceName()) |
| 344 | + .option(CONNECT_TIMEOUT, Duration.ofMillis(500)) |
337 | 345 | .build()); |
338 | 346 |
|
339 | 347 | verifyConnectTimeout(listeningChannel, ConnectionFactoryOptions.parse( |
@@ -430,8 +438,27 @@ private void verifyConnectTimeout( |
430 | 438 |
|
431 | 439 | // Try to connect |
432 | 440 | try { |
| 441 | + long start = System.currentTimeMillis(); |
433 | 442 | awaitError(R2dbcTimeoutException.class, |
434 | 443 | ConnectionFactories.get(options).create()); |
| 444 | + |
| 445 | + // Compare the actual duration to the configured timeout |
| 446 | + Duration timeoutDelta = |
| 447 | + Duration.ofMillis(System.currentTimeMillis() - start) |
| 448 | + .minus(Duration.parse( |
| 449 | + options.getValue(Option.valueOf("connectTimeout")).toString())); |
| 450 | + |
| 451 | + // Expect the actual duration to be at least as much as the configured |
| 452 | + // timeout |
| 453 | + if (timeoutDelta.isNegative()) |
| 454 | + fail("Timeout triggered too soon: " + timeoutDelta); |
| 455 | + |
| 456 | + // Expect the actual duration to be no more than two seconds longer |
| 457 | + // than the configured timeout. This should be a generous enough offset |
| 458 | + // to allow for testing on slow systems |
| 459 | + if (timeoutDelta.toSeconds() > 2) |
| 460 | + fail("Timeout triggered too late: " + timeoutDelta); |
| 461 | + |
435 | 462 | } |
436 | 463 | catch (Throwable unexpected) { |
437 | 464 | unexpected.printStackTrace(); |
|
0 commit comments