diff --git a/internal/infrastructure/servicediscovery/nats/registry.go b/internal/infrastructure/servicediscovery/nats/registry.go index 99582d7a6..7a7d39b4e 100644 --- a/internal/infrastructure/servicediscovery/nats/registry.go +++ b/internal/infrastructure/servicediscovery/nats/registry.go @@ -45,10 +45,10 @@ type Client struct { // On any failure the partially-built connection is closed before returning, so // a failed Connect leaks nothing. func Connect(ctx context.Context, cfg Config, log *slog.Logger) (*Client, error) { + cfg = cfg.withDefaults() if err := cfg.Validate(); err != nil { return nil, err } - cfg = cfg.withDefaults() if log == nil { log = slog.New(slog.DiscardHandler) } diff --git a/internal/infrastructure/servicediscovery/nats/registry_test.go b/internal/infrastructure/servicediscovery/nats/registry_test.go index 1b7f56b1a..c369a1e14 100644 --- a/internal/infrastructure/servicediscovery/nats/registry_test.go +++ b/internal/infrastructure/servicediscovery/nats/registry_test.go @@ -23,6 +23,17 @@ func testConfig() Config { return Config{TTL: time.Second, Heartbeat: 200 * time.Millisecond} } +// TestConnectRejectsTTLBelowDefaultHeartbeat pins the Connect ordering: a +// caller that sets TTL below DefaultHeartbeat and leaves Heartbeat unset must +// be refused as ErrInvalidConfig, not silently given a registry whose live +// entries expire between refreshes (DefaultHeartbeat >= TTL). +func TestConnectRejectsTTLBelowDefaultHeartbeat(t *testing.T) { + _, err := Connect(context.Background(), Config{URL: "nats://127.0.0.1:1", TTL: 2 * time.Second}, discardLogger()) + if !errors.Is(err, ErrInvalidConfig) { + t.Fatalf("Connect() = %v, want ErrInvalidConfig", err) + } +} + // newServer starts a real in-process NATS server with JetStream enabled. This // is the established test technique for the NATS infrastructure (see the // sibling eventbus/nats retention test): a real server, not a mocked client.