Skip to content

Commit

Permalink
Test non-threaded mode, handle memory connections
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 19, 2023
1 parent fa471f8 commit f132a63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 10 additions & 2 deletions datasette/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_internals_database.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f132a63

Please sign in to comment.