Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an unreliable timeout test #2256

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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