Files
sendico/api/pkg/discovery/registry_stale_test.go
2026-02-27 02:53:55 +01:00

106 lines
2.9 KiB
Go

package discovery
import (
"testing"
"time"
)
func TestRegistryDeleteStale_RemovesOnlyExpiredEntries(t *testing.T) {
now := time.Date(2026, 2, 27, 12, 0, 0, 0, time.UTC)
registry := NewRegistry()
stale := RegistryEntry{
ID: "stale-gateway",
InstanceID: "stale-instance",
Service: "CARD",
Rail: RailCardPayout,
Operations: []string{OperationSend},
Version: "3.0.0",
Status: "ok",
Health: HealthParams{TimeoutSec: 30},
LastHeartbeat: now.Add(-31 * time.Second),
}
fresh := RegistryEntry{
ID: "fresh-gateway",
InstanceID: "fresh-instance",
Service: "CARD",
Rail: RailCardPayout,
Operations: []string{OperationSend},
Version: "3.0.0",
Status: "ok",
Health: HealthParams{TimeoutSec: 30},
LastHeartbeat: now.Add(-5 * time.Second),
}
unhealthyButRecent := RegistryEntry{
ID: "degraded-gateway",
InstanceID: "degraded-instance",
Service: "CARD",
Rail: RailCardPayout,
Operations: []string{OperationSend},
Version: "3.0.0",
Status: "degraded",
Health: HealthParams{TimeoutSec: 30},
LastHeartbeat: now.Add(-5 * time.Second),
}
registry.UpsertEntry(stale, now)
registry.UpsertEntry(fresh, now)
registry.UpsertEntry(unhealthyButRecent, now)
removed := registry.DeleteStale(now)
if len(removed) != 1 {
t.Fatalf("unexpected removed count: got=%d want=1", len(removed))
}
wantKey := registryEntryKey(normalizeEntry(stale))
if removed[0] != wantKey {
t.Fatalf("unexpected removed key: got=%q want=%q", removed[0], wantKey)
}
all := registry.List(now, false)
if len(all) != 2 {
t.Fatalf("unexpected remaining entries count: got=%d want=2", len(all))
}
}
func TestRegistryDeleteStale_UsesPerEntryTimeout(t *testing.T) {
now := time.Date(2026, 2, 27, 12, 0, 0, 0, time.UTC)
registry := NewRegistry()
shortTimeout := RegistryEntry{
ID: "short-timeout",
InstanceID: "short-timeout",
Service: "CARD",
Rail: RailCardPayout,
Operations: []string{OperationSend},
Version: "3.0.0",
Status: "ok",
Health: HealthParams{TimeoutSec: 5},
LastHeartbeat: now.Add(-21 * time.Second),
}
longTimeout := RegistryEntry{
ID: "long-timeout",
InstanceID: "long-timeout",
Service: "CARD",
Rail: RailCardPayout,
Operations: []string{OperationSend},
Version: "3.0.0",
Status: "ok",
Health: HealthParams{TimeoutSec: 60},
LastHeartbeat: now.Add(-6 * time.Second),
}
registry.UpsertEntry(shortTimeout, now)
registry.UpsertEntry(longTimeout, now)
removed := registry.DeleteStale(now)
if len(removed) != 1 {
t.Fatalf("unexpected removed count: got=%d want=1", len(removed))
}
wantKey := registryEntryKey(normalizeEntry(shortTimeout))
if removed[0] != wantKey {
t.Fatalf("unexpected removed key: got=%q want=%q", removed[0], wantKey)
}
}