summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-07-19 11:46:40 +0300
committerLars Wirzenius <liw@liw.fi>2020-07-19 13:28:44 +0300
commitee3c218fea70d02d0bc0e1e4baae8cb8c988074c (patch)
tree50068317716c841704736709bfb7e4f825c446d1
parente50eea1299a337a4c2164e069ab0bfa65609424f (diff)
downloadewww-ee3c218fea70d02d0bc0e1e4baae8cb8c988074c.tar.gz
test: wait for ewww to start, and log stderr if it doesn't
When the ewww daemon starts successfully, it'll listen on the assigned port. We wait for that port to be open, for up to five seconds. If that fails, then ewww didn't start successfully, and hopefully there is something useful in its stderr so we read and log that.
-rw-r--r--ewww.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/ewww.py b/ewww.py
index ded5064..8a6e453 100644
--- a/ewww.py
+++ b/ewww.py
@@ -8,6 +8,7 @@ import random
import re
import shutil
import signal
+import socket
import subprocess
import time
import urllib.parse
@@ -82,7 +83,7 @@ def start_server(ctx, filename=None):
logging.debug(f"Starting ewww with config file {filename}")
config = get_file(filename).decode("UTF-8")
config = yaml.safe_load(config)
- config["port"] = random.randint(2000, 30000)
+ port = config["port"] = random.randint(2000, 30000)
logging.debug(f"Picked randomly port for ewww: {config['port']}")
ctx["config"] = config
config = yaml.safe_dump(config)
@@ -90,6 +91,24 @@ def start_server(ctx, filename=None):
start_daemon(ctx, "ewww", [_binary("ewww"), filename])
+ if not port_open("localhost", port, 5.0):
+ stderr = open(ctx["daemon"]["ewww"]["stderr"]).read()
+ logging.debug(f"Stderr from daemon: {stderr!r}")
+
+
+# Wait for a port to be open
+def port_open(host, port, timeout):
+ logging.debug(f"Waiting for port localhost:{port} to be available")
+ started = time.time()
+ while time.time() < started + timeout:
+ try:
+ socket.create_connection((host, port), timeout=timeout)
+ return True
+ except socket.error:
+ pass
+ logging.error(f"Port localhost:{port} is not open")
+ return False
+
# Stop previously started server.
def stop_server(ctx):