Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions Lib/test/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,17 @@ def setsig(self, signum, handler):
old_handler = signal.signal(signum, handler)
self.addCleanup(signal.signal, signum, old_handler)

def wait_for_signals(self, sigs, expected_sigs, deadline):
# Wait for the handlers to run, so that pending signals are not
# coalesced.
#
# Signals normally arrive within microseconds, so poll with a short
# sleep rather than sleeping_retry() whose 10 ms minimum delay
# dominates the runtime (and is stretched by timer coalescing on
# macOS).
while len(sigs) < expected_sigs and time.monotonic() < deadline:
time.sleep(1e-5)

def measure_itimer_resolution(self):
N = 20
times = []
Expand Down Expand Up @@ -1297,14 +1308,11 @@ def second_handler(signum=None, frame=None):
while expected_sigs < N:
os.kill(os.getpid(), signal.SIGPROF)
expected_sigs += 1
# Wait for handlers to run to avoid signal coalescing
while len(sigs) < expected_sigs and time.monotonic() < deadline:
time.sleep(1e-5)
self.wait_for_signals(sigs, expected_sigs, deadline)

os.kill(os.getpid(), signal.SIGUSR1)
expected_sigs += 1
while len(sigs) < expected_sigs and time.monotonic() < deadline:
time.sleep(1e-5)
self.wait_for_signals(sigs, expected_sigs, deadline)

# All ITIMER_REAL signals should have been delivered to the
# Python handler
Expand All @@ -1329,17 +1337,16 @@ def handler(signum, frame):
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL

expected_sigs = 0
deadline = time.monotonic() + support.SHORT_TIMEOUT

while expected_sigs < N:
# Hopefully the SIGALRM will be received somewhere during
# initial processing of SIGUSR2.
signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5)
os.kill(os.getpid(), signal.SIGUSR2)

expected_sigs += 2
# Wait for handlers to run to avoid signal coalescing
for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
if len(sigs) >= expected_sigs:
break
self.wait_for_signals(sigs, expected_sigs, deadline)

# All ITIMER_REAL signals should have been delivered to the
# Python handler
Expand Down
Loading