feat: add allocated server readiness control

This commit is contained in:
Josh Creek
2026-09-01 09:12:10 +01:00
parent 5b80c97337
commit 6caf719158
8 changed files with 218 additions and 4 deletions
+21
View File
@@ -0,0 +1,21 @@
extends "res://tests/test_case.gd"
const ServerControlScript = preload("res://scripts/server_control.gd")
func test_control_rejects_invalid_port_and_starts_loopback_listener() -> void:
var control = ServerControlScript.new()
assert_eq(control.start(0), ERR_INVALID_PARAMETER, "control rejects port zero")
var port := 18000 + (Time.get_ticks_usec() % 1000)
assert_eq(control.start(port, "drain-secret"), OK, "control starts on a valid loopback port")
control.stop()
control.queue_free()
func test_process_ready_and_drain_state_are_monotonic() -> void:
var control = ServerControlScript.new()
assert_true(not control.is_draining(), "control starts non-draining")
control.set_process_ready(true)
assert_true(not control.is_draining(), "process readiness does not imply draining")
control.stop()
control.queue_free()
+51
View File
@@ -0,0 +1,51 @@
extends SceneTree
const ServerControlScript = preload("res://scripts/server_control.gd")
const PORT := 18080
func _init() -> void:
var control = ServerControlScript.new()
root.add_child(control)
if control.start(PORT, "drain-secret") != OK:
printerr("server control failed to bind")
quit(1)
return
control.set_process_ready(true)
await process_frame
var ready_response := await _request("GET", "/ready", [])
if ready_response != 200:
printerr("ready response was %d" % ready_response)
quit(1)
return
var unauthorized := await _request("POST", "/drain", ["Authorization: Bearer wrong"])
if unauthorized != 401:
printerr("unauthorized drain response was %d" % unauthorized)
quit(1)
return
var drained := await _request("POST", "/drain", ["Authorization: Bearer drain-secret"])
if drained != 202 or not control.is_draining():
printerr("authorized drain response/state was %d/%s" % [drained, control.is_draining()])
quit(1)
return
var not_ready := await _request("GET", "/ready", [])
if not_ready != 503:
printerr("draining ready response was %d" % not_ready)
quit(1)
return
control.stop()
print("server control smoke passed")
quit(0)
func _request(method: String, path: String, headers: PackedStringArray) -> int:
var request := HTTPRequest.new()
root.add_child(request)
var http_method := HTTPClient.METHOD_GET if method == "GET" else HTTPClient.METHOD_POST
var err := request.request("http://127.0.0.1:%d%s" % [PORT, path], headers, http_method)
if err != OK:
request.queue_free()
return -1
var result = await request.request_completed
request.queue_free()
return int(result[1])