SIGINT, SIGTERM and SIGHUP all shut the node down in order (SIGTERM is what a Kubernetes pod termination sends). A second signal, or 20 seconds (below the 30 s Kubernetes grants), stops the process at once, so a stuck shutdown cannot hang. The binary knows its version (allmende version, and the 'node running' log line); the image is built with the commit as version and 'make image' pushes it tagged with that commit, refusing a dirty tree. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
42 lines
1020 B
Go
42 lines
1020 B
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func quietLog() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) }
|
|
|
|
func TestAStuckShutdownEndsAfterTheDeadline(t *testing.T) {
|
|
code := make(chan int, 1)
|
|
forceExitOnSecondSignal(quietLog(), 50*time.Millisecond, func(c int) { code <- c })
|
|
select {
|
|
case c := <-code:
|
|
if c != 1 {
|
|
t.Fatalf("exit code %d, want 1", c)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("the deadline did not end the process")
|
|
}
|
|
}
|
|
|
|
func TestASecondSignalEndsTheShutdownAtOnce(t *testing.T) {
|
|
code := make(chan int, 1)
|
|
forceExitOnSecondSignal(quietLog(), time.Minute, func(c int) { code <- c })
|
|
// Signals sent to our own process reach the handler registered above.
|
|
if err := syscall.Kill(syscall.Getpid(), syscall.SIGHUP); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case c := <-code:
|
|
if c != 1 {
|
|
t.Fatalf("exit code %d, want 1", c)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("a second signal did not end the process")
|
|
}
|
|
}
|