From 00a3d84256f6a4f16ba4b5838eca5834f4f2e1da Mon Sep 17 00:00:00 2001 From: Alexander Batischev Date: Mon, 26 Jul 2021 21:47:59 +0300 Subject: Fix daemon.py tests' dependence on netcat This commit fixes two intertwined problems: 1) with traditional netcat, `netcat -l 8888` treats "8888" as a host, and never starts listening on port 8888. This is fixed by replacing netcat with a Python script (as suggested by Lars Wirzenius); 2) Subplot's test suite never notices the above problem because, even though it fails to connect to the port, this error is intentionally swallowed by `_daemon_start_soonish()`. This is fixed by adding an explicit check to ensure that the daemon started after all. Furthermore, the scenario description is reworded to make it clear that we *do* expect the daemon to start, it'll just take a while. --- share/python/lib/daemon.md | 41 +++++++++++++++++++++++------------------ share/python/lib/daemon.py | 5 +++++ share/python/lib/daemon.yaml | 3 +++ 3 files changed, 31 insertions(+), 18 deletions(-) (limited to 'share') 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 -- cgit v1.2.1