Skip to content

Commit

Permalink
Fix an unreliable timeout test (#2256)
Browse files Browse the repository at this point in the history
* Fix the yucky test

* format
  • Loading branch information
Jeffery-Wasty committed Nov 16, 2023
1 parent 11680a6 commit ac200ab
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,32 @@ public void testFOInstanceResolution2() throws SQLException {

verifyTimeout(timerEnd - timerStart, defaultTimeout);
}

/**
* Tests that failover is still used with socket timeout by measuring timing during a socket timeout.
*
* Tests that failover is correctly used after a socket timeout, by confirming total time includes socketTimeout
* for both primary and failover server.
*/
@Test
public void testFailoverInstanceResolutionWithSocketTimeout() {
long timerEnd;
long timerStart = System.currentTimeMillis();
try (Connection conn = PrepUtil.getConnection(connectionString
+ ";failoverPartner=" + RandomUtil.getIdentifier("FailoverPartner")
+ ";socketTimeout=" + waitForDelaySeconds)) {

try (Connection con = PrepUtil.getConnection("jdbc:sqlserver://" + randomServer
+ ";databaseName=FailoverDB;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;"
+ ";socketTimeout=" + waitForDelaySeconds)) {
fail(TestResource.getResource("R_shouldNotConnect"));
} catch (Exception e) {
timerEnd = System.currentTimeMillis();
if (!(e instanceof SQLException)) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
}

verifyTimeout(timerEnd - timerStart, waitForDelaySeconds);
// Driver should correctly attempt to connect to db, experience a socketTimeout, attempt to connect to
// failover, and then have another socketTimeout. So, expected total time is 2 x socketTimeout.
long totalTime = timerEnd - timerStart;
long totalExpectedTime = waitForDelaySeconds * 1000L * 2; // We expect 2 * socketTimeout
assertTrue( totalTime >= totalExpectedTime, TestResource.getResource("R_executionNotLong")
+ "totalTime: " + totalTime + " expectedTime: " + totalExpectedTime);
}
}

Expand Down

0 comments on commit ac200ab

Please sign in to comment.