MCPcopy Create free account
hub / github.com/python/cpython / test_stress_delivery_dependent

Method test_stress_delivery_dependent

Lib/test/test_signal.py:1270–1312  ·  view source on GitHub ↗

This test uses dependent signal handlers.

(self)

Source from the content-addressed store, hash-verified

1268 @unittest.skipUnless(hasattr(signal, "setitimer"),
1269 "test needs setitimer()")
1270 def test_stress_delivery_dependent(self):
1271 """
1272 This test uses dependent signal handlers.
1273 """
1274 N = self.decide_itimer_count()
1275 sigs = []
1276
1277 def first_handler(signum, frame):
1278 # 1e-6 is the minimum non-zero value for `setitimer()`.
1279 # Choose a random delay so as to improve chances of
1280 # triggering a race condition. Ideally the signal is received
1281 # when inside critical signal-handling routines such as
1282 # Py_MakePendingCalls().
1283 signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5)
1284
1285 def second_handler(signum=None, frame=None):
1286 sigs.append(signum)
1287
1288 # Here on Linux, SIGPROF > SIGALRM > SIGUSR1. By using both
1289 # ascending and descending sequences (SIGUSR1 then SIGALRM,
1290 # SIGPROF then SIGALRM), we maximize chances of hitting a bug.
1291 self.setsig(signal.SIGPROF, first_handler)
1292 self.setsig(signal.SIGUSR1, first_handler)
1293 self.setsig(signal.SIGALRM, second_handler) # for ITIMER_REAL
1294
1295 expected_sigs = 0
1296 deadline = time.monotonic() + support.SHORT_TIMEOUT
1297
1298 while expected_sigs < N:
1299 os.kill(os.getpid(), signal.SIGPROF)
1300 expected_sigs += 1
1301 # Wait for handlers to run to avoid signal coalescing
1302 while len(sigs) < expected_sigs and time.monotonic() < deadline:
1303 time.sleep(1e-5)
1304
1305 os.kill(os.getpid(), signal.SIGUSR1)
1306 expected_sigs += 1
1307 while len(sigs) < expected_sigs and time.monotonic() < deadline:
1308 time.sleep(1e-5)
1309
1310 # All ITIMER_REAL signals should have been delivered to the
1311 # Python handler
1312 self.assertEqual(len(sigs), N, "Some signals were lost")
1313
1314 @unittest.skipUnless(hasattr(signal, "setitimer"),
1315 "test needs setitimer()")

Callers

nothing calls this directly

Calls 5

decide_itimer_countMethod · 0.95
setsigMethod · 0.95
killMethod · 0.45
sleepMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected