package ingest
import (
"context"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/index"
)
// With a maintenance lane set, the backfill is handed to it once and
// takes no rebuild slot; the lane decides when it runs.
func TestLineCountBackfillGoesToTheMaintenanceLane(t *testing.T) {
ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
defer ix.Close()
if ix.LineCountsDone() {
t.Skip("a fresh index is already marked counted")
}
p := &Pipeline{ix: ix, code: "x", stop: make(chan struct{})}
var tasks []string
var run func(context.Context) error
p.SetMaintenance(func(task string, f func(context.Context) error) bool {
tasks, run = append(tasks, task), f
return true
})
before := Rebuilds().Started
p.backfillLineCounts()
p.backfillLineCounts()
if len(tasks) != 1 || tasks[0] != "line counts" || run == nil {
t.Errorf("FAIL: scheduled %v, want one line counts task", tasks)
}
if got := Rebuilds().Started; got != before {
t.Errorf("FAIL: the backfill took a rebuild slot (%d -> %d)", before, got)
}
if !t.Failed() {
t.Log("ok")
}
}