Skip to content
quarry Artifact [d9c9f13cda]
Login

Artifact [d9c9f13cda]

Artifact d9c9f13cda4d92ce8200c518be8a7b8b056c46dbf11fa4aedbe2e60933cb336b:


package main

import (
	"context"
	"strings"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/zitcli"
)

// stubWarm records the order of the reads a warm pass makes and how
// many root listings run at once.
type stubWarm struct {
	mu      sync.Mutex
	calls   []string
	cur     atomic.Int32
	max     atomic.Int32
	release chan struct{} // when non-nil, LsUnder waits on it
	entered chan struct{}
	tip     string
	tipMu   sync.Mutex
}

func (s *stubWarm) setTip(v string) {
	s.tipMu.Lock()
	s.tip = v
	s.tipMu.Unlock()
}

func (s *stubWarm) note(c string) {
	s.mu.Lock()
	s.calls = append(s.calls, c)
	s.mu.Unlock()
}

func (s *stubWarm) Ls(context.Context, string) ([]zitcli.LsEntry, error) {
	s.note("ls")
	return nil, nil
}

func (s *stubWarm) LsUnder(_ context.Context, version, _ string) ([]zitcli.LsEntry, error) {
	n := s.cur.Add(1)
	defer s.cur.Add(-1)
	for {
		m := s.max.Load()
		if n <= m || s.max.CompareAndSwap(m, n) {
			break
		}
	}
	s.note("root:" + version)
	if s.entered != nil {
		s.entered <- struct{}{}
	}
	if s.release != nil {
		<-s.release
	}
	return nil, nil
}

func (s *stubWarm) Cat(context.Context, string, string) ([]byte, error) { return nil, nil }

func (s *stubWarm) TipOf(string) (index.Branch, bool, error) {
	s.tipMu.Lock()
	defer s.tipMu.Unlock()
	return index.Branch{Tip: s.tip}, s.tip != "", nil
}
func (s *stubWarm) DefaultBranch() string { return "trunk" }
func (s *stubWarm) WarmActivityDays(string) error {
	s.note("activity")
	return nil
}
func (s *stubWarm) EventKinds() (map[string]int, error) { return nil, nil }

func (s *stubWarm) order() []string {
	s.mu.Lock()
	defer s.mu.Unlock()
	return append([]string(nil), s.calls...)
}

func indexOf(xs []string, v string) int {
	for i, x := range xs {
		if x == v || (v == "root" && strings.HasPrefix(x, "root:")) {
			return i
		}
	}
	return -1
}

func TestWarmListsTheRootBeforeTheActivityScan(t *testing.T) {
	s := &stubWarm{tip: "abc"}
	warmReads(context.Background(), s, s)
	o := s.order()
	r, a := indexOf(o, "root"), indexOf(o, "activity")
	if r != 0 {
		t.Errorf("FAIL: the root listing was not the first read: %v", o)
	}
	if a < 0 || a < indexOf(o, "ls") {
		t.Errorf("FAIL: the activity scan did not come last: %v", o)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestWarmEarlyListsTheRootWithoutASyncRound(t *testing.T) {
	s := &stubWarm{tip: "abc", entered: make(chan struct{}, 1)}
	var w warmer
	var once sync.Once
	warmEarly(context.Background(), &w, &once, s, s)
	select {
	case <-s.entered:
	case <-time.After(5 * time.Second):
		t.Fatal("FAIL: registering a repository with a tip did not start its root listing")
	}
	ran := false
	once.Do(func() { ran = true })
	if ran {
		t.Errorf("FAIL: the first sync round would warm the same tip again")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestWarmEarlyWaitsForATip(t *testing.T) {
	s := &stubWarm{}
	var w warmer
	var once sync.Once
	decided := warmEarly(context.Background(), &w, &once, s, s)
	select {
	case <-decided:
	case <-time.After(5 * time.Second):
		t.Fatal("FAIL: the early warm never decided")
	}
	if len(s.order()) != 0 || w.busy.Load() {
		t.Errorf("FAIL: an index without a tip was warmed: %v", s.order())
	}
	ran := false
	once.Do(func() { ran = true })
	if !ran {
		t.Errorf("FAIL: the first round's warm was spent on a repository with no tip")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A round that ingests while the early warm is still running must not
// lose its warm: the new tip's root listing is memoised once the running
// pass ends.
func TestAMovedTipIsWarmedAfterTheRunningWarm(t *testing.T) {
	s := &stubWarm{tip: "abc", release: make(chan struct{}), entered: make(chan struct{}, 8)}
	var w warmer
	var once sync.Once
	warmEarly(context.Background(), &w, &once, s, s)
	<-s.entered
	s.setTip("def")
	w.start(context.Background(), s, s) // the round that ingested; refused
	close(s.release)
	deadline := time.Now().Add(5 * time.Second)
	for indexOf(s.order(), "root:def") < 0 && time.Now().Before(deadline) {
		time.Sleep(time.Millisecond)
	}
	if indexOf(s.order(), "root:def") < 0 {
		t.Errorf("FAIL: the moved tip's root listing was never warmed: %v", s.order())
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A stopped store's warm does not start another listing.
func TestAStoppedStoreIsNotWarmed(t *testing.T) {
	s := &stubWarm{tip: "abc"}
	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	warmReads(ctx, s, s)
	if len(s.order()) != 0 {
		t.Errorf("FAIL: a stopped store was warmed: %v", s.order())
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

func TestRootListingsAreBoundedFleetWide(t *testing.T) {
	s := &stubWarm{tip: "abc", release: make(chan struct{}), entered: make(chan struct{}, 64)}
	const fleet = 40
	var wg sync.WaitGroup
	for i := 0; i < fleet; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			warmReads(context.Background(), s, s)
		}()
	}
	select {
	case <-s.entered:
	case <-time.After(5 * time.Second):
		t.Fatal("FAIL: the allowed listing did not start")
	}
	time.Sleep(50 * time.Millisecond)
	if cap(frontSem) != 1 {
		t.Errorf("FAIL: the bound is %d, want 1", cap(frontSem))
	}
	if got := s.max.Load(); int(got) != cap(frontSem) {
		t.Errorf("FAIL: %d listings ran at once; the bound is %d", got, cap(frontSem))
	}
	close(s.release)
	wg.Wait()
	if !t.Failed() {
		t.Log("ok")
	}
}