package web
import (
"regexp"
"strings"
"testing"
)
type laneRow = struct {
Name string `json:"name"`
Bound int `json:"bound"`
Running int `json:"running"`
Admitted int `json:"admitted"`
Refused int `json:"refused"`
WaitedMs int64 `json:"waitedMs"`
Waiting int64 `json:"waiting"`
Queue int `json:"queue"`
}
func laneLine(t *testing.T, lanes ...laneRow) string {
t.Helper()
var d pageData
d.Ops.BudgetMs = 500
d.Ops.Lanes = lanes
var buf strings.Builder
if err := opsPage.ExecuteTemplate(&buf, "content", d); err != nil {
t.Fatal(err)
}
m := regexp.MustCompile(`(?s)<p class="ops-phase-l">(.*?)</p>`).FindStringSubmatch(buf.String())
if m == nil {
t.Fatal("FAIL: the ops page drew no lane line")
}
return strings.Join(strings.Fields(regexp.MustCompile(`<[^>]*>`).ReplaceAllString(m[1], "")), " ")
}
// A saturated lane has to read as a diagnosis. All slots busy AND the
// queue full is capacity; all slots busy with an empty queue is a slot
// held too long. Running and refused alone cannot tell those apart,
// which is what left ten thousand "a store could not be ingested" rows
// unactionable.
func TestASaturatedLaneReadsAsADiagnosis(t *testing.T) {
got := laneLine(t, laneRow{Name: "ingest", Bound: 3, Running: 3,
Waiting: 22, Queue: 22, Admitted: 4201, Refused: 9871})
for _, want := range []string{"ingest", "3/3 running", "22 of 22 queued", "4,201 admitted", "9,871 told busy"} {
if !strings.Contains(got, want) {
t.Errorf("FAIL: the line is missing %q: %s", want, got)
}
}
}
// Exact figures, not rounded ones: this is the page an operator reads
// to pick a limit, and "9.9K told busy" is not a number you can set a
// queue from.
func TestTheLaneCountsAreExact(t *testing.T) {
got := laneLine(t, laneRow{Name: "ingest", Bound: 3, Admitted: 4201, Refused: 9871})
if strings.Contains(got, "K told busy") || strings.Contains(got, "K admitted") {
t.Errorf("FAIL: the counts are rounded: %s", got)
}
}
// A quiet lane says zero rather than leaving the word dangling. count
// renders a zero as nothing — right for a nav badge, and here it read
// "archive builds 0/2 running, admitted".
func TestAQuietLaneStillSaysZero(t *testing.T) {
got := laneLine(t, laneRow{Name: "archive builds", Bound: 2})
if !strings.Contains(got, "0 admitted") {
t.Errorf("FAIL: a lane that admitted nothing reads %q", got)
}
// A lane that never refused anybody does not mention refusals.
if strings.Contains(got, "told busy") {
t.Errorf("FAIL: a quiet lane reports refusals: %s", got)
}
}
// The queue depth is stated wherever a lane queues at all: "0 of 8
// queued" is how a reader learns the queue exists. A lane that does not
// queue says nothing, because there is no depth to report.
func TestDepthIsShownWhereverThereIsAQueue(t *testing.T) {
if got := laneLine(t, laneRow{Name: "bulk", Bound: 2, Queue: 8}); !strings.Contains(got, "0 of 8 queued") {
t.Errorf("FAIL: an idle queue is not stated: %s", got)
}
if got := laneLine(t, laneRow{Name: "store subprocesses", Bound: 8, Admitted: 483}); strings.Contains(got, "queued") {
t.Errorf("FAIL: a lane with no queue claims one: %s", got)
}
}