forked from facebookincubator/ft_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_test_utils.py
83 lines (65 loc) · 2.08 KB
/
lock_test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright (c) Meta Platforms, Inc. and affiliates.
# pyre-strict
import signal
import sys
import threading
import time
import unittest
from typing import Callable
from ft_utils.concurrent import AtomicFlag, AtomicReference
def run_interrupt_handling(
self: unittest.TestCase,
lock: object,
acquire: Callable[[object], None],
release: Callable[[object], None],
) -> None:
"""
Run interrupt handling test.
:param self: Test instance
:param lock: Lock object
:param acquire: Function to acquire the lock
:param release: Function to release the lock
"""
# Create atomic flags and references to synchronize between threads
started_flag = AtomicFlag(False)
signal_received_flag = AtomicFlag(False)
main_thread_id_ref = AtomicReference() # pyre-ignore
handler_thread_id_ref = AtomicReference() # pyre-ignore
main_thread_id_ref.set(threading.get_ident())
def signal_handler(signum: int, *args: object) -> None: # pyre-ignore
"""
Signal handler function.
:param signum: Signal number
:param args: Additional arguments
"""
handler_thread_id_ref.set(threading.get_ident())
signal_received_flag.set(True)
if sys.platform == "win32":
plat_signal = signal.SIGBREAK
else:
plat_signal = signal.SIGINT
signal.signal(plat_signal, signal_handler)
# Define a function to run in a separate thread
def worker() -> None: # pyre-ignore
"""
Worker function to run in a separate thread.
"""
acquire(lock)
try:
started_flag.set(True)
signal.raise_signal(plat_signal)
finally:
release(lock)
# Start the worker thread
thread = threading.Thread(target=worker)
thread.start()
while not started_flag:
time.sleep(0.001)
try:
acquire(lock)
self.assertTrue(signal_received_flag)
finally:
release(lock)
thread.join()
self.assertTrue(signal_received_flag)
self.assertEqual(main_thread_id_ref.get(), handler_thread_id_ref.get())