package main
import (
"context"
"fmt"
"testing"
"time"
"craftdesign.group/quarry/internal/zitcli"
)
// A gate refusal is not a build, however long the gate made it wait.
func TestFinfoLineSaysNotBuiltWhenBusy(t *testing.T) {
busy := fmt.Errorf("zitcli: log: %w", zitcli.ErrBusy)
if got := finfoLine("r", busy, 5*time.Second); got == finfoLine("r", nil, 5*time.Second+time.Millisecond) {
t.Errorf("FAIL: a busy refusal logged the same line as a build")
}
if got := finfoLine("r", nil, 6*time.Second); got == "" {
t.Errorf("FAIL: a slow successful build logged nothing")
}
if got := finfoLine("r", nil, time.Second); got != "" {
t.Errorf("FAIL: a fast warm logged %q", got)
}
if got := finfoLine("r", busy, time.Second); got == "" {
t.Errorf("FAIL: a quick busy refusal went unsaid")
}
if !t.Failed() {
t.Log("ok")
}
}
// Boot warms are spread: each repository's gate picks its own start
// inside the window, the wait honours it, and a zero spread starts at once.
func TestBootGateSpreadsStarts(t *testing.T) {
base := time.Unix(1000, 0)
now := base
var napped []time.Duration
nap := func(_ context.Context, d time.Duration) bool {
napped = append(napped, d)
now = now.Add(d)
return true
}
clock := func() time.Time { return now }
offsets := []time.Duration{10 * time.Second, 90 * time.Second, 170 * time.Second}
var gates []*bootGate
for _, off := range offsets {
gates = append(gates, newBootGate(3*time.Minute, func(sp time.Duration) time.Duration {
if sp != 3*time.Minute {
t.Errorf("FAIL: jitter asked for spread %v", sp)
}
return off
}, clock, nap))
}
starts := map[time.Time]bool{}
for _, g := range gates {
starts[g.at] = true
}
if len(starts) != len(offsets) {
t.Errorf("FAIL: %d distinct starts for %d repositories", len(starts), len(offsets))
}
for i, g := range gates {
now = base
napped = nil
if !g.wait(context.Background()) {
t.Fatalf("FAIL: gate %d did not release", i)
}
if len(napped) != 1 || napped[0] != offsets[i] {
t.Errorf("FAIL: gate %d waited %v, want %v", i, napped, offsets[i])
}
napped = nil
g.wait(context.Background()) // later calls cost nothing
if len(napped) != 0 {
t.Errorf("FAIL: gate %d waited again after its start", i)
}
}
zero := newBootGate(0, func(time.Duration) time.Duration { t.Error("FAIL: jitter called for a zero spread"); return 0 }, clock, nap)
napped = nil
zero.wait(context.Background())
if len(napped) != 0 {
t.Errorf("FAIL: a zero spread waited")
}
if !t.Failed() {
t.Log("ok")
}
}
// The warmer holds its pass back until its gate opens, and a cancelled
// store lets it go.
func TestWarmerWaitsForItsBootGate(t *testing.T) {
opened := make(chan struct{})
w := &warmer{gate: &bootGate{at: time.Unix(10, 0), now: func() time.Time { return time.Unix(0, 0) },
nap: func(ctx context.Context, d time.Duration) bool { <-opened; return true }}}
s := &stubWarm{tip: "t1"}
w.start(context.Background(), s, s)
time.Sleep(50 * time.Millisecond)
if s.callCount() != 0 {
t.Errorf("FAIL: the warm ran before its gate opened")
}
close(opened)
deadline := time.Now().Add(5 * time.Second)
for s.callCount() == 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if s.callCount() == 0 {
t.Errorf("FAIL: the warm never ran after its gate opened")
}
if !t.Failed() {
t.Log("ok")
}
}
func (s *stubWarm) callCount() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.calls)
}