Files
anchorage/internal/pkg/placement/rendezvous_test.go
T
57_Wolve 12bf35caf8 anchorage v1.0 initial tree
Greenfield Go multi-tenant IPFS Pinning Service wire-compatible with the
IPFS Pinning Services API spec. Paired 1:1 with Kubo over localhost RPC,
clustered via embedded NATS JetStream, Postgres source-of-truth with
RLS-enforced tenancy, Fiber + huma v2 for the HTTP surface, Authentik
OIDC for session login with kid-rotated HS256 JWT API tokens.

Feature-complete against the 22-milestone build plan, including the
ship-it v1.0 gap items:

  * admin CLIs: drain/uncordon, maintenance, mint-token, rotate-key,
    prune-denylist, rebalance --dry-run, cache-stats, cluster-presences
  * TTL leader election via NATS KV, fence tokens, JetStream dedup
  * rebalancer (plan/apply split), reconciler, requeue sweeper
  * ristretto caches with NATS-backed cross-node invalidation
    (placements live-nodes + token denylist)
  * maintenance watchdog for stuck cluster-pause flag
  * Prometheus /metrics with CIDR ACL, HTTP/pin/scheduler/cache gauges
  * rate limiting: session (10/min) + anonymous global (120/min)
  * integration tests: rebalance, refcount multi-org, RLS belt
  * goreleaser (tar + deb/rpm/apk + Alpine Docker) targeting Gitea

Stack: Cobra/Viper, Fiber v2 + huma v2, embedded NATS JetStream,
pgx/sqlc/golang-migrate, ristretto, TypeID, prometheus/client_golang,
testcontainers-go.
2026-04-16 18:13:36 -05:00

107 lines
2.8 KiB
Go

package placement_test
import (
"fmt"
"sort"
"testing"
"anchorage/internal/pkg/placement"
)
func nodes(ids ...string) []placement.Node {
out := make([]placement.Node, 0, len(ids))
for _, id := range ids {
out = append(out, placement.Node{ID: id, Status: "up"})
}
return out
}
func TestDeterministic(t *testing.T) {
ns := nodes("nod_a", "nod_b", "nod_c", "nod_d", "nod_e")
a := placement.Compute("org_1", "QmABC", 2, ns)
b := placement.Compute("org_1", "QmABC", 2, ns)
if fmt.Sprint(a) != fmt.Sprint(b) {
t.Errorf("non-deterministic: %v vs %v", a, b)
}
if len(a) != 2 {
t.Errorf("want 2 replicas, got %d", len(a))
}
}
func TestDistinctNodes(t *testing.T) {
ns := nodes("nod_a", "nod_b", "nod_c", "nod_d", "nod_e")
r := placement.Compute("org_1", "QmXYZ", 3, ns)
seen := map[string]bool{}
for _, id := range r {
if seen[id] {
t.Errorf("duplicate placement: %s in %v", id, r)
}
seen[id] = true
}
}
func TestUnderReplicatesOnSmallCluster(t *testing.T) {
ns := nodes("nod_a")
r := placement.Compute("org_1", "QmXYZ", 3, ns)
if len(r) != 1 {
t.Errorf("want 1 (cluster too small), got %d", len(r))
}
}
func TestZeroInputs(t *testing.T) {
if r := placement.Compute("", "", 0, nil); len(r) != 0 {
t.Errorf("empty inputs should yield empty slice, got %v", r)
}
}
func TestLoadSpread(t *testing.T) {
// Property test: with N nodes and many CIDs, each node should appear
// roughly 1/N of the time as the primary (first) replica. We allow
// ±30% tolerance; HRW is pseudo-random, not strictly uniform.
ns := nodes("nod_a", "nod_b", "nod_c", "nod_d", "nod_e")
counts := map[string]int{}
total := 5000
for i := 0; i < total; i++ {
cid := fmt.Sprintf("Qm_%d", i)
r := placement.Compute("org_1", cid, 1, ns)
counts[r[0]]++
}
if len(counts) != len(ns) {
t.Errorf("only %d nodes received primary load, want %d", len(counts), len(ns))
}
expected := total / len(ns)
for id, c := range counts {
diff := c - expected
if diff < 0 {
diff = -diff
}
if diff*100/expected > 30 {
t.Errorf("node %s got %d primaries, expected ~%d (±30%%)", id, c, expected)
}
}
}
func TestMinimalDisruptionOnNodeLoss(t *testing.T) {
// When a node disappears, only pins that *had* that node in their
// replica set should see any churn; everyone else stays put.
before := nodes("nod_a", "nod_b", "nod_c", "nod_d")
after := nodes("nod_a", "nod_b", "nod_c")
churn := 0
total := 1000
for i := 0; i < total; i++ {
cid := fmt.Sprintf("Qm_%d", i)
a := placement.Compute("org_1", cid, 2, before)
b := placement.Compute("org_1", cid, 2, after)
sort.Strings(a)
sort.Strings(b)
if fmt.Sprint(a) != fmt.Sprint(b) {
churn++
}
}
// Losing 1 of 4 nodes: roughly 2/4 = 50% of pins included that node
// in their top-2. We just assert "less than all" churned.
if churn >= total {
t.Errorf("everything churned (%d/%d) — HRW broken?", churn, total)
}
}