Skip to content

Commit

Permalink
Tests | Random generate Message Id and drop later to avoid conflicts …
Browse files Browse the repository at this point in the history
…with already set messages. (#1017)
  • Loading branch information
cheenamalhotra authored Apr 4, 2019
1 parent caddd83 commit 00e08ca
Showing 1 changed file with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,23 @@ private void testStatementPoolingInternal(String mode) throws Exception {
if (mode.equalsIgnoreCase("bulkcopy")) {
modifyConnectionForBulkCopyAPI(con);
}

int msgId = Constants.RANDOM.nextInt(50000, 99999);

// Test behavior with statement pooling.
con.setStatementPoolingCacheSize(10);
this.executeSQL(con,
"IF NOT EXISTS (SELECT * FROM sys.messages WHERE message_id = 99586) EXEC sp_addmessage 99586, 16, 'Prepared handle GAH!';");

this.executeSQL(con, "IF EXISTS (SELECT * FROM sys.messages WHERE message_id = " + msgId
+ ") EXEC sp_dropmessage @msgnum = " + msgId + ", @lang = 'all';");
this.executeSQL(con, "EXEC sp_addmessage " + msgId + ", 16, 'Prepared handle GAH!';");
// Test with missing handle failures (fake).
this.executeSQL(con, "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " (col INT);INSERT " + AbstractSQLGenerator.escapeIdentifier(tableName) + " VALUES (1);");
this.executeSQL(con, "CREATE PROC #updateProc1 AS UPDATE "
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " SET col += 1; IF EXISTS (SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " WHERE col % 5 = 0) RAISERROR(99586,16,1);");
this.executeSQL(con,
"CREATE PROC #updateProc1 AS UPDATE " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " SET col += 1; IF EXISTS (SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " WHERE col % 5 = 0) RAISERROR("
+ msgId + ",16,1);");
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement("#updateProc1")) {
for (int i = 0; i < 100; ++i) {
try {
Expand All @@ -551,6 +558,9 @@ private void testStatementPoolingInternal(String mode) throws Exception {
}
}
}
} finally {
this.executeSQL(con, "IF EXISTS (SELECT * FROM sys.messages WHERE message_id = " + msgId
+ ") EXEC sp_dropmessage @msgnum = " + msgId + ", @lang = 'all';");
}

// test updated value, should be 1 + 100 = 101
Expand All @@ -562,13 +572,17 @@ private void testStatementPoolingInternal(String mode) throws Exception {
}

// Test batching with missing handle failures (fake).
this.executeSQL(con,
"IF NOT EXISTS (SELECT * FROM sys.messages WHERE message_id = 99586) EXEC sp_addmessage 99586, 16, 'Prepared handle GAH!';");

this.executeSQL(con, "IF EXISTS (SELECT * FROM sys.messages WHERE message_id = " + msgId
+ ") EXEC sp_dropmessage @msgnum = " + msgId + ", @lang = 'all';");
this.executeSQL(con, "EXEC sp_addmessage " + msgId + ", 16, 'Prepared handle GAH!';");
this.executeSQL(con, "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName2)
+ " (col INT);INSERT " + AbstractSQLGenerator.escapeIdentifier(tableName2) + " VALUES (1);");
this.executeSQL(con, "CREATE PROC #updateProc2 AS UPDATE "
+ AbstractSQLGenerator.escapeIdentifier(tableName2) + " SET col += 1; IF EXISTS (SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName2) + " WHERE col % 5 = 0) RAISERROR(99586,16,1);");
this.executeSQL(con,
"CREATE PROC #updateProc2 AS UPDATE " + AbstractSQLGenerator.escapeIdentifier(tableName2)
+ " SET col += 1; IF EXISTS (SELECT * FROM "
+ AbstractSQLGenerator.escapeIdentifier(tableName2) + " WHERE col % 5 = 0) RAISERROR("
+ msgId + ",16,1);");
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement("#updateProc2")) {
for (int i = 0; i < 100; ++i) {
pstmt.addBatch();
Expand Down Expand Up @@ -597,6 +611,9 @@ private void testStatementPoolingInternal(String mode) throws Exception {
rs.next();
assertSame(101, rs.getInt(1));
}
} finally {
this.executeSQL(con, "IF EXISTS (SELECT * FROM sys.messages WHERE message_id = " + msgId
+ ") EXEC sp_dropmessage @msgnum = " + msgId + ", @lang = 'all';");
}

// Test behavior with statement pooling enabled
Expand Down

0 comments on commit 00e08ca

Please sign in to comment.