Files
archie/internal/infrastructure/eventbus/nats/embedded_test.go
T
sam c7987f0595 feat(nats): run an embedded server when no external NATS is configured
Wire github.com/nats-io/nats-server/v2 into production startup so a
single-process deployment gets reaction delivery and task distribution
over an in-process server rather than a separate one
(docs/prds/embedded-nats.md).

Meaning change: [nats].url empty used to mean "no NATS, use the SQLite
ClaimNext flow"; it now means "start an embedded NATS server". The old
behaviour is the explicit opt-out [nats].mode = "off". mode resolves from
url when unset (url set -> external, url empty -> embedded), and
agent.mode="nats" / [containers] now require mode="external" (an embedded
loopback server cannot be reached by agent workers or containers).

The daemon owns the embedded server's lifecycle: StartEmbedded before
dialing the client, Shutdown after the client closes (LIFO cleanups). The
embedded server binds loopback on a random port, enables JetStream in a
store dir under the data directory, and leaves its logger nil so a startup
failure surfaces as a Connect error rather than an os.Exit.

Retention becomes a nats.Config field (default WorkQueuePolicy) so a future
reaction stream can fan out under LimitsPolicy instead of the hardcoded
competing-consumer policy that starves a second consumer on overlapping
subjects.
2026-08-22 05:18:31 +10:00

56 lines
1.7 KiB
Go

package nats
import (
"context"
"testing"
"github.com/nats-io/nats.go"
)
// TestStartEmbeddedLifecycle proves the in-process server starts with
// JetStream enabled, accepts a client that provisions a stream, and stops on
// Shutdown. It is the production path behind docs/prds/embedded-nats.md,
// distinct from the test-only natstest helper used elsewhere.
func TestStartEmbeddedLifecycle(t *testing.T) {
srv, err := StartEmbedded(t.Context(), EmbeddedOptions{StoreDir: t.TempDir()}, discardLogger())
if err != nil {
t.Fatalf("StartEmbedded = %v", err)
}
url := srv.ClientURL()
if url == "" {
t.Fatal("ClientURL() = empty, want the bound address")
}
// Connecting and provisioning a stream proves JetStream is live, not just
// the core NATS socket.
c, err := Connect(t.Context(), Config{
URL: url,
Subjects: []string{"embedded.test.>"},
FilterSubject: "embedded.test.>",
}, discardLogger())
if err != nil {
t.Fatalf("Connect to embedded server = %v", err)
}
c.Close()
srv.Shutdown()
// After Shutdown the port must refuse new connections, proving the server
// owns its own stop rather than leaking the goroutine.
if _, err := nats.Connect(url); err == nil {
t.Fatalf("nats.Connect(%q) after Shutdown = nil, want connection refused", url)
}
}
// TestStartEmbeddedShutdownOnCancelledContext pins that a cancelled ctx aborts
// startup rather than blocking the full readiness window during shutdown
// racing boot.
func TestStartEmbeddedShutdownOnCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(t.Context())
cancel()
if _, err := StartEmbedded(ctx, EmbeddedOptions{StoreDir: t.TempDir()}, discardLogger()); err == nil {
t.Fatal("StartEmbedded(cancelled ctx) = nil, want an error")
}
}