Expire idle snapshots and abandoned jobs, cap snapshot count

A background sweep every minute drops snapshots unused for an hour,
finished jobs, and uploads never committed, deleting their workspaces.
At most 8 snapshots are held; the least recently used is evicted.
This commit is contained in:
Andy Dixon
2026-02-21 09:37:43 +00:00
parent 25e2d2f390
commit 1c93df022d
3 changed files with 100 additions and 3 deletions
+38
View File
@@ -13,6 +13,7 @@ import (
"reflect"
"strings"
"testing"
"time"
)
func TestDeliveryRangesMergeBeforeSourceIsReleased(t *testing.T) {
@@ -323,3 +324,40 @@ func TestSessionsKeepIndependentSnapshots(t *testing.T) {
t.Fatalf("snapshots = %d, want 2", len(server.snapshots))
}
}
func TestExpireDropsIdleSnapshotsAndAbandonedUploads(t *testing.T) {
server, c := newTestServer(t)
_, snapshot := c.indexLocal(map[string]string{"a.go": "package a"})
var pending struct{ JobID string }
c.json("POST", "/api/jobs", `{"kind":"local"}`, 201, &pending)
c.json("POST", "/api/jobs/"+pending.JobID+"/files?path=b.go", "package b", 200, nil)
snapshotRoot := server.snapshots[snapshot].Root
pendingRoot := server.jobs[pending.JobID].root
server.expire(time.Now())
c.json("GET", "/api/snapshots/"+snapshot, "", 200, nil)
server.expire(time.Now().Add(idleExpiry + time.Minute))
c.json("GET", "/api/snapshots/"+snapshot, "", 404, nil)
c.json("GET", "/api/jobs/"+pending.JobID, "", 404, nil)
for _, root := range []string{snapshotRoot, pendingRoot} {
if _, err := os.Stat(root); !os.IsNotExist(err) {
t.Errorf("workspace %s should be deleted", root)
}
}
if len(server.jobs) != 0 {
t.Errorf("jobs left: %d", len(server.jobs))
}
}
func TestSnapshotCapEvictsLeastRecentlyUsed(t *testing.T) {
server, first := newTestServer(t)
_, oldest := first.indexLocal(map[string]string{"a.go": "package a"})
for i := range maxSnapshots {
first.stranger().indexLocal(map[string]string{"a.go": fmt.Sprint("package p", i)})
}
if len(server.snapshots) != maxSnapshots {
t.Fatalf("snapshots = %d, want %d", len(server.snapshots), maxSnapshots)
}
first.json("GET", "/api/snapshots/"+oldest, "", 404, nil)
}