Skip to content
quarry Artifact [067c27e620]
Login

Artifact [067c27e620]

Artifact 067c27e6200a64b2d718db07ef89f960b4c3e9ee631af7cc10b3293dc194241c:


package obs

import (
	"errors"
	"sync"
	"sync/atomic"
	"testing"
	"time"
)

// A traffic page is computed once a minute per range and filters: fresh
// answers come from the memo, stale ones come back at once while one
// refresh runs behind them, a past window is never memoised, and a
// failed refresh keeps serving the last answer.
func TestTrafficIsMemoisedAndRefreshedBehind(t *testing.T) {
	var m trafficMemo
	var calls atomic.Int32
	compute := func(q TrafficQuery) (*Traffic, error) {
		calls.Add(1)
		return &Traffic{Range: q.Range}, nil
	}
	q := TrafficQuery{Range: TrafficRange{Name: "7d"}, Filters: map[string]string{"repo": "quarry"}}
	now := time.Now()
	if _, err := m.get(q, now, compute); err != nil || calls.Load() != 1 {
		t.Fatalf("FAIL: first read %v, %d computes", err, calls.Load())
	}
	m.get(q, now.Add(20*time.Second), compute)
	if calls.Load() != 1 {
		t.Errorf("FAIL: a fresh read computed again (%d)", calls.Load())
	}
	if got, _ := m.get(q, now.Add(2*time.Minute), compute); got == nil {
		t.Error("FAIL: a stale read did not answer at once")
	}
	for i := 0; i < 50 && calls.Load() < 2; i++ {
		time.Sleep(10 * time.Millisecond)
	}
	if calls.Load() != 2 {
		t.Errorf("FAIL: a stale read started %d refreshes, want one", calls.Load()-1)
	}
	other := TrafficQuery{Range: TrafficRange{Name: "7d"}}
	m.get(other, now, compute)
	if calls.Load() != 3 {
		t.Error("FAIL: a different filter set shared an answer")
	}
	past := TrafficQuery{Range: TrafficRange{Name: "7d"}, Now: now.Add(-time.Hour)}
	m.get(past, now, compute)
	m.get(past, now, compute)
	if calls.Load() != 5 {
		t.Error("FAIL: a question about the past was memoised")
	}
	failing := func(TrafficQuery) (*Traffic, error) { return nil, errors.New("down") }
	var f trafficMemo
	f.get(q, now, compute)
	if got, err := f.get(q, now.Add(2*time.Minute), failing); got == nil || err != nil {
		t.Error("FAIL: a failing refresh did not keep serving the last answer")
	}
	t.Log("ok")
}

type warmSink struct {
	Sink
	calls atomic.Int32
}

func (s *warmSink) Traffic(q TrafficQuery) (*Traffic, error) {
	s.calls.Add(1)
	return &Traffic{Range: q.Range}, nil
}

// WarmTraffic fills the memo for every named range at once, so the
// first view of each is answered from it.
func TestWarmTrafficFillsEveryRange(t *testing.T) {
	var r Recorder
	ws := &warmSink{}
	r.sink = ws
	stop := make(chan struct{})
	go r.WarmTraffic(stop)
	for i := 0; i < 100 && int(ws.calls.Load()) < len(TrafficRanges); i++ {
		time.Sleep(10 * time.Millisecond)
	}
	close(stop)
	before := ws.calls.Load()
	for _, rg := range TrafficRanges {
		if _, err := r.Traffic(TrafficQuery{Range: rg}); err != nil {
			t.Fatal(err)
		}
	}
	if ws.calls.Load() != before || int(before) < len(TrafficRanges) {
		t.Errorf("FAIL: warming computed %d ranges and the reads computed %d more", before, ws.calls.Load()-before)
	}
	t.Log("ok")
}

type rangeSink struct {
	Sink
	mu    sync.Mutex
	calls map[string]int
	fail  map[string]bool
}

func (s *rangeSink) Traffic(q TrafficQuery) (*Traffic, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.calls[q.Range.Name]++
	if s.fail[q.Range.Name] {
		return nil, errors.New("down")
	}
	return &Traffic{Range: q.Range}, nil
}

// The ranges past a day are the expensive reads, so a tick every few
// minutes recomputes only the short ones; the long ones wait out
// trafficLongEvery, and one whose read failed is tried on the next tick.
func TestWarmTrafficRecomputesLongRangesLessOften(t *testing.T) {
	var r Recorder
	rs := &rangeSink{calls: map[string]int{}, fail: map[string]bool{}}
	r.sink = rs
	last := map[string]time.Time{}
	stop := make(chan struct{})
	t0 := time.Now()
	want := func(when string, shortCalls, longCalls int) {
		t.Helper()
		for _, rg := range TrafficRanges {
			w := shortCalls
			if rg.Span > trafficShortSpan {
				w = longCalls
			}
			if rs.calls[rg.Name] != w {
				t.Errorf("FAIL: %s: range %s computed %d times, want %d", when, rg.Name, rs.calls[rg.Name], w)
			}
		}
	}
	r.warmRanges(t0, last, stop)
	want("first", 1, 1)
	for i := 1; i*int(trafficWarmEvery/time.Minute) < int(trafficLongEvery/time.Minute); i++ {
		r.warmRanges(t0.Add(time.Duration(i)*trafficWarmEvery), last, stop)
	}
	n := int(trafficLongEvery / trafficWarmEvery) // ticks before the long ones are due
	want("before the long interval", 1+n, 1)
	r.warmRanges(t0.Add(trafficLongEvery), last, stop)
	want("at the long interval", 2+n, 2)

	rs.fail["30d"] = true
	r.warmRanges(t0.Add(2*trafficLongEvery), last, stop)
	r.warmRanges(t0.Add(2*trafficLongEvery+trafficWarmEvery), last, stop)
	if got := rs.calls["30d"]; got != 4 {
		t.Errorf("FAIL: a failed long range was computed %d times, want 4 (retried on the next tick)", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}