Skip to content

Commit

Permalink
Initial commit. Added in CRL connection tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffery-Wasty committed Sep 21, 2024
1 parent df67a98 commit 34658ea
Showing 1 changed file with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@


/**
* Test statement retry for configurable retry logic
* Test connection and statement retry for configurable retry logic
*/
public class ConfigurableRetryLogicTest extends AbstractTest {
private static String connectionStringCRL = null;
Expand Down Expand Up @@ -169,6 +169,29 @@ public void testStatementRetryWithShortQueryTimeout(String addedRetryParams) thr
}
}

/**
* Tests connection retry. Used in other tests.
*
* @throws Exception
* if unable to connect or execute against db
*/
public void testConnectionRetry(String replacedDbName, String addedRetryParams) throws Exception {
String cxnString = connectionString + addedRetryParams;
cxnString = TestUtils.addOrOverrideProperty(cxnString, "database", replacedDbName);

try (Connection conn = DriverManager.getConnection(cxnString); Statement s = conn.createStatement()) {
try {
fail(TestResource.getResource("R_expectedFailPassed"));
} catch (Exception e) {
System.out.println("blah");
assertTrue(e.getMessage().startsWith("There is already an object"),
TestResource.getResource("R_unexpectedExceptionContent") + ": " + e.getMessage());
} finally {
dropTable(s);
}
}
}

/**
* Tests that the correct number of retries are happening for all statement scenarios. Tests are expected to take
* a minimum of the sum of whatever has been defined for the waiting intervals, and maximum of the previous sum
Expand Down Expand Up @@ -444,6 +467,94 @@ public void testRetryChange() throws Exception {
}
}

/**
* Tests that the correct number of retries are happening for all connection scenarios. Tests are expected to take
* a minimum of the sum of whatever has been defined for the waiting intervals, and maximum of the previous sum
* plus some amount of time to account for test environment slowness.
*/
@Test
public void connectionTimingTest() {
long totalTime;
long timerStart = System.currentTimeMillis();
long expectedMaxTime = 10;

// No retries since CRL rules override, expected time ~1 second
try {
testConnectionRetry("blah", "retryExec={9999};");
} catch (Exception e) {
assertTrue(
(e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase()))
|| (TestUtils.getProperty(connectionString, "msiClientId") != null && e.getMessage()
.toLowerCase().contains(TestResource.getResource("R_loginFailedMI").toLowerCase()))
|| ((isSqlAzure() || isSqlAzureDW()) && e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_connectTimedOut").toLowerCase())),
e.getMessage());

if (e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase())) {
// Only check the timing if the correct error, "cannot open database", is returned.
totalTime = System.currentTimeMillis() - timerStart;
assertTrue(totalTime < TimeUnit.SECONDS.toMillis(expectedMaxTime),
"total time: " + totalTime + ", expected time: " + TimeUnit.SECONDS.toMillis(expectedMaxTime));
}
}

timerStart = System.currentTimeMillis();
long expectedMinTime = 20;
expectedMaxTime = 30;

// (0s attempt + 10s wait + 0s attempt) * 2 due to current driver bug = expected 20s execution time
try {
testConnectionRetry("blah", "retryExec={4060};");
} catch (Exception e) {
assertTrue(
(e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase()))
|| (TestUtils.getProperty(connectionString, "msiClientId") != null && e.getMessage()
.toLowerCase().contains(TestResource.getResource("R_loginFailedMI").toLowerCase()))
|| ((isSqlAzure() || isSqlAzureDW()) && e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_connectTimedOut").toLowerCase())),
e.getMessage());

if (e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase())) {
// Only check the timing if the correct error, "cannot open database", is returned.
totalTime = System.currentTimeMillis() - timerStart;
assertTrue(totalTime < TimeUnit.SECONDS.toMillis(expectedMaxTime), "total time: " + totalTime
+ ", expected max time: " + TimeUnit.SECONDS.toMillis(expectedMaxTime));
assertTrue(totalTime > TimeUnit.SECONDS.toMillis(expectedMinTime), "total time: " + totalTime
+ ", expected min time: " + TimeUnit.SECONDS.toMillis(expectedMinTime));
}
}

timerStart = System.currentTimeMillis();

// Append should work the same way
try {
testConnectionRetry("blah", "retryExec={+4060,4070};");
} catch (Exception e) {
assertTrue(
(e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase()))
|| (TestUtils.getProperty(connectionString, "msiClientId") != null && e.getMessage()
.toLowerCase().contains(TestResource.getResource("R_loginFailedMI").toLowerCase()))
|| ((isSqlAzure() || isSqlAzureDW()) && e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_connectTimedOut").toLowerCase())),
e.getMessage());

if (e.getMessage().toLowerCase()
.contains(TestResource.getResource("R_cannotOpenDatabase").toLowerCase())) {
// Only check the timing if the correct error, "cannot open database", is returned.
totalTime = System.currentTimeMillis() - timerStart;
assertTrue(totalTime < TimeUnit.SECONDS.toMillis(expectedMaxTime), "total time: " + totalTime
+ ", expected max time: " + TimeUnit.SECONDS.toMillis(expectedMaxTime));
assertTrue(totalTime > TimeUnit.SECONDS.toMillis(expectedMinTime), "total time: " + totalTime
+ ", expected min time: " + TimeUnit.SECONDS.toMillis(expectedMinTime));
}
}
}

private static void createTable(Statement stmt) throws SQLException {
String sql = "create table " + tableName + " (c1 int null);";
stmt.execute(sql);
Expand Down

0 comments on commit 34658ea

Please sign in to comment.