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.
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package servicediscovery
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
// TestEventKindString pins the human-readable form of each kind, including the
|
|
// zero value (unset/unknown), so logging and diagnostics never render a magic
|
|
// number.
|
|
func TestEventKindString(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
kind EventKind
|
|
want string
|
|
}{
|
|
{name: "join", kind: Join, want: "join"},
|
|
{name: "leave", kind: Leave, want: "leave"},
|
|
{name: "unknown out-of-range", kind: EventKind(99), want: "unknown"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := tt.kind.String(); got != tt.want {
|
|
t.Errorf("EventKind(%d).String() = %q, want %q", tt.kind, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestErrNotInstalledWraps pins that implementations can wrap the sentinel and
|
|
// callers can still match it with errors.Is, which is the contract's intended
|
|
// use (a broker or registry specific error may be joined around it).
|
|
func TestErrNotInstalledWraps(t *testing.T) {
|
|
wrapped := fmt.Errorf("resolve %s: %w", "curator", ErrNotInstalled)
|
|
if !errors.Is(wrapped, ErrNotInstalled) {
|
|
t.Fatalf("wrapped ErrNotInstalled does not match errors.Is: %v", wrapped)
|
|
}
|
|
}
|