mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Tests now close SQLite database connections and files explicitly, refs #1843
Also added a db.close() method to the Database class.
This commit is contained in:
parent
2ec5583629
commit
867e0abd34
3 changed files with 23 additions and 7 deletions
|
|
@ -47,6 +47,8 @@ class Database:
|
||||||
# These are used when in non-threaded mode:
|
# These are used when in non-threaded mode:
|
||||||
self._read_connection = None
|
self._read_connection = None
|
||||||
self._write_connection = None
|
self._write_connection = None
|
||||||
|
# This is used to track all file connections so they can be closed
|
||||||
|
self._all_file_connections = []
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cached_table_counts(self):
|
def cached_table_counts(self):
|
||||||
|
|
@ -91,9 +93,16 @@ class Database:
|
||||||
assert not (write and not self.is_mutable)
|
assert not (write and not self.is_mutable)
|
||||||
if write:
|
if write:
|
||||||
qs = ""
|
qs = ""
|
||||||
return sqlite3.connect(
|
conn = sqlite3.connect(
|
||||||
f"file:{self.path}{qs}", uri=True, check_same_thread=False
|
f"file:{self.path}{qs}", uri=True, check_same_thread=False
|
||||||
)
|
)
|
||||||
|
self._all_file_connections.append(conn)
|
||||||
|
return conn
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
# Close all connections - useful to avoid running out of file handles in tests
|
||||||
|
for connection in self._all_file_connections:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
async def execute_write(self, sql, params=None, block=True):
|
async def execute_write(self, sql, params=None, block=True):
|
||||||
def _inner(conn):
|
def _inner(conn):
|
||||||
|
|
|
||||||
|
|
@ -874,6 +874,13 @@ If your function raises an exception that exception will be propagated up to the
|
||||||
|
|
||||||
If you specify ``block=False`` the method becomes fire-and-forget, queueing your function to be executed and then allowing your code after the call to ``.execute_write_fn()`` to continue running while the underlying thread waits for an opportunity to run your function. A UUID representing the queued task will be returned. Any exceptions in your code will be silently swallowed.
|
If you specify ``block=False`` the method becomes fire-and-forget, queueing your function to be executed and then allowing your code after the call to ``.execute_write_fn()`` to continue running while the underlying thread waits for an opportunity to run your function. A UUID representing the queued task will be returned. Any exceptions in your code will be silently swallowed.
|
||||||
|
|
||||||
|
.. _database_close:
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
----------
|
||||||
|
|
||||||
|
Closes all of the open connections to file-backed databases. This is mainly intended to be used by large test suites, to avoid hitting limits on the number of open files.
|
||||||
|
|
||||||
.. _internals_database_introspection:
|
.. _internals_database_introspection:
|
||||||
|
|
||||||
Database introspection
|
Database introspection
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
from datasette.utils.sqlite import sqlite3, sqlite_version
|
from datasette.utils.sqlite import sqlite3
|
||||||
from datasette.utils.testing import TestClient
|
from datasette.utils.testing import TestClient
|
||||||
import click
|
import click
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
@ -9,11 +9,9 @@ import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import pytest
|
import pytest
|
||||||
import random
|
import random
|
||||||
import sys
|
|
||||||
import string
|
import string
|
||||||
import tempfile
|
import tempfile
|
||||||
import textwrap
|
import textwrap
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
# This temp file is used by one of the plugin config tests
|
# This temp file is used by one of the plugin config tests
|
||||||
|
|
@ -163,9 +161,11 @@ def make_app_client(
|
||||||
crossdb=crossdb,
|
crossdb=crossdb,
|
||||||
)
|
)
|
||||||
yield TestClient(ds)
|
yield TestClient(ds)
|
||||||
# Close the connection to avoid "too many open files" errors
|
# Close as many database connections as possible
|
||||||
conn.close()
|
# to try and avoid too many open files error
|
||||||
os.remove(filepath)
|
for db in ds.databases.values():
|
||||||
|
if not db.is_memory:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue