diff options
Diffstat (limited to 'share/python/lib/daemon.md')
-rw-r--r-- | share/python/lib/daemon.md | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/share/python/lib/daemon.md b/share/python/lib/daemon.md index 9484926..2a9a2e0 100644 --- a/share/python/lib/daemon.md +++ b/share/python/lib/daemon.md @@ -27,31 +27,36 @@ then there is no "/bin/sleep 12765" process # Daemon takes a while to open its port -[netcat]: https://en.wikipedia.org/wiki/Netcat - -This scenario verifies that if the background process never starts -listening on its port, the daemon library handles that correctly. We -do this by using [netcat][] to start a dummy daemon, after a short -delay. The lib/daemon code will wait for netcat to open its port, by -connecting to the port. It then closes the port, which causes netcat -to terminate. +This scenario verifies that if the background process doesn't immediately start +listening on its port, the daemon library handles that correctly. We do this +with a helper script that waits 2 seconds before opening the port. The +lib/daemon code will wait for the script by repeatedly trying to connect. Once +successful, it immediately closes the port, which causes the script to +terminate. ~~~scenario -given a daemon helper shell script slow-start-daemon.sh -given there is no "slow-start-daemon.sh" process -when I try to start "./slow-start-daemon.sh" as slow-daemon, on port 8888 +given a daemon helper shell script slow-start-daemon.py +given there is no "slow-start-daemon.py" process +when I try to start "./slow-start-daemon.py" as slow-daemon, on port 8888 +then starting the daemon succeeds when I stop background process slow-daemon -then there is no "slow-start-daemon.sh" process +then there is no "slow-start-daemon.py" process ~~~ -~~~{#slow-start-daemon.sh .file .sh .numberLines} -#!/bin/bash +~~~{#slow-start-daemon.py .file .python .numberLines} +#!/usr/bin/env python3 -set -euo pipefail +import socket +import time + +time.sleep(2) + +s = socket.create_server(("", 8888)) +(conn, _) = s.accept() +conn.recv(1) +s.close() -sleep 2 -netcat -l 8888 > /dev/null -echo OK +print("OK") ~~~ # Daemon never opens the intended port |