Skip to content

Commit ac200ab

Browse files
Fix an unreliable timeout test (#2256)
* Fix the yucky test * format
1 parent 11680a6 commit ac200ab

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/connection/TimeoutTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,32 @@ public void testFOInstanceResolution2() throws SQLException {
286286

287287
verifyTimeout(timerEnd - timerStart, defaultTimeout);
288288
}
289-
289+
290290
/**
291-
* Tests that failover is still used with socket timeout by measuring timing during a socket timeout.
292-
*
291+
* Tests that failover is correctly used after a socket timeout, by confirming total time includes socketTimeout
292+
* for both primary and failover server.
293293
*/
294294
@Test
295295
public void testFailoverInstanceResolutionWithSocketTimeout() {
296296
long timerEnd;
297297
long timerStart = System.currentTimeMillis();
298-
299-
try (Connection conn = PrepUtil.getConnection(connectionString
300-
+ ";failoverPartner=" + RandomUtil.getIdentifier("FailoverPartner")
301-
+ ";socketTimeout=" + waitForDelaySeconds)) {
298+
299+
try (Connection con = PrepUtil.getConnection("jdbc:sqlserver://" + randomServer
300+
+ ";databaseName=FailoverDB;failoverPartner=" + randomServer + "\\foo;user=sa;password=pwd;"
301+
+ ";socketTimeout=" + waitForDelaySeconds)) {
302302
fail(TestResource.getResource("R_shouldNotConnect"));
303303
} catch (Exception e) {
304304
timerEnd = System.currentTimeMillis();
305305
if (!(e instanceof SQLException)) {
306306
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
307307
}
308308

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

0 commit comments

Comments
 (0)