0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

refactor(tests): ensure BEC dispatcher singleton object is renewed at each test

and add a check for dangling threads
This commit is contained in:
2024-01-19 16:19:42 +01:00
parent d281d6576c
commit d909673071
12 changed files with 81 additions and 51 deletions

32
tests/conftest.py Normal file
View File

@ -0,0 +1,32 @@
import pytest
import threading
from bec_widgets.utils import bec_dispatcher as bec_dispatcher_module
@pytest.fixture()
def threads_check():
current_threads = set(
th
for th in threading.enumerate()
if "loguru" not in th.name and th is not threading.main_thread()
)
yield
threads_after = set(
th
for th in threading.enumerate()
if "loguru" not in th.name and th is not threading.main_thread()
)
additional_threads = threads_after - current_threads
assert (
len(additional_threads) == 0
), f"Test creates {len(additional_threads)} threads that are not cleaned: {additional_threads}"
@pytest.fixture(autouse=True)
def bec_dispatcher(threads_check):
bec_dispatcher = bec_dispatcher_module.BECDispatcher()
yield bec_dispatcher
bec_dispatcher.disconnect_all()
# reinitialize singleton for next test
bec_dispatcher_module._bec_dispatcher = None

View File

@ -5,18 +5,10 @@ import pytest
from bec_lib.messages import ScanMessage
from bec_lib.connector import MessageObject
# TODO: find a better way to mock singletons
from bec_widgets.utils.bec_dispatcher import _BECDispatcher
msg = MessageObject(topic="", value=ScanMessage(point_id=0, scanID=0, data={}).dumps())
@pytest.fixture(name="bec_dispatcher")
def _bec_dispatcher():
bec_dispatcher = _BECDispatcher()
yield bec_dispatcher
@pytest.fixture(name="consumer")
def _consumer(bec_dispatcher):
bec_dispatcher.client.connector.consumer = Mock()