feat: bound supervisor drain termination

This commit is contained in:
Josh Creek
2026-09-01 09:18:19 +01:00
parent e7c835af52
commit 58508bd87c
4 changed files with 108 additions and 3 deletions
+50
View File
@@ -180,3 +180,53 @@ func TestSupervisorRejectsRemoteOrPartialDrainConfiguration(t *testing.T) {
}
}
}
func TestRunDrainsBeforeChildExit(t *testing.T) {
marker := filepath.Join(t.TempDir(), "drained")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/drain" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.Header.Get("Authorization") != "Bearer run-secret" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if err := os.WriteFile(marker, []byte("drained"), 0600); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusAccepted)
}))
defer server.Close()
command := []string{"/bin/sh", "-c", "while [ ! -f '" + marker + "' ]; do sleep 0.01; done"}
s, err := New(Config{Command: command, DrainURL: server.URL + "/drain", DrainToken: "run-secret"})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(30*time.Millisecond, cancel)
if err := s.Run(ctx, time.Second); err != nil {
t.Fatalf("graceful run: %v", err)
}
if _, err := os.Stat(marker); err != nil {
t.Fatalf("drain endpoint was not called: %v", err)
}
}
func TestRunForceKillsUnresponsiveChildAtDeadline(t *testing.T) {
s, err := New(Config{Command: []string{"/bin/sh", "-c", "trap '' TERM; sleep 5"}})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(30*time.Millisecond, cancel)
started := time.Now()
err = s.Run(ctx, 50*time.Millisecond)
if err == nil || !strings.Contains(err.Error(), "force-killed") {
t.Fatalf("unresponsive child result = %v", err)
}
if elapsed := time.Since(started); elapsed > time.Second {
t.Fatalf("force-kill exceeded bounded deadline: %s", elapsed)
}
}