package registry import ( "fmt" "reflect" "testing" ) func TestArtifactHosts(t *testing.T) { for _, mode := range []string{"empty", "union", "missing-timers", "closed", "limit", "overflow"} { t.Run(mode, func(t *testing.T) { r := openTestRegistry(t) defer r.Close() var want []string switch mode { case "union": for _, host := range []string{"route-only", "both"} { if err := r.Upsert(Route{Host: host}); err != nil { t.Fatal(err) } } for _, host := range []string{"orphan", "both"} { if err := r.ReplaceTimers(host, []Timer{{Host: host, Name: "paused", Kind: "cron", NextAt: 0}, {Host: host, Name: "done", Kind: "once", NextAt: -1}}); err != nil { t.Fatal(err) } } want = []string{"both", "orphan", "route-only"} case "missing-timers": if _, err := r.sql.Exec("DROP TABLE timers"); err != nil { t.Fatal(err) } case "closed": if err := r.Close(); err != nil { t.Fatal(err) } case "limit", "overflow": n := 8192 if mode == "overflow" { n++ } tx, err := r.sql.Begin() if err != nil { t.Fatal(err) } defer tx.Rollback() for i := 0; i < n; i++ { host := fmt.Sprintf("host-%05d", i) if _, err := tx.Exec("INSERT INTO timers (host,name,kind,spec,path,next_at,created_at) VALUES (?, 'paused', 'cron', '', '', 0, 1)", host); err != nil { t.Fatal(err) } want = append(want, host) } if err := tx.Commit(); err != nil { t.Fatal(err) } } got, err := r.ArtifactHosts() if mode == "closed" || mode == "missing-timers" || mode == "overflow" { if err == nil || got != nil { t.Fatalf("incomplete discovery: count=%d error=%v", len(got), err) } return } if err != nil || !reflect.DeepEqual(got, want) { t.Fatalf("host discovery: count=%d want=%d error=%v", len(got), len(want), err) } }) } }