Homepage (TDD slice 2): the http probe method
Red test first: a raw TCP listener that never speaks HTTP must read down under an http probe (up under tcp), while a real HTTP server reads up. This test caught a real hang — the backgrounded listener inherited the test runner's stdout pipe and held it open; fixed by redirecting its output and killing both listeners in teardown. scripts/homepage-status.py: probe() now dispatches on method. http does an urlopen (any HTTP status = up; no connection = down); tcp connects. The checker is now complete: tcp + http + wip + atomic write + generated_at. Remaining slices: services.json schema, the pages + theme + toggle, the Caddyfile (validate + two hashes), the git-driven deploy.
This commit is contained in:
@@ -11,6 +11,8 @@ import os
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from datetime import datetime, timezone
|
||||
|
||||
TIMEOUT = 5 # seconds, per probe (Q11 / Q40)
|
||||
@@ -18,6 +20,17 @@ TIMEOUT = 5 # seconds, per probe (Q11 / Q40)
|
||||
|
||||
def probe(host, port, method="tcp"):
|
||||
"""Return 'up' if the backend answers, else 'down'."""
|
||||
if method == "http":
|
||||
try:
|
||||
urllib.request.urlopen(
|
||||
urllib.request.Request("http://%s:%d/" % (host, port)),
|
||||
timeout=TIMEOUT,
|
||||
)
|
||||
return "up"
|
||||
except urllib.error.HTTPError:
|
||||
return "up" # an HTTP response of any status means the backend is up
|
||||
except Exception:
|
||||
return "down"
|
||||
try:
|
||||
with socket.create_connection((host, port), timeout=TIMEOUT):
|
||||
return "up"
|
||||
|
||||
Reference in New Issue
Block a user