Improve recorder and worker thread matching in RecorderPool (#116886)
* Improve recorder and worker thread matching in RecorderPool Previously we would look at the name of the threads. This was a brittle if because other integrations may name their thread Recorder or DbWorker. Instead we now use explict thread ids which ensures there will never be a conflict * fix * fixes * fixes
This commit is contained in:
parent
ee031f4850
commit
6339c63176
5 changed files with 65 additions and 23 deletions
|
@ -12,20 +12,32 @@ from homeassistant.components.recorder.pool import RecorderPool
|
|||
|
||||
async def test_recorder_pool_called_from_event_loop() -> None:
|
||||
"""Test we raise an exception when calling from the event loop."""
|
||||
engine = create_engine("sqlite://", poolclass=RecorderPool)
|
||||
recorder_and_worker_thread_ids: set[int] = set()
|
||||
engine = create_engine(
|
||||
"sqlite://",
|
||||
poolclass=RecorderPool,
|
||||
recorder_and_worker_thread_ids=recorder_and_worker_thread_ids,
|
||||
)
|
||||
with pytest.raises(RuntimeError):
|
||||
sessionmaker(bind=engine)().connection()
|
||||
|
||||
|
||||
def test_recorder_pool(caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test RecorderPool gives the same connection in the creating thread."""
|
||||
|
||||
engine = create_engine("sqlite://", poolclass=RecorderPool)
|
||||
recorder_and_worker_thread_ids: set[int] = set()
|
||||
engine = create_engine(
|
||||
"sqlite://",
|
||||
poolclass=RecorderPool,
|
||||
recorder_and_worker_thread_ids=recorder_and_worker_thread_ids,
|
||||
)
|
||||
get_session = sessionmaker(bind=engine)
|
||||
shutdown = False
|
||||
connections = []
|
||||
add_thread = False
|
||||
|
||||
def _get_connection_twice():
|
||||
if add_thread:
|
||||
recorder_and_worker_thread_ids.add(threading.get_ident())
|
||||
session = get_session()
|
||||
connections.append(session.connection().connection.driver_connection)
|
||||
session.close()
|
||||
|
@ -44,6 +56,7 @@ def test_recorder_pool(caplog: pytest.LogCaptureFixture) -> None:
|
|||
assert "accesses the database without the database executor" in caplog.text
|
||||
assert connections[0] != connections[1]
|
||||
|
||||
add_thread = True
|
||||
caplog.clear()
|
||||
new_thread = threading.Thread(target=_get_connection_twice, name=DB_WORKER_PREFIX)
|
||||
new_thread.start()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue