diff --git a/robot/helper_invariant_thread_safety_advanced.py b/robot/helper_invariant_thread_safety_advanced.py index 785085815..e05b795fa 100644 --- a/robot/helper_invariant_thread_safety_advanced.py +++ b/robot/helper_invariant_thread_safety_advanced.py @@ -6,6 +6,7 @@ Tests advanced lock contention, deadlock prevention, and robustness scenarios. from __future__ import annotations +import contextlib import sys import threading import time @@ -302,10 +303,8 @@ def rapid_create_destroy() -> None: inv_id = all_adds.pop() else: return - try: + with contextlib.suppress(Exception): svc.remove_invariant(inv_id) - except Exception: - pass except Exception as e: with errors_lock: errors.append(e) @@ -344,18 +343,12 @@ def thread_safety_with_invariant_modifications() -> None: def modify_operation(thread_id: int) -> None: try: - inv = svc.add_invariant( - text=f"Modify-{thread_id}", - scope=InvariantScope.GLOBAL, - source_name="modifications", - ) items = svc.list_invariants() - assert isinstance(items, list) if items and thread_id % 2 == 0: - try: - svc.remove_invariant(items[0].id if hasattr(items[0], "id") else items[0]) - except Exception: - pass + with contextlib.suppress(Exception): + svc.remove_invariant( + items[0].id if hasattr(items[0], "id") else items[0] + ) if items: svc.enforce_invariants( @@ -367,7 +360,9 @@ def thread_safety_with_invariant_modifications() -> None: errors.append(e) with ThreadPoolExecutor(max_workers=thread_count) as executor: - futures = [executor.submit(modify_operation, i) for i in range(thread_count)] + futures = [ + executor.submit(modify_operation, i) for i in range(thread_count) + ] for future in as_completed(futures): future.result() @@ -386,7 +381,9 @@ COMMANDS: dict[str, callable] = { "deadlock-prevention": deadlock_prevention, "concurrent-enforce-idempotency": concurrent_enforce_idempotency, "rapid-create-destroy": rapid_create_destroy, - "thread-safety-with-invariant-modifications": thread_safety_with_invariant_modifications, + "thread-safety-with-invariant-modifications": ( + thread_safety_with_invariant_modifications + ), } if __name__ == "__main__":