Skip to content
quarry Artifact [b68422f1a4]
Login

Artifact [b68422f1a4]

Artifact b68422f1a417af6c50668457d5d5904226173473de57ac9a61d55070c84c180d:


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)
	}
}