Skip to content
quarry Artifact [60382083a2]
Login

Artifact [60382083a2]

Artifact 60382083a20bd814fb280fa3e71d1dd26a58de105e220bef8a6a83b5aa357f5a:


package main

import (
	"context"
	"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
}

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, string, 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")
	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) {
	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 {
			return i
		}
	}
	return -1
}

func TestWarmListsTheRootBeforeTheActivityScan(t *testing.T) {
	s := &stubWarm{tip: "abc"}
	warmReads(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 warming atomic.Bool
	var once sync.Once
	warmEarly(&warming, &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")
	}
	// The sync round's warm-at-boot latch is spent, so that round does
	// not warm the same tip again.
	deadline := time.Now().Add(5 * time.Second)
	for warming.Load() && time.Now().Before(deadline) {
		time.Sleep(time.Millisecond)
	}
	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 warming atomic.Bool
	var once sync.Once
	warmEarly(&warming, &once, s, s)
	time.Sleep(50 * time.Millisecond)
	if len(s.order()) != 0 {
		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")
	}
}

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(s, s)
		}()
	}
	for i := 0; i < cap(frontSem); i++ {
		select {
		case <-s.entered:
		case <-time.After(5 * time.Second):
			t.Fatal("FAIL: the allowed listings did not start")
		}
	}
	time.Sleep(50 * time.Millisecond)
	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")
	}
}