Permalink
Browse files

test: add statement.getQueryTimeoutMs, so millisecond-scale timeouts …

…can be tested
  • Loading branch information...
vlsi committed Oct 31, 2015
1 parent ad7fc43 commit 892d7af82c4e077613a077ac950e63a8a5d069cc
Showing with 54 additions and 5 deletions.
  1. +29 −5 org/postgresql/jdbc2/AbstractJdbc2Statement.java
  2. +25 −0 org/postgresql/test/jdbc2/StatementTest.java
@@ -115,7 +115,7 @@
/** Number of rows to get in a batch. */
protected int fetchSize = 0;
/** Timeout (in seconds) for a query */
/** Timeout (in milli-seconds) for a query */
protected int timeout = 0;
protected boolean replaceProcessingEnabled = true;
@@ -757,6 +757,30 @@ public void setEscapeProcessing(boolean enable) throws SQLException
* @exception SQLException if a database access error occurs
*/
public int getQueryTimeout() throws SQLException
{
return getQueryTimeoutMs() / 1000;
}
/*
* Sets the queryTimeout limit
*
* @param seconds - the new query timeout limit in seconds
* @exception SQLException if a database access error occurs
*/
public void setQueryTimeout(int seconds) throws SQLException
{
setQueryTimeoutMs(seconds * 1000);
}
/*
* The queryTimeout limit is the number of seconds the driver
* will wait for a Statement to execute. If the limit is
* exceeded, a SQLException is thrown.
*
* @return the current query timeout limit in seconds; 0 = unlimited
* @exception SQLException if a database access error occurs
*/
public int getQueryTimeoutMs() throws SQLException
{
checkClosed();
return timeout;
@@ -768,14 +792,14 @@ public int getQueryTimeout() throws SQLException
* @param seconds - the new query timeout limit in seconds
* @exception SQLException if a database access error occurs
*/
public void setQueryTimeout(int seconds) throws SQLException
public void setQueryTimeoutMs(int millis) throws SQLException
{
checkClosed();
if (seconds < 0)
if (millis < 0)
throw new PSQLException(GT.tr("Query timeout must be a value greater than or equals to 0."),
PSQLState.INVALID_PARAMETER_VALUE);
timeout = seconds;
timeout = millis;
}
/**
@@ -3475,7 +3499,7 @@ public void run()
}
};
connection.addTimerTask(cancelTimerTask, timeout * 1000);
connection.addTimerTask(cancelTimerTask, timeout);
}
private synchronized void killTimerTask()
@@ -8,6 +8,7 @@
package org.postgresql.test.jdbc2;
import junit.framework.TestCase;
import org.postgresql.jdbc2.AbstractJdbc2Statement;
import org.postgresql.test.TestUtil;
import java.sql.*;
@@ -505,6 +506,30 @@ public void run()
}
}
/**
* Test executes two queries one after another.
* The first one has timeout of 1ms, and the second one does not.
* The timeout of the first query should not impact the second one.
* @throws SQLException
*/
public void testShortQueryTimeout() throws SQLException
{
long deadLine = System.currentTimeMillis() + 10000;
Statement stmt = con.createStatement();
((AbstractJdbc2Statement) stmt).setQueryTimeoutMs(1);
Statement stmt2 = con.createStatement();
while(System.currentTimeMillis() < deadLine) {
try
{
stmt.execute("select 1");
} catch (SQLException e)
{
// ignore "statement cancelled"
}
stmt2.executeQuery("select 1");
}
}
public void testSetQueryTimeoutWithSleep() throws SQLException, InterruptedException
{
// check that the timeout starts ticking at execute, not at the

0 comments on commit 892d7af

Please sign in to comment.