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.
119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
package nats
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats-server/v2/server"
|
|
natstest "github.com/nats-io/nats-server/v2/test"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"github.com/samcharles93/archie-core/internal/eventbus"
|
|
)
|
|
|
|
func discardLogger() *slog.Logger { return slog.New(slog.DiscardHandler) }
|
|
|
|
// TestWithDefaultsRetention pins the retention default: unset defaults to
|
|
// WorkQueuePolicy (task distribution), and an explicit LimitsPolicy is left
|
|
// alone. The pointer form is what lets "unset" default to WorkQueuePolicy
|
|
// when the policy's zero value (LimitsPolicy) is a valid explicit choice.
|
|
func TestWithDefaultsRetention(t *testing.T) {
|
|
t.Run("unset defaults to work-queue", func(t *testing.T) {
|
|
cfg := (Config{}).withDefaults()
|
|
if cfg.Retention == nil {
|
|
t.Fatal("withDefaults().Retention = nil, want non-nil")
|
|
}
|
|
if *cfg.Retention != jetstream.WorkQueuePolicy {
|
|
t.Fatalf("withDefaults().Retention = %v, want %v", *cfg.Retention, jetstream.WorkQueuePolicy)
|
|
}
|
|
})
|
|
|
|
t.Run("explicit limits is preserved", func(t *testing.T) {
|
|
limits := jetstream.LimitsPolicy
|
|
cfg := (Config{Retention: &limits}).withDefaults()
|
|
if cfg.Retention == nil || *cfg.Retention != jetstream.LimitsPolicy {
|
|
t.Fatalf("withDefaults().Retention = %v, want %v", cfg.Retention, jetstream.LimitsPolicy)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestConnectRetentionFanout proves the reason retention must be a Config
|
|
// field: under LimitsPolicy two independent consumers each receive every
|
|
// matching message (fan-out), the behaviour reactions need and WorkQueuePolicy
|
|
// cannot provide. This is the exact trap CLAUDE.md documents -- a second
|
|
// consumer on an overlapping filter subject under WorkQueuePolicy silently
|
|
// gets nothing.
|
|
func TestConnectRetentionFanout(t *testing.T) {
|
|
srv := natstest.RunServer(&server.Options{Port: -1, JetStream: true, StoreDir: t.TempDir()})
|
|
t.Cleanup(srv.Shutdown)
|
|
|
|
limits := jetstream.LimitsPolicy
|
|
c, err := Connect(t.Context(), Config{
|
|
URL: srv.ClientURL(),
|
|
StreamName: "REACTIONS_FANOUT_TEST",
|
|
Subjects: []string{"reaction.fanout.test.>"},
|
|
FilterSubject: "reaction.fanout.test.>",
|
|
Retention: &limits,
|
|
}, discardLogger())
|
|
if err != nil {
|
|
t.Fatalf("Connect = %v", err)
|
|
}
|
|
t.Cleanup(c.Close)
|
|
|
|
var mu sync.Mutex
|
|
gotA := map[string]int{}
|
|
gotB := map[string]int{}
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
done := make(chan struct{}, 2)
|
|
subscriber := func(recv map[string]int) {
|
|
defer func() { done <- struct{}{} }()
|
|
_ = c.Subscribe(ctx, "reaction.fanout.test.>", func(m eventbus.Message) error {
|
|
mu.Lock()
|
|
recv[string(m.Data())]++
|
|
mu.Unlock()
|
|
return nil
|
|
})
|
|
}
|
|
go subscriber(gotA)
|
|
go subscriber(gotB)
|
|
|
|
// Publish after the consumers have had a chance to be created, but the
|
|
// Limits stream retains messages regardless of subscriber timing.
|
|
time.Sleep(200 * time.Millisecond)
|
|
if err := c.Publish(t.Context(), "reaction.fanout.test.1", []byte("x")); err != nil {
|
|
t.Fatalf("Publish x = %v", err)
|
|
}
|
|
if err := c.Publish(t.Context(), "reaction.fanout.test.2", []byte("y")); err != nil {
|
|
t.Fatalf("Publish y = %v", err)
|
|
}
|
|
|
|
// Both consumers must observe both messages; poll rather than sleep a
|
|
// fixed duration so a slow CI runner does not flake.
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
mu.Lock()
|
|
aFull := gotA["x"] >= 1 && gotA["y"] >= 1
|
|
bFull := gotB["x"] >= 1 && gotB["y"] >= 1
|
|
mu.Unlock()
|
|
if aFull && bFull {
|
|
break
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
cancel()
|
|
<-done
|
|
<-done
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for name, got := range map[string]map[string]int{"A": gotA, "B": gotB} {
|
|
if got["x"] != 1 || got["y"] != 1 {
|
|
t.Errorf("consumer %s received %v, want x and y once each (fan-out failed)", name, got)
|
|
}
|
|
}
|
|
}
|