Skip to content

Commit

Permalink
add test-deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Oct 11, 2023
1 parent 8882934 commit 9920431
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
92 changes: 92 additions & 0 deletions unit-tests/live/test-deadlock.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <chrono>

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;
}
}
}
}
62 changes: 62 additions & 0 deletions unit-tests/live/test-deadlock.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 9920431

Please sign in to comment.