Skip to content

Commit

Permalink
Make timeout double, add small test.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Nov 1, 2024
1 parent 8ac7ab0 commit 9727279
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,21 +1410,21 @@ static PyObject* pysqlite_connection_set_busy_handler(pysqlite_Connection* self,

static PyObject* pysqlite_connection_set_busy_timeout(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
int busy_timeout;
double busy_timeout;

static char *kwlist[] = { "timeout", NULL };

if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i:set_busy_timeout",
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "d:set_busy_timeout",
kwlist, &busy_timeout)) {
return NULL;
}

int rc;
rc = sqlite3_busy_timeout(self->db, busy_timeout * 1000);
rc = sqlite3_busy_timeout(self->db, (int)busy_timeout * 1000);
if (rc != SQLITE_OK) {
PyErr_SetString(pysqlite_OperationalError, "Error setting busy timeout");
return NULL;
Expand Down
39 changes: 38 additions & 1 deletion tests/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import os
import unittest

from sqlcipher3 import dbapi2 as sqlite


Expand Down Expand Up @@ -277,12 +278,48 @@ def trace(statement):
self.assertEqual(traced_statements, queries)


class TestBusyHandlerTimeout(unittest.TestCase):
def test_busy_handler(self):
accum = []
def custom_handler(n):
accum.append(n)
return 0 if n == 3 else 1

self.addCleanup(os.unlink, 'busy.db')
conn1 = sqlite.connect('busy.db')
conn2 = sqlite.connect('busy.db')
conn2.set_busy_handler(custom_handler)

conn1.execute('begin exclusive')
with self.assertRaises(sqlite.OperationalError):
conn2.execute('create table test(id)')
self.assertEqual(accum, [0, 1, 2, 3])
accum.clear()

conn2.set_busy_handler(None)
with self.assertRaises(sqlite.OperationalError):
conn2.execute('create table test(id)')
self.assertEqual(accum, [])

conn2.set_busy_handler(custom_handler)
with self.assertRaises(sqlite.OperationalError):
conn2.execute('create table test(id)')
self.assertEqual(accum, [0, 1, 2, 3])
accum.clear()

conn2.set_busy_timeout(0.01) # Clears busy handler.
with self.assertRaises(sqlite.OperationalError):
conn2.execute('create table test(id)')
self.assertEqual(accum, [])


def suite():
loader = unittest.TestLoader()
tests = [loader.loadTestsFromTestCase(t) for t in (
CollationTests,
ProgressTests,
TraceCallbackTests)]
TraceCallbackTests,
TestBusyHandlerTimeout)]
return unittest.TestSuite(tests)

def test():
Expand Down

0 comments on commit 9727279

Please sign in to comment.