diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-10 13:06:18 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-10 13:06:18 +0100 |
| commit | ea5a277c6b8bb2ba48b0ca8822797cf6ab0b3405 (patch) | |
| tree | d2ce1c0fe156dcd9c53196c151bdfed091a25618 /tests/test_sleep.py | |
| parent | 8ec2352c22780202fcd39982f1c3e65d2dae0cc1 (diff) | |
| download | deck-it-ea5a277c6b8bb2ba48b0ca8822797cf6ab0b3405.tar.gz deck-it-ea5a277c6b8bb2ba48b0ca8822797cf6ab0b3405.zip | |
feat: the PC tells the tablet it is going to sleep
deckit/sleep.py holds an elogind delay inhibitor and watches
PrepareForSleep with dbus-monitor. On a suspend the server marks its
answers with X-Deckit-Sleep for 2 s, so the tablet's next poll shows "The
PC is asleep" at once and drops its wake lock, then lets the inhibitor go.
On resume it takes the inhibitor again. The 15 s silence fallback stays,
worded "The PC is not answering", for a crash or a dropped tunnel.
The inhibitor's command is `tail --pid=<server>` and dbus-monitor has a
parent-death signal, so a killed server leaves no lock delaying every
suspend (checked with kill -9: both gone within 2 s).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_sleep.py')
| -rw-r--r-- | tests/test_sleep.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/test_sleep.py b/tests/test_sleep.py new file mode 100644 index 0000000..99e819c --- /dev/null +++ b/tests/test_sleep.py @@ -0,0 +1,75 @@ +# deck-it - tests for sleep +# Copyright (C) 2026 Danilo M. <danix@danix.xyz> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) + +import os +import shutil +import stat +import subprocess +import tempfile +import threading +import time +import unittest + +from deckit import sleep + + +def until(fn, secs=3): + end = time.monotonic() + secs + while time.monotonic() < end: + if fn(): + return True + time.sleep(0.02) + return False + + +class Watch(unittest.TestCase): + def test_flags_then_releases_and_takes_the_lock_again(self): + bin_ = tempfile.mkdtemp(dir="/tmp") + self.addCleanup(shutil.rmtree, bin_) + log = Path(bin_) / "log" + # A suspend announced at 0.3 s, the resume at 1.2 s. + for name, body in { + "dbus-monitor": "sleep 0.3; echo ' boolean true'; sleep 0.9; echo ' boolean false'; sleep 1", + "elogind-inhibit": f"echo take >> {log}; exec sleep 100", + }.items(): + p = Path(bin_) / name + p.write_text("#!/bin/sh\n" + body + "\n") + p.chmod(p.stat().st_mode | stat.S_IXUSR) + path = os.environ["PATH"] + os.environ["PATH"] = bin_ + ":" + path + self.addCleanup(os.environ.__setitem__, "PATH", path) + sleep.SHOW = 0.2 + taken = [] + real = sleep._inhibit + sleep._inhibit = lambda: taken.append(real()) or taken[-1] + self.addCleanup(setattr, sleep, "_inhibit", real) + self.addCleanup(lambda: [subprocess.run(["pkill", "-g", str(p.pid)]) for p in taken]) + threading.Thread(target=sleep.watch, daemon=True).start() + + self.assertTrue(until(sleep.sleeping.is_set)) + self.assertTrue(until(lambda: taken[0].poll() is not None)) # let go: the PC may sleep + self.assertTrue(until(lambda: not sleep.sleeping.is_set())) + self.assertTrue(until(lambda: len(taken) == 2 and taken[1].poll() is None)) + self.assertTrue(until(lambda: log.read_text().split() == ["take", "take"])) + + +if __name__ == "__main__": + unittest.main() |
