[Libpqxx-general] cancellation of synchronous sql statements
Jeroen T. Vermeulen
jtv at xs4all.nl
Sun Aug 23 10:01:45 UTC 2009
On Sat, August 22, 2009 00:52, Madden, Don (DCOR) wrote:
> As a starting point for testing cancel query, I've attached a simple
> test in the enclosed file called
> "test_cancel_query.cxx". The simple test example, will install a signal
> handler for SIGALRM so it can run a timer for a short period, invoke a
> blocking SQL command for 2s. The timer will raise SIGALRM after 1s and
> cancel the blocking SQL command after 1s. The test will pass if the
> cancel exception is caught shortly after 1s (e.g. within 1s). For this
> test purposes, there is 2 supporting classes of CStopWatch to measure
> the time it took from starting the query to the timer expiring and
> cancelling the command. To raise the timer signal, there is a class
> called CSigAlrmTimer.
It fails for me. :-/ It's also pretty sensitive to portability and
timing variations, so for now I've gone with a very simple test:
#include <test_helpers.hxx>
using namespace PGSTD;
using namespace pqxx;
namespace
{
void test_cancel_query(connection_base &c, transaction_base &t)
{
pipeline p(t, "test_cancel_query");
p.retain(0);
const pipeline::query_id i = p.insert("SELECT pg_sleep(3)");
c.cancel_query();
PQXX_CHECK_THROWS(p.retrieve(i), sql_error, "Canceled query did not
throw.");
}
} // namespace
PQXX_REGISTER_TEST(test_cancel_query)
This doesn't exercise calling the function from a signal handler, but it
should exercise the cancel functionality fairly reliably. It doesn't test
that the statement is actually aborted, but we're only guaranteed a
reasonable effort anyway.
Jeroen
More information about the Libpqxx-general
mailing list