Skip to content
quarry Artifact [31d18ee871]
Login

Artifact [31d18ee871]

Artifact 31d18ee871082c144bbbab60b51329cc83e9a71201ac8c0f0908f388561e53e5:


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)
	}
	// The counts arrive some other way (a rebuild) while it is queued:
	// the task finds them done and does not crosslink. p.cli is nil, so a
	// task that went on would panic.
	if err := ix.MarkLineCountsDone(); err != nil {
		t.Fatal(err)
	}
	if err := run(context.Background()); err != nil {
		t.Errorf("FAIL: a backfill already done returned %v", err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}