From 99204311bdae8f8e6316a9a23411d65ec9843b30 Mon Sep 17 00:00:00 2001 From: Eran Date: Wed, 11 Oct 2023 09:00:35 +0300 Subject: [PATCH] add test-deadlock --- unit-tests/live/test-deadlock.cpp | 92 +++++++++++++++++++++++++++++++ unit-tests/live/test-deadlock.py | 62 +++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 unit-tests/live/test-deadlock.cpp create mode 100644 unit-tests/live/test-deadlock.py diff --git a/unit-tests/live/test-deadlock.cpp b/unit-tests/live/test-deadlock.cpp new file mode 100644 index 00000000000..a9f4e956a43 --- /dev/null +++ b/unit-tests/live/test-deadlock.cpp @@ -0,0 +1,92 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2023 Intel Corporation. All Rights Reserved. + +//#test:device D400* + +#include "live-common.h" +#include +#include + +using namespace rs2; + + +#if 0 +TEST_CASE( "simple iteration over query_devices", "[live]" ) +{ + rs2::context ctx; + rs2::device device; + + std::cout << "Librealsense - " << RS2_API_VERSION_STR << std::endl; + + rs2::device_list list = ctx.query_devices(); + std::cout << "You have " << list.size() << " RealSense devices connected before triggering reset" << std::endl; + + /* Reset all the devices */ + for( uint32_t dev_idx = 0; dev_idx < list.size(); dev_idx++ ) + { + rs2::device dev = list[dev_idx]; + } +} +#endif + + +TEST_CASE( "deadlock", "[live]" ) +{ + rs2::context ctx; + + std::cout << "Librealsense - " << RS2_API_VERSION_STR << std::endl; + + rs2::device_list list = ctx.query_devices(); + std::cout << "You have " << list.size() << " RealSense devices connected before triggering reset" << std::endl; + std::vector< rs2::device > devices; + + // Get all the devices + for( uint32_t dev_idx = 0; dev_idx < list.size(); dev_idx++ ) + devices.push_back( list[dev_idx] ); + + // Set up a callback + ctx.set_devices_changed_callback( + [&devices]( rs2::event_information const & i ) + { + for( auto & d : devices ) + { + if( d && i.was_removed( d ) ) + { + std::cout << "--- " << d.get_info( RS2_CAMERA_INFO_NAME ) << std::endl; + d = rs2::device(); // destroy it while we're in the callback + } + } + for( auto & d : i.get_new_devices() ) + { + std::cout << "+++ " << d.get_info( RS2_CAMERA_INFO_NAME ) << std::endl; + devices.push_back( d ); + } + } ); + + // Reset everything + for( auto & dev : devices ) + dev.hardware_reset(); + + std::this_thread::sleep_for( std::chrono::seconds( 6 ) ); + + list = ctx.query_devices(); + std::cout << "You have " << list.size() << " RealSense devices connected after reset" << std::endl; + + for( uint32_t dev_idx = 0; dev_idx < list.size(); dev_idx++ ) + { + { + rs2::device dev; + dev = list[dev_idx]; + + /* Add 500 ms delay to avoid deadlock */ + // usleep(500000); + + // Select the device required. For example, lets select dev_idx = 0. + if( dev_idx == 0 ) + { + //device = dev; + //break; + } + } + } +} diff --git a/unit-tests/live/test-deadlock.py b/unit-tests/live/test-deadlock.py new file mode 100644 index 00000000000..433b9a6c16a --- /dev/null +++ b/unit-tests/live/test-deadlock.py @@ -0,0 +1,62 @@ +# License: Apache 2.0. See LICENSE file in root directory. +# Copyright(c) 2023 Intel Corporation. All Rights Reserved. + +#test:device D400* !D457 + +import pyrealsense2 as rs +from rspy import test, log +from time import sleep + +c = rs.context( '{"dds":false}' ) + +device_was_removed = False +device_was_added = False +def callback( info ): + global dev, device_was_removed, device_was_added + print( '----> callback is called' ) + if dev is not None: + if info.was_removed( dev ): + log.i( 'device was removed' ) + device_was_removed = True + dev = None + for handle in info.get_new_devices(): + log.i( 'new device=', handle ) + if dev is None: + dev = handle + device_was_added = True + print( '<---- callback returning' ) +c.set_devices_changed_callback( callback ) + +#sleep(2) + +test.start( 'get the device' ) +dev = None +for dev in c.query_devices(): + break +test.check( dev is not None ) +test.finish() + +del c + +test.start( 'resetting the device' ) +dev.hardware_reset() +count = 10 +while count > 0 and not device_was_removed: + sleep( 0.5 ) + count -= 1 +test.info( 'device', dev ) +test.check( device_was_removed ) +test.finish() + +test.start( 'wait for it' ) +count = 10 +while count > 0 and not device_was_added: + sleep( 0.5 ) + count -= 1 +test.info( 'device', dev ) +test.check( device_was_added ) +test.finish() + +#sleep(2) + +test.print_results_and_exit()