From f132a6328e5d0e19eee6b50369287085ae2ed6ba Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 19 Dec 2023 10:47:06 -0800 Subject: [PATCH] Test non-threaded mode, handle memory connections --- datasette/database.py | 12 ++++++++++-- tests/test_internals_database.py | 9 ++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/datasette/database.py b/datasette/database.py index ef516e9ed9..f2c980d705 100644 --- a/datasette/database.py +++ b/datasette/database.py @@ -169,7 +169,11 @@ async def execute_isolated_fn(self, fn): result = fn(isolated_connection) finally: isolated_connection.close() - self._all_file_connections.remove(isolated_connection) + try: + self._all_file_connections.remove(isolated_connection) + except ValueError: + # Was probably a memory connection + pass return result else: # Threaded mode - send to write thread @@ -230,7 +234,11 @@ def _execute_writes(self): result = e finally: isolated_connection.close() - self._all_file_connections.remove(isolated_connection) + try: + self._all_file_connections.remove(isolated_connection) + except ValueError: + # Was probably a memory connection + pass else: try: result = task.fn(conn) diff --git a/tests/test_internals_database.py b/tests/test_internals_database.py index eb842c968e..e05111007a 100644 --- a/tests/test_internals_database.py +++ b/tests/test_internals_database.py @@ -1,6 +1,7 @@ """ Tests for the datasette.database.Database class """ +from datasette.app import Datasette from datasette.database import Database, Results, MultipleValues from datasette.utils.sqlite import sqlite3 from datasette.utils import Column @@ -543,7 +544,12 @@ def inner(conn): @pytest.mark.asyncio -async def test_execute_isolated(db): +@pytest.mark.parametrize("disable_threads", (False, True)) +async def test_execute_isolated(db, disable_threads): + if disable_threads: + ds = Datasette(memory=True, settings={"num_sql_threads": 0}) + db = ds.add_database(Database(ds, memory_name="test_num_sql_threads_zero")) + # Create temporary table in write await db.execute_write( "create temporary table created_by_write (id integer primary key)" @@ -577,6 +583,7 @@ def create_shared_table(conn): # ... and a second call to isolated should not see that connection either assert not await db.execute_isolated_fn(table_exists_checker("created_by_isolated")) + @pytest.mark.asyncio async def test_mtime_ns(db): assert isinstance(db.mtime_ns, int)