diff options
author | Maciej Kisielewski <maciej.kisielewski@canonical.com> | 2022-05-30 13:38:07 +0200 |
---|---|---|
committer | Maciej Kisielewski <maciej.kisielewski@canonical.com> | 2022-06-01 13:18:26 +0200 |
commit | ef59fb20b81b4ea2d186f1241f884b0e06072621 (patch) | |
tree | 35ff8358816f5d63072ee04ad481b7aa799916d2 | |
parent | 58eb482a29441a62c89f3f55d08995f399442f3c (diff) |
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.
-rwxr-xr-x | bin/removable_storage_watcher.py | 26 |
1 files 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__": |