From ef59fb20b81b4ea2d186f1241f884b0e06072621 Mon Sep 17 00:00:00 2001 From: Maciej Kisielewski Date: Mon, 30 May 2022 13:38:07 +0200 Subject: delay zapperized insertion/removal for 5s Previously the command to connect or disconnect the USB device was sent almost at the same time that removable_watcher started waiting for that change to take place. This meant that there was a big chance for that action to happen _before_ the test could observe the change. This patch introduces a delay (with a background thread) that calls Zapper after 5 seconds from starting the observation. The whole test is programmed to wait for 20s, so the delay doesn't change the duration of the test. It's also not some fixing-race-conditions-with-random-sleeps kind of fix. --- bin/removable_storage_watcher.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/bin/removable_storage_watcher.py b/bin/removable_storage_watcher.py index ac450ba..7208954 100755 --- a/bin/removable_storage_watcher.py +++ b/bin/removable_storage_watcher.py @@ -7,6 +7,7 @@ import dbus import logging import os import sys +import threading import gi gi.require_version('GUdev', '1.0') @@ -922,18 +923,31 @@ def main(): "ZAPPER_ADDRESS environment variable not found!") zapper_control = ControlVersionDecider().decide(zapper_host) usb_address = args.zapper_usb_address - if args.action == "insert": + delay = 5 # in seconds + + def do_the_insert(): logging.info("Calling zapper to connect the USB device") zapper_control.usb_set_state(usb_address, 'dut') - elif args.action == "remove": + insert_timer = threading.Timer(delay, do_the_insert) + + def do_the_remove(): logging.info("Calling zapper to disconnect the USB device") zapper_control.usb_set_state(usb_address, 'off') + remove_timer = threading.Timer(delay, do_the_remove) + if args.action == "insert": + logging.info("Starting timer for delayed insertion") + insert_timer.start() + elif args.action == "remove": + logging.info("Starting timer for delayed removal") + remove_timer.start() + try: + res = listener.check(args.timeout) + return res + except KeyboardInterrupt: + return 1 + else: print("\n\n{} NOW\n\n".format(args.action.upper()), flush=True) - try: - return listener.check(args.timeout) - except KeyboardInterrupt: - return 1 if __name__ == "__main__": -- cgit v1.2.3