Files
archie/internal/app/archied/gateway_test.go
T
sam 7a38d9b79f fix(deploy): announce systemd READY from archie-gateway and archie-state-store
2fcc32d8 gave archied sd_notify: one READY=1 when the run loop starts serving.
RunGateway and RunStateStore serve contracts and health surfaces of their own --
the State Store even takes -ready-addr for a readiness HTTP listener -- but told
systemd nothing, so run as Type=notify either would hang until TimeoutStartSec
and be killed in a restart loop while perfectly healthy (archie-core-1174).

Both now announce at the moment each declares itself serving, the same notion
archied's runLoop announces beside healthSurface.markServing -- no second
definition of started. The Gateway announces from its serving tail
(serveGatewayListener, extracted from RunGateway), after the listener is bound;
the State Store announces after its optional surfaces have started, because a
readiness surface that fails to bind fails the boot and READY must not precede
it, and before serveStateStore, which begins accepting the moment it is called.
No unit file changes Type=notify for them; the helper already lives in package
archied beside both entry points, so no move was needed.

Tests follow the notify pattern the daemon's announcement already has: the
Gateway test drives its serving tail over a bound loopback listener with a
stub contract and judges READY against a real Snapshot RPC on the same socket;
the State Store test drives the real RunStateStore boot over a pgtest database
and waits for the datagram. Both failed red before the announcement existed --
no datagram within the window -- and pass after.
2026-09-26 20:39:56 +10:00

137 lines
4.6 KiB
Go

package archied
import (
"context"
"log/slog"
"net"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "github.com/samcharles93/archie-core/internal/contracts/gateway/v1"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/taskactions"
"github.com/samcharles93/archie-core/internal/taskstate"
)
func TestGatewayServerOptsLoopbackIsInsecure(t *testing.T) {
opts, loopback, err := gatewayServerOpts("127.0.0.1:8585", "")
if err != nil {
t.Fatalf("gatewayServerOpts(loopback, no token) error: %v", err)
}
if !loopback {
t.Fatal("loopback listener should report loopback")
}
if len(opts) != 0 {
t.Fatalf("loopback listener should have no server opts, got %d", len(opts))
}
}
func TestGatewayServerOptsNonLoopbackRequiresToken(t *testing.T) {
if _, _, err := gatewayServerOpts("0.0.0.0:8585", ""); err == nil {
t.Fatal("non-loopback listener without a token should fail closed")
}
opts, loopback, err := gatewayServerOpts("0.0.0.0:8585", "secret")
if err != nil {
t.Fatalf("non-loopback listener with a token error: %v", err)
}
if loopback {
t.Fatal("non-loopback listener should not report loopback")
}
if len(opts) != 2 {
t.Fatalf("non-loopback listener should install two server options (unary and stream auth interceptors), got %d", len(opts))
}
}
func TestGatewayServerOptsMalformedListen(t *testing.T) {
if _, _, err := gatewayServerOpts("not-an-address", ""); err == nil {
t.Fatal("malformed listen address should error")
}
}
// stubGatewayContract satisfies messaging.ChatContract with zero values: the
// serve test exercises the listener, the READY announcement and one read RPC,
// not the chat runtime.
type stubGatewayContract struct{}
func (stubGatewayContract) Snapshot(context.Context) (messaging.ChatSnapshot, error) {
return messaging.ChatSnapshot{}, nil
}
func (stubGatewayContract) GetSession(context.Context, string) (messaging.SessionContext, bool, error) {
return messaging.SessionContext{}, false, nil
}
func (stubGatewayContract) RecentMessages(context.Context, string, int) ([]messaging.Message, error) {
return nil, nil
}
func (stubGatewayContract) RecentTurns(context.Context, string, int) ([]messaging.TurnRecord, error) {
return nil, nil
}
func (stubGatewayContract) Route(context.Context, messaging.Inbound) (messaging.ChatReply, error) {
return messaging.ChatReply{}, nil
}
func (stubGatewayContract) Stream(context.Context, messaging.Inbound) (<-chan messaging.ChatEvent, error) {
return nil, nil
}
func (stubGatewayContract) Cancel(context.Context, string) (messaging.ChatCancellation, error) {
return messaging.ChatCancellation{}, nil
}
func (stubGatewayContract) SetPersona(context.Context, string, string) (bool, error) {
return false, nil
}
func (stubGatewayContract) ApplyTaskAction(context.Context, string, int64, taskstate.Action) (messaging.TaskActionResult, error) {
return messaging.TaskActionResult{}, nil
}
func (stubGatewayContract) ApplyOperatorTaskAction(context.Context, taskactions.Actor, int64, taskstate.Action) (messaging.TaskActionResult, error) {
return messaging.TaskActionResult{}, nil
}
// TestGatewayServingAnnouncesReady: archie-gateway run as a Type=notify unit
// must send READY=1 at the moment it declares itself serving, or systemd kills
// a healthy process once TimeoutStartSec expires (archie-core-1174). The
// announcement is judged against the same evidence archied's run loop uses:
// the contract answers over the bound listener, so READY asserts a serving
// process rather than a bound socket.
func TestGatewayServingAnnouncesReady(t *testing.T) {
listener := newNotifyListener(t)
b := &boot{log: slog.New(slog.DiscardHandler)}
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
serveCh := make(chan error, 1)
go func() { serveCh <- b.serveGatewayListener(ctx, ln, true, stubGatewayContract{}, nil) }()
if !listener.waitForState(t, 5*time.Second, readyState) {
t.Fatal("no READY=1 datagram within 5s of the Gateway serving")
}
conn, err := grpc.NewClient("passthrough:///"+ln.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("dial: %v", err)
}
defer conn.Close()
if _, err := pb.NewChatServiceClient(conn).Snapshot(t.Context(), &pb.SnapshotRequest{}); err != nil {
t.Fatalf("Snapshot over gRPC: %v", err)
}
cancel()
if err := <-serveCh; err != nil {
t.Fatalf("serveGatewayListener returned: %v", err)
}
}