mirror of
https://github.com/simonw/datasette.git
synced 2026-09-02 22:54:08 +02:00
Merge d9791867ee into bdc9731740
This commit is contained in:
commit
bf4ebffbbc
2 changed files with 37 additions and 1 deletions
|
|
@ -354,6 +354,15 @@ class Database:
|
|||
result = fn(self._write_connection)
|
||||
else:
|
||||
result = fn(self._write_connection)
|
||||
if not block:
|
||||
# There is no write thread here, so the write has already
|
||||
# finished. Hand back the same (task_id, reply_future) shape
|
||||
# _send_to_write_thread() returns, with the future already
|
||||
# resolved, so the block=False path below is identical in
|
||||
# both modes.
|
||||
reply_future = asyncio.get_running_loop().create_future()
|
||||
reply_future.set_result(result)
|
||||
result = (uuid.uuid4(), reply_future)
|
||||
else:
|
||||
result = await self._send_to_write_thread(
|
||||
fn, block=block, transaction=transaction
|
||||
|
|
@ -425,7 +434,7 @@ class Database:
|
|||
)
|
||||
self._write_thread.name = f"_execute_writes for database {self.name}"
|
||||
self._write_thread.start()
|
||||
task_id = uuid.uuid5(uuid.NAMESPACE_DNS, "datasette.io")
|
||||
task_id = uuid.uuid4()
|
||||
loop = asyncio.get_running_loop()
|
||||
reply_future = loop.create_future()
|
||||
self._write_queue.put(
|
||||
|
|
|
|||
|
|
@ -705,6 +705,33 @@ async def test_execute_write_fn_block_false(db):
|
|||
assert isinstance(task_id, uuid.UUID)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("disable_threads", (False, True))
|
||||
async def test_execute_write_fn_block_false_returns_uuid(tmp_path, disable_threads):
|
||||
# block=False is documented to return "a UUID representing the queued task".
|
||||
# With num_sql_threads=0 there is no write thread, so the non-threaded branch
|
||||
# has to satisfy the same contract as the threaded one.
|
||||
settings = {"num_sql_threads": 0} if disable_threads else {}
|
||||
ds = Datasette([], memory=True, settings=settings)
|
||||
await ds.invoke_startup()
|
||||
db = ds.add_memory_database("test_block_false")
|
||||
await db.execute_write(
|
||||
"create table if not exists t (id integer primary key, v text)"
|
||||
)
|
||||
|
||||
def write_fn(conn):
|
||||
conn.execute("insert into t (v) values ('a')")
|
||||
# Returns None, like most write functions.
|
||||
|
||||
task_id = await db.execute_write_fn(write_fn, block=False)
|
||||
|
||||
assert isinstance(task_id, uuid.UUID)
|
||||
# Distinct per call, so a caller can tell two queued tasks apart.
|
||||
second = await db.execute_write_fn(write_fn, block=False)
|
||||
assert isinstance(second, uuid.UUID)
|
||||
assert second != task_id
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_write_fn_block_true(db):
|
||||
def write_fn(conn):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue