From cca647307b97989d3b87b1ec9802a234b1f2550d Mon Sep 17 00:00:00 2001 From: Steven Palma Date: Wed, 14 May 2025 14:06:59 +0200 Subject: [PATCH] fix(tests): kill thread when camera async_read tests fail --- tests/cameras/test_opencv.py | 25 ++++++++++++++----------- tests/cameras/test_realsense.py | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/tests/cameras/test_opencv.py b/tests/cameras/test_opencv.py index 6f74a969..3b141290 100644 --- a/tests/cameras/test_opencv.py +++ b/tests/cameras/test_opencv.py @@ -126,12 +126,15 @@ def test_async_read(index_or_path): camera = OpenCVCamera(config) camera.connect(do_warmup_read=False) - img = camera.async_read() + try: + img = camera.async_read() - assert camera.thread is not None - assert camera.thread.is_alive() - assert isinstance(img, np.ndarray) - camera.disconnect() # To stop/join the thread. Otherwise get warnings when the test ends + assert camera.thread is not None + assert camera.thread.is_alive() + assert isinstance(img, np.ndarray) + finally: + if camera.is_connected: + camera.disconnect() # To stop/join the thread. Otherwise get warnings when the test ends def test_async_read_timeout(): @@ -139,10 +142,12 @@ def test_async_read_timeout(): camera = OpenCVCamera(config) camera.connect(do_warmup_read=False) - with pytest.raises(TimeoutError): - camera.async_read(timeout_ms=0) - - camera.disconnect() + try: + with pytest.raises(TimeoutError): + camera.async_read(timeout_ms=0) + finally: + if camera.is_connected: + camera.disconnect() def test_async_read_before_connect(): @@ -183,5 +188,3 @@ def test_all_rotations(rotation, index_or_path): assert camera.width == original_width assert camera.height == original_height assert img.shape[:2] == (original_height, original_width) - - camera.disconnect() diff --git a/tests/cameras/test_realsense.py b/tests/cameras/test_realsense.py index 12502eeb..30e9e932 100644 --- a/tests/cameras/test_realsense.py +++ b/tests/cameras/test_realsense.py @@ -136,12 +136,15 @@ def test_async_read(mock_enable_device): camera = RealSenseCamera(config) camera.connect(do_warmup_read=False) - img = camera.async_read() + try: + img = camera.async_read() - assert camera.thread is not None - assert camera.thread.is_alive() - assert isinstance(img, np.ndarray) - camera.disconnect() # To stop/join the thread. Otherwise get warnings when the test ends + assert camera.thread is not None + assert camera.thread.is_alive() + assert isinstance(img, np.ndarray) + finally: + if camera.is_connected: + camera.disconnect() # To stop/join the thread. Otherwise get warnings when the test ends @patch("pyrealsense2.config.enable_device", side_effect=mock_rs_config_enable_device_from_file) @@ -150,10 +153,12 @@ def test_async_read_timeout(mock_enable_device): camera = RealSenseCamera(config) camera.connect(do_warmup_read=False) - with pytest.raises(TimeoutError): - camera.async_read(timeout_ms=0) - - camera.disconnect() + try: + with pytest.raises(TimeoutError): + camera.async_read(timeout_ms=0) + finally: + if camera.is_connected: + camera.disconnect() def test_async_read_before_connect():