Skip to content

Commit

Permalink
Fixed failing test cases
Browse files Browse the repository at this point in the history
Fixed failing test cases by catching appropriate exceptions and
verifying with asserts
  • Loading branch information
Rasmus Johansson committed May 20, 2015
1 parent c49eb6c commit ea70a96
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions src/test/java/org/mariadb/jdbc/TimeoutTest.java
Expand Up @@ -7,6 +7,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.Statement;
import java.util.Random;

Expand Down Expand Up @@ -71,23 +72,44 @@ private static int selectValue(Connection conn, int value)
*/
@Test
public void socketTimeoutTest() throws SQLException {
int exceptionCount = 0;
// set a short connection timeout
setConnection("&connectTimeout=5&socketTimeout=5");
PreparedStatement ps = connection.prepareStatement("SELECT 1");
ResultSet rs = ps.executeQuery();
rs.next();
logInfo(rs.getString(1));

// wait for the connection to time out
ps = connection.prepareStatement("SELECT sleep(1)");

rs = ps.executeQuery();

// a timeout should occur here
try
{
rs = ps.executeQuery();
} catch (SQLException e) {
// check that it's a timeout that occurs
if (e.getMessage().contains("timed out"))
exceptionCount++;
}

ps = connection.prepareStatement("SELECT 2");

rs = ps.executeQuery();
rs.next();
logInfo(rs.getString(1));
// connection should be closed here, exception expected
try
{
rs = ps.executeQuery();
rs.next();
} catch (SQLException e) {
// check that it's a execute "called on closed" exception that occurs
if (e.getMessage().contains("called on closed"))
exceptionCount++;
}

// the connection should be closed
assertTrue(connection.isClosed());
// there should have been two exceptions
assertTrue(exceptionCount == 2);
}

@Test
Expand All @@ -98,8 +120,16 @@ public void waitTimeoutStatementTest() throws SQLException, InterruptedException

logInfo(connection.toString());

statement.execute("SELECT 1");

// here a SQLNonTransientConnectionException is expected
// "Could not read resultset: unexpected end of stream, ..."
try
{
statement.execute("SELECT 1");
} catch (SQLException e) {
// verify that the correct type of exception is thrown
assertTrue(e.getMessage().contains("Could not read resultset"));
}

statement.close();
connection.close();
connection = null;
Expand All @@ -111,22 +141,24 @@ public void waitTimeoutResultSetTest() throws SQLException, InterruptedException
ResultSet rs = stmt.executeQuery("SELECT 1");

rs.next();
logInfo(rs.getString(1));

//stmt = connection.createStatement();
stmt.execute("set session wait_timeout=1");
Thread.sleep(3000); // Wait for the server to kill the connection

rs = stmt.executeQuery("SELECT 2");

rs.next();
logInfo(rs.getString(1));

assertTrue(connection.isClosed());

// here a SQLNonTransientConnectionException is expected
// "Could not read resultset: unexpected end of stream, ..."
try
{
rs = stmt.executeQuery("SELECT 2");
rs.next();
} catch (SQLException e) {
// verify that the correct type of exception is thrown
assertTrue(e.getMessage().contains("Could not read resultset"));
}
}

// CONJ-68
// TODO: this test is not able to repeat the bug. Ignore until then.
// TODO: this test is not yet able to repeat the bug. Ignore until then.
@Ignore
@Test
public void lastPacketFailedTest() throws SQLException
Expand Down

0 comments on commit ea70a96

Please sign in to comment.