package leader_test import ( "context" "sync/atomic" "testing" "time" "anchorage/internal/pkg/leader" embeddednats "anchorage/internal/pkg/nats" ) // startEmbedded spins up an in-process NATS server for this test. func startEmbedded(t *testing.T) *embeddednats.Server { t.Helper() srv, err := embeddednats.Start(context.Background(), embeddednats.ServerConfig{ ServerName: "leader-test", DataDir: t.TempDir(), ClientHost: "127.0.0.1", ClientPort: -1, JSReplicas: 1, }) if err != nil { t.Fatalf("embeddednats.Start: %v", err) } t.Cleanup(srv.Close) return srv } func TestSingleCandidatePromotes(t *testing.T) { srv := startEmbedded(t) var promoted, demoted atomic.Int32 el, err := leader.New(context.Background(), srv.JS, leader.Options{ NodeID: "nod_solo", TTL: time.Second, RenewInterval: 200 * time.Millisecond, ClaimInterval: 200 * time.Millisecond, OnPromote: func(context.Context) { promoted.Add(1) }, OnDemote: func(context.Context) { demoted.Add(1) }, }) if err != nil { t.Fatalf("leader.New: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() go func() { _ = el.Run(ctx) }() // Wait up to 2s for promotion. deadline := time.Now().Add(2 * time.Second) for time.Now().Before(deadline) { if el.IsLeader() { break } time.Sleep(50 * time.Millisecond) } if !el.IsLeader() { t.Fatalf("candidate never promoted within 2s") } if promoted.Load() != 1 { t.Errorf("OnPromote fired %d times, want 1", promoted.Load()) } cancel() time.Sleep(300 * time.Millisecond) if demoted.Load() < 1 { t.Errorf("OnDemote did not fire on ctx cancel") } } func TestTwoCandidatesExactlyOneWins(t *testing.T) { srv := startEmbedded(t) mk := func(id string) *leader.Elector { el, err := leader.New(context.Background(), srv.JS, leader.Options{ NodeID: id, TTL: time.Second, RenewInterval: 200 * time.Millisecond, ClaimInterval: 200 * time.Millisecond, }) if err != nil { t.Fatalf("leader.New: %v", err) } return el } a := mk("nod_a") b := mk("nod_b") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() go func() { _ = a.Run(ctx) }() go func() { _ = b.Run(ctx) }() deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { if a.IsLeader() || b.IsLeader() { break } time.Sleep(50 * time.Millisecond) } if a.IsLeader() == b.IsLeader() { t.Fatalf("both candidates report leader=%v (expected exactly one true)", a.IsLeader()) } }