package index
import (
"path/filepath"
"testing"
)
// The fleet and operations pages read every store's check-in count. The
// memo answers without the database until a fold moves the generation,
// and then answers the new number.
func TestCheckinCountMemoHoldsUntilAFold(t *testing.T) {
ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
defer ix.Close()
first, err := ix.CheckinCountMemo()
if err != nil {
t.Fatal(err)
}
exact, _ := ix.CheckinCount()
if first != exact {
t.Errorf("FAIL: memo %d, exact %d on first read", first, exact)
}
// A row written behind the memo's back is not seen until a fold is
// announced: that is what makes the memo cheap.
if _, err := ix.w.Exec(`INSERT INTO checkin SELECT * FROM checkin WHERE 0`); err != nil {
t.Fatal(err)
}
ix.checkinMu.Lock()
ix.checkinN = 41 // a value only the memo could return
ix.checkinMu.Unlock()
if n, _ := ix.CheckinCountMemo(); n != 41 {
t.Errorf("FAIL: the memo asked the database again with no fold: %d", n)
}
ix.invalidateCounts()
if n, _ := ix.CheckinCountMemo(); n != exact {
t.Errorf("FAIL: after a fold the memo answered %d, want the database's %d", n, exact)
}
if !t.Failed() {
t.Log("ok")
}
}