Three step-log sequences in the state aged, one per retention (7, 30, 90 days); at each step's end up to 20 entries per sequence past their retention delete the output and the pipeline (7 days), a kept result not read for 30 days (30 days) and a passed pipeline (90 days), never a pipeline that is its branch's newest; each delete is a journal line. The step log is written to and read from the log's stream (design 47 G §470); a log entry names the kept results it wrote or carried and a kept result its last read. Builds: design 47 G §330, §410, §470 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
145 lines
5.4 KiB
Go
145 lines
5.4 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
)
|
|
|
|
type agedState map[string]uint64
|
|
|
|
func (a agedState) Get(name string) (uint64, error) { return a[name], nil }
|
|
func (a agedState) Put(name string, s uint64) error { a[name] = s; return nil }
|
|
|
|
// agedPipelines is memStore that deletes.
|
|
type agedPipelines struct {
|
|
memStore
|
|
deleted []string
|
|
}
|
|
|
|
func (a *agedPipelines) Delete(key string) error {
|
|
delete(a.memStore, key)
|
|
a.deleted = append(a.deleted, key)
|
|
return nil
|
|
}
|
|
|
|
type keptStore struct {
|
|
m map[string]seat.KeptResult
|
|
deleted []string
|
|
}
|
|
|
|
func (k *keptStore) Get(key string) (seat.KeptResult, bool, error) {
|
|
v, ok := k.m[key]
|
|
return v, ok, nil
|
|
}
|
|
func (k *keptStore) Delete(key string) error {
|
|
delete(k.m, key)
|
|
k.deleted = append(k.deleted, key)
|
|
return nil
|
|
}
|
|
|
|
type agedFixture struct {
|
|
ager *Ager
|
|
log []LoggedEntry
|
|
aged agedState
|
|
pipes *agedPipelines
|
|
kept *keptStore
|
|
outputs []string
|
|
journal []string
|
|
readFrom []uint64
|
|
}
|
|
|
|
var agedNow = time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC)
|
|
|
|
func newAgedFixture() *agedFixture {
|
|
f := &agedFixture{aged: agedState{}, pipes: &agedPipelines{memStore: memStore{}}, kept: &keptStore{m: map[string]seat.KeptResult{}}}
|
|
f.ager = &Ager{Aged: f.aged, Pipelines: f.pipes, Kept: f.kept, Now: func() time.Time { return agedNow },
|
|
Outputs: func(n string) error { f.outputs = append(f.outputs, n); return nil },
|
|
Logf: func(format string, a ...any) { f.journal = append(f.journal, sprintf(format, a...)) },
|
|
Log: func(from uint64, limit int) ([]LoggedEntry, error) {
|
|
f.readFrom = append(f.readFrom, from)
|
|
var out []LoggedEntry
|
|
for _, e := range f.log {
|
|
if e.Sequence >= from && len(out) < limit {
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
return out, nil
|
|
}}
|
|
return f
|
|
}
|
|
|
|
func sprintf(format string, a ...any) string { return fmt.Sprintf(format, a...) }
|
|
|
|
func (f *agedFixture) pipeline(branch string, source int64, outcome string, run string) string {
|
|
key := branch + "." + strconv.FormatInt(source, 10) + ".app"
|
|
f.pipes.memStore[key] = struct {
|
|
p seat.Pipeline
|
|
rev uint64
|
|
}{seat.Pipeline{Pipeline: key, Branch: branch, Source: source, Module: "app", Outcome: outcome, Run: run}, 1}
|
|
return key
|
|
}
|
|
|
|
func days(n int) time.Time { return agedNow.Add(-time.Duration(n) * 24 * time.Hour) }
|
|
|
|
// The runner ages its own kept data in the step log's order, by the retentions of the design: at 7 days an output and the
|
|
// pipeline that did not pass or is not its branch's newest, at 30 days a kept result not read for 30 days, at 90 days a passed
|
|
// pipeline that is not its branch's newest; each retention keeps its own sequence in the state aged (design 47 G §330).
|
|
func TestTheRunnerAgesItsOwnKeptDataByTheStepLogsOrder(t *testing.T) {
|
|
f := newAgedFixture()
|
|
failed := f.pipeline("main", 1, seat.PipelineOutcomeFailed, "")
|
|
passed := f.pipeline("main", 2, seat.PipelineOutcomePassed, "")
|
|
old := f.pipeline("main", 3, seat.PipelineOutcomePassed, "")
|
|
newest := f.pipeline("main", 9, seat.PipelineOutcomeFailed, "")
|
|
stale, fresh := days(40), days(2)
|
|
f.kept.m["unit.sha256:a"] = seat.KeptResult{Digest: "sha256:a", Level: "unit", LastRead: &stale}
|
|
f.kept.m["unit.sha256:b"] = seat.KeptResult{Digest: "sha256:b", Level: "unit", LastRead: &fresh}
|
|
f.log = []LoggedEntry{
|
|
{1, seat.LogEntry{Pipeline: old, Output: "out-1", At: days(100)}},
|
|
{2, seat.LogEntry{Pipeline: failed, Output: "out-2", Kept: []string{"unit.sha256:a", "unit.sha256:b"}, At: days(35)}},
|
|
{3, seat.LogEntry{Pipeline: passed, Output: "out-3", At: days(10)}},
|
|
{4, seat.LogEntry{Pipeline: newest, Output: "out-4", At: days(9)}},
|
|
{5, seat.LogEntry{Pipeline: newest, Output: "out-5", At: days(1)}},
|
|
}
|
|
f.ager.Age()
|
|
if got := strings.Join(f.outputs, ","); got != "out-1,out-2,out-3,out-4" {
|
|
t.Errorf("outputs deleted past 7 days: %s", got)
|
|
}
|
|
if got := strings.Join(f.pipes.deleted, ","); got != failed+","+old {
|
|
t.Errorf("pipelines deleted: %s, want %s (the failed one at 7 days, the old passed one at 90; the passed at 7 stays; the newest stays)", got, failed+","+old)
|
|
}
|
|
if got := strings.Join(f.kept.deleted, ","); got != "unit.sha256:a" {
|
|
t.Errorf("kept results deleted: %s", got)
|
|
}
|
|
if f.aged["7d"] != 4 || f.aged["30d"] != 2 || f.aged["90d"] != 1 {
|
|
t.Errorf("sequences aged: %v, want 4, 2 and 1 (the entries past each retention)", f.aged)
|
|
}
|
|
for _, want := range []string{"deleted the output out-1 (7d)", "deleted the pipeline " + failed + " (7d)", "deleted the kept result unit.sha256:a (30d", "deleted the pipeline " + old + " (90d)"} {
|
|
if !strings.Contains(strings.Join(f.journal, "\n"), want) {
|
|
t.Errorf("the journal names no %q:\n%s", want, strings.Join(f.journal, "\n"))
|
|
}
|
|
}
|
|
// A second step's end reads onward from the moved sequences and deletes nothing again.
|
|
f.readFrom, f.outputs = nil, nil
|
|
f.ager.Age()
|
|
if len(f.outputs) != 0 || f.readFrom[0] != 5 {
|
|
t.Errorf("the second ageing read from %v and deleted %v; want it to read from 5", f.readFrom, f.outputs)
|
|
}
|
|
}
|
|
|
|
// One step's end ages at most 20 entries per retention.
|
|
func TestOneStepEndAgesAtMostTwentyEntries(t *testing.T) {
|
|
f := newAgedFixture()
|
|
for i := uint64(1); i <= 25; i++ {
|
|
f.log = append(f.log, LoggedEntry{i, seat.LogEntry{Pipeline: "x", Output: "o" + strconv.FormatUint(i, 10), At: days(50)}})
|
|
}
|
|
f.ager.Age()
|
|
if len(f.outputs) != AgedPerStep || f.aged["7d"] != 20 {
|
|
t.Fatalf("%d outputs deleted, sequence %d; want 20 and 20", len(f.outputs), f.aged["7d"])
|
|
}
|
|
}
|