Add a broker-neutral service registry contract (Endpoint, EventKind, Event, ServiceRegistry, ErrNotInstalled) and a NATS JetStream KV implementation, plus the registration side a service uses to announce itself. The contract's governing semantic -- NotInstalled is not Down -- is modeled by two buckets: a durable installed marker (never expires) distinct from expiring heartbeat keys. Instances heartbeat their key on an interval; a stopped process leaves via TTL expiry. Register writes the marker once then heartbeats; Resolve returns ErrNotInstalled only when the marker is absent, and an empty roster for installed-but-down. Watch dedupes heartbeat refreshes so it emits membership changes, not liveness pings. Unwired: no daemon/bootstrap path consumes it yet. Deliberately uses a dot key separator instead of the research sketch's slash so per-service watch wildcards match.
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package nats
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestWithDefaults(t *testing.T) {
|
|
cfg := (Config{}).withDefaults()
|
|
if cfg.Bucket != DefaultBucket {
|
|
t.Errorf("withDefaults().Bucket = %q, want %q", cfg.Bucket, DefaultBucket)
|
|
}
|
|
if cfg.InstalledBucket != DefaultInstalledBucket {
|
|
t.Errorf("withDefaults().InstalledBucket = %q, want %q", cfg.InstalledBucket, DefaultInstalledBucket)
|
|
}
|
|
if cfg.TTL != DefaultTTL {
|
|
t.Errorf("withDefaults().TTL = %v, want %v", cfg.TTL, DefaultTTL)
|
|
}
|
|
if cfg.Heartbeat != DefaultHeartbeat {
|
|
t.Errorf("withDefaults().Heartbeat = %v, want %v", cfg.Heartbeat, DefaultHeartbeat)
|
|
}
|
|
}
|
|
|
|
func TestValidate(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
cfg Config
|
|
wantErr bool
|
|
}{
|
|
{name: "valid", cfg: Config{URL: "nats://x", TTL: 15 * time.Second, Heartbeat: 5 * time.Second}, wantErr: false},
|
|
{name: "missing url", cfg: Config{TTL: time.Second, Heartbeat: 200 * time.Millisecond}, wantErr: true},
|
|
{name: "heartbeat equals ttl", cfg: Config{URL: "nats://x", TTL: time.Second, Heartbeat: time.Second}, wantErr: true},
|
|
{name: "heartbeat exceeds ttl", cfg: Config{URL: "nats://x", TTL: time.Second, Heartbeat: 2 * time.Second}, wantErr: true},
|
|
{name: "negative durations", cfg: Config{URL: "nats://x", TTL: -time.Second, Heartbeat: time.Second}, wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.cfg.Validate()
|
|
if tt.wantErr {
|
|
if !errors.Is(err, ErrInvalidConfig) {
|
|
t.Fatalf("Validate() = %v, want ErrInvalidConfig", err)
|
|
}
|
|
} else if err != nil {
|
|
t.Fatalf("Validate() = %v, want nil", err)
|
|
}
|
|
})
|
|
}
|
|
}
|