package index
import (
"fmt"
"testing"
"time"
)
// A step of opening an index is logged when it is slow, and only then,
// each timed from the end of the one before.
func TestOpenLogsOnlyItsSlowSteps(t *testing.T) {
at := time.Unix(1000, 0)
var logged []string
lap := openLaps("x.db", func() time.Time { return at }, func(f string, a ...any) {
logged = append(logged, fmt.Sprintf(f, a...))
})
at = at.Add(100 * time.Millisecond)
lap("quick")
at = at.Add(75 * time.Second)
lap("slow")
at = at.Add(time.Millisecond)
lap("quick again")
if len(logged) != 1 {
t.Fatalf("FAIL: logged %q, want one line", logged)
}
if want := "index: open x.db: slow took 1m15s"; logged[0] != want {
t.Errorf("FAIL: logged %q, want %q", logged[0], want)
}
}