mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
test(handoff): time held exits from the run, and only once the program is at its exit
This commit is contained in:
1 parent
cdbed1bcb8
commit
49689e4722
1 file changed
+31
-12
@@ -2,6 +2,7 @@ import errno
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import select
|
||||
import shutil
|
||||
import signal
|
||||
import socket
|
||||
@@ -2048,23 +2049,39 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
worker = "-E DDH_WORKER_PID=$$" if self.prefix else "DDH_WORKER_PID=$$"
|
||||
runner = " ".join(self.prefix) + " " + settings + " " + worker if self.prefix else "env " + settings + " " + worker
|
||||
run = subprocess.Popen(["sh", "-c", "exec %s %s" % (runner, probe)], pass_fds=(table,),
|
||||
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
|
||||
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||||
run.started = time.monotonic() # after the build: only the run is timed
|
||||
run.seen, run.rest = {}, b""
|
||||
self.addCleanup(run.wait)
|
||||
self.addCleanup(lambda: run.poll() is None and run.kill())
|
||||
return run, table
|
||||
|
||||
@staticmethod
|
||||
def printed(run):
|
||||
return dict(line.split("\t", 1) for line in run.stdout.read().splitlines() if "\t" in line)
|
||||
def printed(run, until=(), deadline=60):
|
||||
# What the program printed, as it comes: it flushes once, at the end of main(), so lines in mean
|
||||
# it is at its exit. Reads until the keys in until are in (or end of output, or the deadline).
|
||||
fd = run.stdout.fileno()
|
||||
end = time.monotonic() + deadline
|
||||
while not all(key in run.seen for key in until) and time.monotonic() < end:
|
||||
if not select.select([fd], [], [], 0.1)[0]:
|
||||
continue
|
||||
data = os.read(fd, 4096)
|
||||
if not data:
|
||||
break
|
||||
*lines, run.rest = (run.rest + data).split(b"\n")
|
||||
for line in lines:
|
||||
if b"\t" in line:
|
||||
key, value = line.decode().split("\t", 1)
|
||||
run.seen[key] = value
|
||||
return run.seen
|
||||
|
||||
def test_with_no_free_fd_a_pidfd_ends_a_held_exit_once_its_holder_has_exited(self):
|
||||
forker = subprocess.Popen(["sleep", "1"])
|
||||
self.addCleanup(forker.wait) # a zombie once it exits: kill() alone would call it alive
|
||||
start = time.monotonic()
|
||||
run, table = self.start_worker_exit("takenpidfd", forker.pid, 4, nofds=True)
|
||||
self.assertEqual(9, run.wait(timeout=60)) # its own exit, not a signal
|
||||
took = time.monotonic() - start
|
||||
result = self.printed(run)
|
||||
took = time.monotonic() - run.started
|
||||
result = self.printed(run, ("pidfd", "nofds"))
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
# well inside the bounded wait (5000 looks) that kill() alone would have taken
|
||||
@@ -2076,12 +2093,12 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
run, table = self.start_worker_exit("takenbounded", forker.pid, 4, nofds=True, child_start=0)
|
||||
result = self.printed(run, ("pidfd", "nofds")) # at its exit before the wait is timed
|
||||
self.assertEqual("0", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
with self.assertRaises(subprocess.TimeoutExpired):
|
||||
run.wait(timeout=2) # the holder lives, as kill() tells
|
||||
self.assertEqual(9, run.wait(timeout=90)) # then the bound: it gives up waiting
|
||||
result = self.printed(run)
|
||||
self.assertEqual("0", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
state, status = struct.unpack("<ii", os.pread(table, 8, 64 + 12))
|
||||
self.assertEqual(4, state) # nothing written into the entry its holder still has
|
||||
self.assertEqual(0, status)
|
||||
@@ -2091,13 +2108,13 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
run, table = self.start_worker_exit("takenlivepidfd", forker.pid, 4, nofds=True)
|
||||
result = self.printed(run, ("pidfd", "nofds")) # at its exit before the wait is timed
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
with self.assertRaises(subprocess.TimeoutExpired):
|
||||
run.wait(timeout=15) # past the bound kill() alone gives (5000 looks)
|
||||
os.pwrite(table, struct.pack("<i", 1), 64 + 12) # given back, HANDED
|
||||
self.assertEqual(9, run.wait(timeout=30))
|
||||
result = self.printed(run)
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
self.assertEqual("1", result.get("nofds"), result)
|
||||
state, status = struct.unpack("<ii", os.pread(table, 8, 64 + 12))
|
||||
self.assertEqual(2, state) # EXITED
|
||||
self.assertEqual(9 << 8, status)
|
||||
@@ -2107,6 +2124,8 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
self.addCleanup(forker.wait)
|
||||
self.addCleanup(forker.kill)
|
||||
run, table = self.start_worker_exit("takenlive", forker.pid, 4) # TAKEN
|
||||
result = self.printed(run, ("pidfd",)) # at its exit before the wait is timed
|
||||
self.assertEqual("1", result.get("pidfd"), result)
|
||||
with self.assertRaises(subprocess.TimeoutExpired):
|
||||
run.wait(timeout=2) # held while the parent that took the entry lives
|
||||
self.assertEqual(4, struct.unpack("<i", os.pread(table, 4, 64 + 12))[0])
|
||||
|
||||
Reference in new issue
Block a user