summaryrefslogtreecommitdiff
path: root/share
diff options
context:
space:
mode:
Diffstat (limited to 'share')
-rw-r--r--share/python/lib/daemon.md41
-rw-r--r--share/python/lib/daemon.py5
-rw-r--r--share/python/lib/daemon.yaml3
3 files changed, 31 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
diff --git a/share/python/lib/daemon.py b/share/python/lib/daemon.py
index 0b47fe9..a62ca24 100644
--- a/share/python/lib/daemon.py
+++ b/share/python/lib/daemon.py
@@ -220,6 +220,11 @@ def daemon_start_fails_with(ctx, message=None):
assert message.lower() in error.lower()
+def daemon_start_succeeds(ctx):
+ daemon = ctx.declare("_daemon")
+ assert "_start_error" not in daemon
+
+
def daemon_get_stdout(ctx, name):
return _daemon_get_output(ctx, name, "stdout")
diff --git a/share/python/lib/daemon.yaml b/share/python/lib/daemon.yaml
index 4fab1f6..424a98f 100644
--- a/share/python/lib/daemon.yaml
+++ b/share/python/lib/daemon.yaml
@@ -29,6 +29,9 @@
- then: starting daemon fails with "{message:text}"
function: daemon_start_fails_with
+- then: starting the daemon succeeds
+ function: daemon_start_succeeds
+
- then: daemon {name} stdout is "{text:text}"
function: daemon_stdout_is