Skip to content

Commit

Permalink
Fix | Remove unwanted assert check and add tests for cancelQueryTimeo…
Browse files Browse the repository at this point in the history
…ut connection property | GitHub issue microsoft#525
  • Loading branch information
cheenamalhotra committed May 2, 2018
1 parent 2ff0015 commit 4e18619
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7156,7 +7156,6 @@ public Thread newThread(Runnable r)
TDSCommand command,
SQLServerConnection con) {
assert timeoutSeconds > 0;
assert null != command;

this.timeoutSeconds = timeoutSeconds;
this.command = command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.runner.RunWith;

import com.microsoft.sqlserver.jdbc.SQLServerConnection;
import com.microsoft.sqlserver.jdbc.SQLServerStatement;
import com.microsoft.sqlserver.testframework.AbstractTest;
import com.microsoft.sqlserver.testframework.Utils;
import com.microsoft.sqlserver.testframework.util.RandomUtil;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void testQueryTimeout() throws Exception {
createWaitForDelayPreocedure(conn);

conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";queryTimeout=" + (waitForDelaySeconds / 2) + ";");

try {
conn.createStatement().execute("exec " + waitForDelaySPName);
throw new Exception("Exception for queryTimeout is not thrown.");
Expand All @@ -118,7 +119,71 @@ public void testQueryTimeout() throws Exception {
fail("Unexpected error message occured! "+ e.toString() );
}
}

/**
* Tests sanity of connection property.
* @throws Exception
*/
@Test
public void testCancelQueryTimeout() throws Exception {
SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);

dropWaitForDelayProcedure(conn);
createWaitForDelayPreocedure(conn);

conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";queryTimeout=" + (waitForDelaySeconds / 2) + ";cancelQueryTimeout=" + waitForDelaySeconds + ";");

try {
SQLServerStatement statement = (SQLServerStatement) conn.createStatement();
statement.execute("exec " + waitForDelaySPName);
throw new Exception("Exception for queryTimeout is not thrown.");
}
catch (Exception e) {
if (!(e instanceof java.sql.SQLTimeoutException)) {
throw e;
}
assertEquals(e.getMessage(), "The query has timed out.", "Invalid exception message");
}
try{
conn.createStatement().execute("SELECT @@version");
}catch (Exception e) {
fail("Unexpected error message occured! "+ e.toString() );
}
}

/**
* Tests sanity of connection property.
* @throws Exception
*/
@Test
public void testCancelQueryTimeoutOnStatement() throws Exception {
SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);

dropWaitForDelayProcedure(conn);
createWaitForDelayPreocedure(conn);

conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";");

try {
SQLServerStatement statement = (SQLServerStatement) conn.createStatement();
statement.setQueryTimeout(waitForDelaySeconds/2);
statement.setCancelQueryTimeout(waitForDelaySeconds);
statement.execute("exec " + waitForDelaySPName);
throw new Exception("Exception for queryTimeout is not thrown.");
}
catch (Exception e) {
if (!(e instanceof java.sql.SQLTimeoutException)) {
throw e;
}
assertEquals(e.getMessage(), "The query has timed out.", "Invalid exception message");
}
try{
conn.createStatement().execute("SELECT @@version");
}catch (Exception e) {
fail("Unexpected error message occured! "+ e.toString() );
}
}

/**
* When socketTimeout occurs, the connection will be marked as closed.
* @throws Exception
Expand Down

0 comments on commit 4e18619

Please sign in to comment.