package ingest
import (
"errors"
"os/exec"
"strings"
"sync/atomic"
"testing"
"time"
"craftdesign.group/quarry/internal/events"
)
// A store rewritten under the saved cursor (shun, pack) is rebuilt from
// its census off the lane, the way a missing cursor is; nothing is
// republished through the bus, and the lane slot is gone before the
// rebuild starts (#481).
func TestCursorLossRebuildsOffTheLane(t *testing.T) {
pipe, ix, repo := testPipeline(t)
if _, err := pipe.Sync(); err != nil {
t.Fatal(err)
}
head, err := exec.Command("zit", "ls", "--long", "-R", repo, "tip").Output()
if err != nil {
t.Fatal(err)
}
run(t, repo, "shun", strings.Fields(string(head))[0], "--force")
s := watchSpans(t)
var held atomic.Bool
var heldAtRebuild atomic.Int32 // 1 = slot still held when the rebuild began
old := rebuildSpan
rebuildSpan = func(d int) {
if d > 0 && held.Load() {
heldAtRebuild.Store(1)
}
old(d)
}
t.Cleanup(func() { rebuildSpan = old })
admit := func(body func(leave func()) error) error {
held.Store(true)
defer held.Store(false)
return body(func() { held.Store(false) })
}
before := streamMsgs(t, pipe)
if _, err := pipe.SyncIn(admit); err != nil {
t.Fatalf("FAIL: round after a rewrite: %v", err)
}
if _, in := s.counts(); in != 1 {
t.Errorf("FAIL: rebuilds started = %d, want 1", in)
}
if heldAtRebuild.Load() != 0 {
t.Errorf("FAIL: the lane slot was still held when the rebuild started")
}
if got := streamMsgs(t, pipe); got != before {
t.Errorf("FAIL: the stream grew from %d to %d messages: the census went through the bus", before, got)
}
_ = ix
if !t.Failed() {
t.Log("ok")
}
}
func streamMsgs(t *testing.T, p *Pipeline) uint64 {
t.Helper()
info, err := p.ev.JS().StreamInfo(events.StreamName)
if err != nil {
t.Fatal(err)
}
return info.State.LastSeq
}
// A poll round queued for a lane slot does not hold the store: a write
// op's TrySync gets in, and when the slot comes the round finds the store
// held and skips instead of blocking.
func TestQueuedPollRoundDoesNotHoldTheStore(t *testing.T) {
pipe, _, _ := testPipeline(t)
admitted := make(chan struct{})
release := make(chan struct{})
admit := func(body func(leave func()) error) error {
<-release // the lane is full until the test says otherwise
close(admitted)
return body(func() {})
}
done := make(chan error, 1)
go func() { _, err := pipe.SyncIn(admit); done <- err }()
time.Sleep(100 * time.Millisecond) // the poll is queued now
if _, err := pipe.TrySync(); errors.Is(err, ErrRoundBusy) {
t.Errorf("FAIL: a queued poll round held the store; TrySync = %v", err)
}
if !pipe.mu.TryLock() {
t.Errorf("FAIL: p.mu held while the poll was queued for the lane")
} else {
pipe.mu.Unlock()
}
if t.Failed() { // the queued round holds the store; do not wait on it
close(release)
<-done
return
}
// Admitted while another round holds the store: skipped, not blocked.
pipe.mu.Lock()
close(release)
select {
case err := <-done:
if err != nil {
t.Errorf("FAIL: the skipped round errored: %v", err)
}
case <-time.After(5 * time.Second):
t.Errorf("FAIL: the admitted round blocked on the held store")
}
pipe.mu.Unlock()
<-admitted
if !t.Failed() {
t.Log("ok")
}
}