Skip to content
quarry Artifact [4e28fa1ac3]
Login

Artifact [4e28fa1ac3]

Artifact 4e28fa1ac332386f1f012b740fc3f9554fb85cde9e74056f05540bb4018900a1:


package index

import (
	"path/filepath"
	"testing"
	"time"
)

func openCountIndex(t *testing.T) *Index {
	t.Helper()
	ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { ix.Close() })
	return ix
}

func addCheckin(t *testing.T, ix *Index, hash string) {
	t.Helper()
	if _, err := ix.w.Exec(`INSERT INTO checkin (hash, date, user, comment) VALUES (?, '2026-01-01', 'u', 'm')`, hash); err != nil {
		t.Fatal(err)
	}
}

// The fleet and operations pages read every store's check-in count. The
// memo answers without the database until a fold moves the generation.
func TestCheckinCountMemoHoldsUntilAFold(t *testing.T) {
	ix := openCountIndex(t)
	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.
	addCheckin(t, ix, "c1")
	if n, _ := ix.CheckinCountMemo(); n != first {
		t.Errorf("FAIL: the memo asked the database again with no fold: %d", n)
	}
}

// A fold must not make the next reader wait for a recount: it gets the
// last count at once, and the count converges to the real one without a
// further fold.
func TestCheckinCountMemoServesTheLastCountWhileRecounting(t *testing.T) {
	ix := openCountIndex(t)
	if _, err := ix.CheckinCountMemo(); err != nil {
		t.Fatal(err)
	}
	hold := make(chan struct{})
	ix.checkinRun = func() { <-hold }
	addCheckin(t, ix, "c1")
	addCheckin(t, ix, "c2")
	ix.invalidateCounts()

	done := make(chan int, 1)
	go func() { n, _ := ix.CheckinCountMemo(); done <- n }()
	select {
	case n := <-done:
		if n != 0 {
			t.Errorf("FAIL: with the recount held, the read answered %d, want the last count 0", n)
		}
	case <-time.After(5 * time.Second):
		t.Fatal("FAIL: the read waited for the recount")
	}
	close(hold)
	deadline := time.Now().Add(10 * time.Second)
	for {
		if n, _ := ix.CheckinCountMemo(); n == 2 {
			return
		}
		if time.Now().After(deadline) {
			n, _ := ix.CheckinCountMemo()
			t.Fatalf("FAIL: the count never reached 2, still %d", n)
		}
		time.Sleep(5 * time.Millisecond)
	}
}

// The first read of a store with no count yet is exact, and the warm
// call fills the count before anyone asks.
func TestWarmCheckinCountFillsTheCount(t *testing.T) {
	ix := openCountIndex(t)
	addCheckin(t, ix, "c1")
	ix.WarmCheckinCount()
	deadline := time.Now().Add(10 * time.Second)
	for {
		ix.checkinMu.Lock()
		known, n := ix.checkinKnown, ix.checkinN
		ix.checkinMu.Unlock()
		if known {
			if n != 1 {
				t.Fatalf("FAIL: the warm count is %d, want 1", n)
			}
			return
		}
		if time.Now().After(deadline) {
			t.Fatal("FAIL: the warm call never kept a count")
		}
		time.Sleep(5 * time.Millisecond)
	}
}