edge: asset e2e fix round 1: a policy stand-in by assetpolicy's rule, real restarts (asset cache plan 3 Task 9)
The home's LocalAssets is e2ePolicy: it decides as assetPolicy.decide does (the union of the installed and serving builds' declared prefixes, under the route's owner and AssetGen), answers only from its last settled pass and nothing before the first, and each settled pass republishes the golden entry. The generation scene installs v2 while v1 serves, and the undeclaring cases follow the declaration through the switch and the writer's next pass. The restart test recreates both Edges and their policies on the warm disks and checks both edges' cold-start refusals (the home's wakes the instance; the far edge's is the unknown-host 404) before hits with no wake after the first passes. Held first requests are waited for with a timeout, and the rulings on miss after an abandonment and on refused assertions are cited at their assertions.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+453
−136
internal/edge/asset_e2e_test.go+453 −136
CI failed — run details
diff --git a/internal/edge/asset_e2e_test.go b/internal/edge/asset_e2e_test.go| index 1422d041f..8fd7befe7 100644 |
| --- a/internal/edge/asset_e2e_test.go |
| +++ b/internal/edge/asset_e2e_test.go |
| @@ -16,10 +16,15 @@ package edge |
| // handshaken with betty by name, and every fill is the real signed hop. |
| // |
| // Two things are fixtures rather than production code, and they are named |
| -// so: the home's LocalAssets is fakeLocalAssets answering exactly what the |
| -// golden publishes (internal/host's assetPolicy cannot be imported here; it |
| -// is the writer whose output the golden pins), and a generation-managed |
| -// instance runs behind genActivator, Task 5's hand-driven GenSource. |
| +// so: each box's LocalAssets is e2ePolicy, a stand-in for internal/host's |
| +// assetPolicy (which this package cannot import) that decides by the same |
| +// rule from shipped manifests, the installed build and the serving build, |
| +// and answers nothing before its first settled pass; and a |
| +// generation-managed instance runs behind genActivator, Task 5's |
| +// hand-driven GenSource. Each settled pass also republishes the home's |
| +// edges-file entry, the golden with the decision's at (and generation or |
| +// owner where a test changes them); a decision whose paths are not the |
| +// golden's is a test bug. |
| // |
| // The golden's "at" is fixed. It is rebased to the test clock after the |
| // golden is read (plan 3 drift D14), and nothing else in it is changed |
| @@ -28,6 +33,7 @@ package edge |
| // every signature here is made with time.Now too. |
| import ( |
| + "context" |
| "crypto/rand" |
| "encoding/hex" |
| "encoding/json" |
| @@ -35,8 +41,10 @@ import ( |
| "io" |
| "net/http" |
| "path/filepath" |
| + "slices" |
| "strconv" |
| "strings" |
| + "sync" |
| "sync/atomic" |
| "testing" |
| "time" |
| @@ -64,40 +72,132 @@ func e2eGoldenView(t *testing.T, now time.Time) edgesHostView { |
| return v |
| } |
| -// e2eGoldenEntry is the golden entry as JSON, rebased to now, with edit |
| -// applied (nil for none). |
| -func e2eGoldenEntry(t *testing.T, now time.Time, edit func(*edgesHostView)) string { |
| - t.Helper() |
| - v := e2eGoldenView(t, now) |
| - if edit != nil { |
| - edit(&v) |
| +// e2ePolicy stands in for internal/host's assetPolicy (assetpolicy.go), |
| +// which internal/edge cannot import. It decides the way assetPolicy.decide |
| +// does for the one asset route: the catch is the union of the prefixes |
| +// declared by the route's installed build (Route.Version) and by its serving |
| +// build (the activator's ServingGen(host).Build; none for a sleeping |
| +// instance or a route without generations), under the route's owner and |
| +// AssetGen, and nothing when that union is empty. For and Declared answer |
| +// only from the last settled pass's snapshot and are false until the first |
| +// (assetPolicy.current), and AssetsReady says whether there has been one. |
| +// The real rule is pinned in internal/host by |
| +// TestPublishesTheUnionOfInstalledAndServingBuilds, |
| +// TestSleepingInstanceKeepsTheInstalledBuildsPaths and |
| +// TestForMakesNoRegistryReadPerCall. |
| +type e2ePolicy struct { |
| + route func() (registry.Route, bool) // nil: the box routes no asset host |
| + serving func(host string) (string, bool) |
| + |
| + mu sync.Mutex |
| + manifests map[string][]string // build → the prefixes its manifest declares |
| + |
| + snap atomic.Pointer[e2eSnapshot] |
| +} |
| + |
| +type e2eSnapshot struct { |
| + hosts map[string]HostAssets |
| + builds map[string][]string |
| +} |
| + |
| +var _ LocalAssets = (*e2ePolicy)(nil) |
| + |
| +// cold is a policy of the same box after a restart: the same inputs, no |
| +// snapshot. |
| +func (p *e2ePolicy) cold() *e2ePolicy { |
| + p.mu.Lock() |
| + defer p.mu.Unlock() |
| + q := &e2ePolicy{route: p.route, serving: p.serving, manifests: map[string][]string{}} |
| + for b, paths := range p.manifests { |
| + q.manifests[b] = paths |
| + } |
| + return q |
| +} |
| + |
| +// decide is assetPolicy.decide for the route, now. |
| +func (p *e2ePolicy) decide(now time.Time) (HostAssets, bool) { |
| + if p.route == nil { |
| + return HostAssets{}, false |
| + } |
| + r, ok := p.route() |
| + if !ok || r.AssetGen == "" || r.Account == "" || r.App == "" { |
| + return HostAssets{}, false |
| + } |
| + builds := []string{r.Version} |
| + if p.serving != nil { |
| + if b, ok := p.serving(r.Host); ok && b != "" { |
| + builds = append(builds, b) |
| + } |
| } |
| - b, err := json.Marshal(v) |
| - if err != nil { |
| - t.Fatal(err) |
| + var paths []string |
| + p.mu.Lock() |
| + for _, b := range builds { |
| + paths = append(paths, p.manifests[b]...) |
| } |
| - return string(b) |
| + p.mu.Unlock() |
| + slices.Sort(paths) |
| + paths = slices.Compact(paths) |
| + if len(paths) == 0 { |
| + return HostAssets{}, false |
| + } |
| + return HostAssets{Account: r.Account, App: r.App, Paths: paths, Gen: r.AssetGen, At: now, Primary: r.Host}, true |
| } |
| -// e2eGoldenAssets is the home's own answer for the host: what its policy |
| -// snapshot holds when the golden is what its writer published. |
| -func e2eGoldenAssets(t *testing.T) HostAssets { |
| - t.Helper() |
| - v := e2eGoldenView(t, time.Now()) |
| - return HostAssets{Account: v.Account, App: v.App, Paths: append([]string(nil), v.Assets.Paths...), |
| - Gen: v.Assets.Gen, At: time.Unix(v.Assets.At, 0), Primary: v.Assets.Primary} |
| +// pass is the edges-file writer's settled pass: it stores the snapshot For |
| +// and Declared answer from and returns the decision the writer publishes. |
| +func (p *e2ePolicy) pass(now time.Time) (HostAssets, bool) { |
| + a, ok := p.decide(now) |
| + s := &e2eSnapshot{hosts: map[string]HostAssets{}, builds: map[string][]string{}} |
| + if ok { |
| + s.hosts[a.Primary] = a |
| + } |
| + p.mu.Lock() |
| + for b, paths := range p.manifests { |
| + s.builds[b] = slices.Clone(paths) |
| + } |
| + p.mu.Unlock() |
| + p.snap.Store(s) |
| + return a, ok |
| +} |
| + |
| +func (p *e2ePolicy) AssetsReady() bool { return p.snap.Load() != nil } |
| + |
| +func (p *e2ePolicy) For(host string) (HostAssets, bool) { |
| + s := p.snap.Load() |
| + if s == nil { |
| + return HostAssets{}, false |
| + } |
| + a, ok := s.hosts[host] |
| + if !ok || time.Since(a.At) > time.Hour { |
| + return HostAssets{}, false |
| + } |
| + a.Paths = slices.Clone(a.Paths) |
| + return a, true |
| +} |
| + |
| +func (p *e2ePolicy) Declared(host, build, path string) bool { |
| + s := p.snap.Load() |
| + if build == "" || s == nil { |
| + return false |
| + } |
| + if _, ok := s.hosts[host]; !ok { |
| + return false |
| + } |
| + return slices.ContainsFunc(s.builds[build], func(prefix string) bool { return strings.HasPrefix(path, prefix) }) |
| } |
| // assetE2E is the armed two-edge scene. |
| type assetE2E struct { |
| *backhaulScene |
| - app *laBackend // the home's app, on TCP (nil on a generation scene) |
| - local *fakeLocalAssets // the home's policy snapshot |
| - route registry.Route // the home's route row for laHost |
| + app *laBackend // the home's app, on TCP (nil on a generation scene) |
| + policy *e2ePolicy // the home's policy |
| + farPolicy *e2ePolicy // the far box's policy: it routes no asset host |
| + route registry.Route // the home's route row for laHost |
| aStore, bStore *assetcache.Store |
| aDir, bDir string |
| aMeter, bMeter *UsageMeter |
| aTap *e2eTap |
| + homeUnarmed bool |
| } |
| type assetE2EOpts struct { |
| @@ -107,6 +207,11 @@ type assetE2EOpts struct { |
| route func(*registry.Route) |
| // act is the home edge's activator. |
| act Activator |
| + // manifests is what each shipped build declares; nil is the golden's |
| + // paths for laBuild, the default route's installed build. |
| + manifests map[string][]string |
| + // serving is the home's serving build (the activator's ServingGen). |
| + serving func(host string) (string, bool) |
| // farRoutes are routes of the far edge's own. |
| farRoutes []registry.Route |
| // homeUnarmed leaves the home edge's asset cache off; its edges file |
| @@ -119,8 +224,7 @@ func newAssetE2E(t *testing.T, o assetE2EOpts) *assetE2E { |
| x := &assetE2E{ |
| route: registry.Route{Host: laHost, Kind: "instance", Status: "ready", |
| Account: "abc", App: "shop", AssetGen: laGen, Version: laBuild}, |
| - local: &fakeLocalAssets{hosts: map[string]HostAssets{}, builds: map[string]bool{}}, |
| - aMeter: NewUsageMeter(), bMeter: NewUsageMeter(), aTap: &e2eTap{}, |
| + aMeter: NewUsageMeter(), bMeter: NewUsageMeter(), aTap: &e2eTap{}, homeUnarmed: o.homeUnarmed, |
| } |
| if o.handler != nil { |
| x.app = newLABackend(t, o.handler) |
| @@ -129,36 +233,32 @@ func newAssetE2E(t *testing.T, o assetE2EOpts) *assetE2E { |
| if o.route != nil { |
| o.route(&x.route) |
| } |
| - golden := e2eGoldenAssets(t) |
| - if golden.Account != x.route.Account || golden.App != x.route.App || golden.Gen != x.route.AssetGen { |
| - t.Fatalf("test bug: the route %+v is not the golden's owner and generation %+v", x.route, golden) |
| + golden := e2eGoldenView(t, time.Now()) |
| + if golden.Account != x.route.Account || golden.App != x.route.App || golden.Assets.Gen != x.route.AssetGen { |
| + t.Fatalf("test bug: the route %+v is not the golden's owner and generation %+v", x.route, golden.Assets) |
| + } |
| + manifests := o.manifests |
| + if manifests == nil { |
| + manifests = map[string][]string{laBuild: golden.Assets.Paths} |
| } |
| + x.policy = &e2ePolicy{route: func() (registry.Route, bool) { return x.route, true }, serving: o.serving, manifests: manifests} |
| + x.farPolicy = &e2ePolicy{} |
| x.backhaulScene = newBackhaulSceneWith(t, backhaulSceneExtra{ |
| - homeRoutes: []registry.Route{x.route}, |
| - farRoutes: o.farRoutes, |
| - homeAct: o.act, |
| - certs: []string{laHost}, |
| - homeEntries: map[string]string{laHost: e2eGoldenEntry(t, time.Now(), nil)}, |
| + homeRoutes: []registry.Route{x.route}, |
| + farRoutes: o.farRoutes, |
| + homeAct: o.act, |
| + certs: []string{laHost}, |
| }) |
| + // Both boxes' writers settle a first pass; the home's publishes its |
| + // entry. |
| + x.settle(t) |
| + x.farPolicy.pass(time.Now()) |
| x.converge(t) |
| - // Production wiring: every edge has the shared LRU (RunWithActivator), |
| - // so a caught request is seen not to read or fill it. |
| - for _, be := range []*backhaulEdge{x.a, x.b} { |
| - be.edge.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| - } |
| - x.local.set(laHost, golden) |
| - x.local.declare(laBuild, true) |
| x.aDir = filepath.Join(t.TempDir(), "asset-cache") |
| x.bDir = filepath.Join(t.TempDir(), "asset-cache") |
| x.aStore, x.bStore = openStoreAt(t, x.aDir), openStoreAt(t, x.bDir) |
| - if !o.homeUnarmed { |
| - armForTest(t, x.a.edge, x.aStore, x.local) |
| - } |
| - armForTest(t, x.b.edge, x.bStore, &fakeLocalAssets{hosts: map[string]HostAssets{}, builds: map[string]bool{}}) |
| - x.a.edge.EnableUsage(x.aMeter) |
| - x.b.edge.EnableUsage(x.bMeter) |
| - x.a.tap.Store(x.aTap) |
| + x.wire(t) |
| // The premise, asserted: betty has no route for the host, and knows its |
| // asset block only from annie's file. |
| @@ -174,6 +274,50 @@ func newAssetE2E(t *testing.T, o assetE2EOpts) *assetE2E { |
| return x |
| } |
| +// wire is what each box's process arms after New, as production does: the |
| +// shared LRU (RunWithActivator; a caught request is seen not to read or fill |
| +// it), the asset cache over the box's store and policy (EnableAssetCache), |
| +// and the usage meter. The home's door tap is the test's. |
| +func (x *assetE2E) wire(t *testing.T) { |
| + t.Helper() |
| + for _, be := range []*backhaulEdge{x.a, x.b} { |
| + be.edge.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes)) |
| + } |
| + if !x.homeUnarmed { |
| + armForTest(t, x.a.edge, x.aStore, x.policy) |
| + } |
| + armForTest(t, x.b.edge, x.bStore, x.farPolicy) |
| + x.a.edge.EnableUsage(x.aMeter) |
| + x.b.edge.EnableUsage(x.bMeter) |
| + x.a.tap.Store(x.aTap) |
| +} |
| + |
| +// settle is the home box's edges-file writer pass: the policy settles a |
| +// snapshot, and the home's file is rewritten with the decision. The entry |
| +// is the golden with the decision's at, generation and owner; with no |
| +// decision it is the golden's owner entry without an assets block. The far |
| +// edge sees it at its next home-map poll (converge). |
| +func (x *assetE2E) settle(t *testing.T) { |
| + t.Helper() |
| + a, ok := x.policy.pass(time.Now()) |
| + v := e2eGoldenView(t, time.Now()) |
| + v.Account, v.App = x.route.Account, x.route.App |
| + if ok { |
| + if !slices.Equal(a.Paths, v.Assets.Paths) || a.Primary != v.Assets.Primary { |
| + t.Fatalf("test bug: the policy decided %v for %s, the golden publishes %v for %s", |
| + a.Paths, a.Primary, v.Assets.Paths, v.Assets.Primary) |
| + } |
| + v.Assets.Gen, v.Assets.At = a.Gen, a.At.Unix() |
| + } else { |
| + v.Assets = nil |
| + } |
| + b, err := json.Marshal(v) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + x.publishHomeEdgesFile(t, map[string]string{laHost: string(b)}) |
| +} |
| + |
| // fetch is one client request for laHost at be. Accept-Encoding defaults to |
| // identity, so the client's transport neither asks for gzip nor undoes it. |
| func (x *assetE2E) fetch(t *testing.T, be *backhaulEdge, path string, hdr ...string) (*http.Response, string) { |
| @@ -225,6 +369,24 @@ func e2eAwait(t *testing.T, ch <-chan e2eResult) e2eResult { |
| return e2eResult{} |
| } |
| +// e2eStarted waits for the app to see a held first request, failing at once |
| +// if that request finished first (it never reached the app) and after ten |
| +// seconds if neither happens. |
| +func e2eStarted(t *testing.T, started <-chan struct{}, first <-chan e2eResult) { |
| + t.Helper() |
| + select { |
| + case <-started: |
| + case r := <-first: |
| + status := 0 |
| + if r.resp != nil { |
| + status = r.resp.StatusCode |
| + } |
| + t.Fatalf("the first request finished (status %d, error %v, body %q) before the app saw it", status, r.err, r.body) |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the app never saw the first request") |
| + } |
| +} |
| + |
| func wantE2ECache(t *testing.T, resp *http.Response, want string) { |
| t.Helper() |
| if got := resp.Header.Get(assetCacheHeader); got != want { |
| @@ -286,9 +448,9 @@ func e2eLRULen(e *Edge) int { |
| } |
| // setHome rewrites the home's route row (edit applied to the current one) |
| -// and reloads the edge, and moves the home's policy snapshot to the same |
| -// owner and generation: the home has moved on. The far edge's view does not |
| -// move until the home's edges file is republished and the maps converge. |
| +// and reloads the edge. The home's policy moves at its writer's next pass |
| +// (settle), and the far edge's view at the home-map poll after that |
| +// (converge). |
| func (x *assetE2E) setHome(t *testing.T, edit func(*registry.Route)) { |
| t.Helper() |
| edit(&x.route) |
| @@ -298,9 +460,6 @@ func (x *assetE2E) setHome(t *testing.T, edit func(*registry.Route)) { |
| if err := x.a.edge.Reload(); err != nil { |
| t.Fatal(err) |
| } |
| - a := e2eGoldenAssets(t) |
| - a.Account, a.App, a.Gen = x.route.Account, x.route.App, x.route.AssetGen |
| - x.local.set(laHost, a) |
| } |
| // ---- miss, hit, gzip, purge ------------------------------------------------- |
| @@ -427,11 +586,11 @@ func TestAssetE2EPurgeMakesEveryEdgeMiss(t *testing.T) { |
| t.Fatalf("the app saw %d requests warming, want 1", n) |
| } |
| - // The purge: a fresh generation on the home's record (its route row |
| - // and its policy snapshot), and the home's edges file rewritten with |
| - // it. One home-map poll later every edge misses once. |
| + // The purge: a fresh generation on the home's record, the home's |
| + // writer pass (its policy snapshot and its edges file, rewritten with |
| + // the generation). One home-map poll later every edge misses once. |
| x.setHome(t, func(rt *registry.Route) { rt.AssetGen = e2eGen2 }) |
| - x.publishHomeEdgesFile(t, map[string]string{laHost: e2eGoldenEntry(t, time.Now(), func(v *edgesHostView) { v.Assets.Gen = e2eGen2 })}) |
| + x.settle(t) |
| x.converge(t) |
| if a, ok := x.b.homes.AssetsFor(laHost, time.Now()); !ok || a.Gen != e2eGen2 { |
| t.Fatalf("the far edge's map holds %+v %v after the poll, want generation %s", a, ok, e2eGen2) |
| @@ -471,6 +630,10 @@ func TestAssetE2EOldFarEdgeForgedMarkerIsBilledAsAHop(t *testing.T) { |
| if req.Get(assetFillHeader) != forged || req.Get(hopHeader) != "1" { |
| t.Fatalf("the home edge received %v, want the hop and its forged marker", req) |
| } |
| + // Ruling (Task 9 review, fix round 1): a refused asserted fill gets no |
| + // acknowledgement, stores nothing under the asserted key and is billed |
| + // once as an ordinary hop; the home's own cache then treats it as any |
| + // ordinary request for its current key. |
| for _, h := range []string{assetAckHeader, assetExpiresHeader} { |
| if v := out.Values(h); len(v) > 0 { |
| t.Fatalf("the home answered a forged marker with %s %q", h, v) |
| @@ -568,6 +731,9 @@ func TestAssetE2ERefusedFillsAreStoredNowhere(t *testing.T) { |
| cache string |
| }{ |
| {"set-cookie", assetOK(laBody, "Set-Cookie", "s=1"), 200, "bypass"}, |
| + // Ruling (Task 9 review, fix round 1): a streamed response says miss |
| + // once an admitted fill was attempted; a later abandonment cannot |
| + // change a header already sent. |
| {"set-cookie-trailer", trailer, 200, "miss"}, |
| {"vary-cookie", assetOK(laBody, "Vary", "Cookie"), 200, "bypass"}, |
| {"vary-origin", assetOK(laBody, "Vary", "Origin"), 200, "bypass"}, |
| @@ -664,7 +830,7 @@ func TestAssetE2ECoalescedWaitersAndRefusals(t *testing.T) { |
| }}) |
| t.Cleanup(release) |
| lead := x.fetchAsync(x.b, "/assets/app.js") |
| - <-started |
| + e2eStarted(t, started, lead) |
| var rest []<-chan e2eResult |
| for i := 0; i < waiters; i++ { |
| rest = append(rest, x.fetchAsync(x.b, "/assets/app.js")) |
| @@ -746,7 +912,7 @@ func TestAssetE2EStalledBodyIsAbandoned(t *testing.T) { |
| t.Cleanup(free) |
| res := x.fetchAsync(x.b, "/assets/app.js") |
| - <-started |
| + e2eStarted(t, started, res) |
| laWaitFor(t, "both edges' reservations", func() bool { |
| return x.aStore.Stats().Reserved > 0 && x.bStore.Stats().Reserved > 0 |
| }) |
| @@ -760,6 +926,9 @@ func TestAssetE2EStalledBodyIsAbandoned(t *testing.T) { |
| } |
| free() |
| r := e2eAwait(t, res) |
| + // Ruling (Task 9 review, fix round 1): a streamed response says miss |
| + // once an admitted fill was attempted; a later abandonment cannot change |
| + // a header already sent. |
| wantE2ECache(t, r.resp, "miss") |
| if r.body != "partial-rest" { |
| t.Fatalf("the client got %q, want the whole stream as an ordinary response would", r.body) |
| @@ -781,35 +950,88 @@ func genAssetApp(t *testing.T, sock, body string) (*genApp, *atomic.Int64) { |
| return a, &n |
| } |
| +// e2eWakeActivator is genActivator with a wake: a request it is handed while |
| +// the instance has no current generation first runs wake, as the real |
| +// activator starts the instance's process. wakes counts them. |
| +type e2eWakeActivator struct { |
| + *genActivator |
| + wakes atomic.Int64 |
| + wake atomic.Pointer[func()] |
| +} |
| + |
| +func (a *e2eWakeActivator) ServeHTTP(w http.ResponseWriter, r *http.Request, rt registry.Route, proxy http.Handler) bool { |
| + if _, ok := a.ServingGen(rt.Host); !ok { |
| + if f := a.wake.Load(); f != nil { |
| + a.wakes.Add(1) |
| + (*f)() |
| + } |
| + } |
| + return a.genActivator.ServeHTTP(w, r, rt, proxy) |
| +} |
| + |
| +// genScene is the scene with laHost a generation-managed instance. |
| +type genScene struct { |
| + *assetE2E |
| + act *e2eWakeActivator |
| + dir string |
| + v1, v2 *genApp |
| + n1, n2 *atomic.Int64 |
| +} |
| + |
| // newGenAssetE2E is the scene with laHost an activator-managed instance on a |
| -// unix socket (Kind instance, Hibernate set: what selects generation |
| -// routing at the socket arm, drift D15), its generation v1 serving body |
| -// "v1" and v2 installed beside it, answering "v2". |
| -func newGenAssetE2E(t *testing.T) (x *assetE2E, act *genActivator, v1, v2 *genApp, n1, n2 *atomic.Int64) { |
| +// unix socket (Kind instance, Hibernate set: what selects generation routing |
| +// at the socket arm, drift D15) in the middle of an update: the installed |
| +// build (Route.Version) is v2, generation 1 (build v1) serves, and v2's |
| +// process is up beside it. manifests is what each build declares; the home's |
| +// policy catches the union of the installed and serving builds' prefixes. |
| +func newGenAssetE2E(t *testing.T, manifests map[string][]string) *genScene { |
| t.Helper() |
| - dir := genSockDir(t) |
| + g := &genScene{dir: genSockDir(t)} |
| // The canonical socket answers loudly: a request reaching it means the |
| // edge fell back to plain socket proxying. |
| - startGenApp(t, filepath.Join(dir, "canon.sock"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + startGenApp(t, filepath.Join(g.dir, "canon.sock"), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| http.Error(w, "canonical", http.StatusInternalServerError) |
| })) |
| - v1, n1 = genAssetApp(t, filepath.Join(dir, "v1.sock"), "v1") |
| - v2, n2 = genAssetApp(t, filepath.Join(dir, "v2.sock"), "v2") |
| - act = &genActivator{fakeGens: newFakeGens(true)} |
| - act.switchTo(laHost, GenInfo{Socket: v1.sock, N: 1, Build: "v1"}) |
| - x = newAssetE2E(t, assetE2EOpts{act: act, route: func(rt *registry.Route) { |
| - rt.Hibernate = true |
| - rt.Socket = filepath.Join(dir, "canon.sock") |
| - rt.Version = "v0" |
| - }}) |
| + g.v1, g.n1 = genAssetApp(t, filepath.Join(g.dir, "v1.sock"), "v1") |
| + g.v2, g.n2 = genAssetApp(t, filepath.Join(g.dir, "v2.sock"), "v2") |
| + g.act = &e2eWakeActivator{genActivator: &genActivator{fakeGens: newFakeGens(true)}} |
| + g.act.switchTo(laHost, GenInfo{Socket: g.v1.sock, N: 1, Build: "v1"}) |
| + g.assetE2E = newAssetE2E(t, assetE2EOpts{ |
| + act: g.act, |
| + manifests: manifests, |
| + serving: func(host string) (string, bool) { |
| + gi, ok := g.act.ServingGen(host) |
| + return gi.Build, ok |
| + }, |
| + route: func(rt *registry.Route) { |
| + rt.Hibernate = true |
| + rt.Socket = filepath.Join(g.dir, "canon.sock") |
| + rt.Version = "v2" |
| + }, |
| + }) |
| // A fixture that silently fell back to plain proxying would test |
| // nothing here (astra P2-4). |
| - up, ok := x.a.edge.lookupUpstreamPath(laHost, "/assets/app.js") |
| + up, ok := g.a.edge.lookupUpstreamPath(laHost, "/assets/app.js") |
| if !ok || !up.genManaged || !up.hibernating { |
| t.Fatalf("the home's upstream for %s is ok=%v genManaged=%v hibernating=%v, want a generation-managed instance", |
| laHost, ok, up.genManaged, up.hibernating) |
| } |
| - return x, act, v1, v2, n1, n2 |
| + return g |
| +} |
| + |
| +// wantPolicy checks the home policy's settled snapshot: whether it catches |
| +// laHost (with the golden's paths), and which builds declare /assets/app.js. |
| +func (g *genScene) wantPolicy(t *testing.T, catches bool, declares map[string]bool) { |
| + t.Helper() |
| + a, ok := g.policy.For(laHost) |
| + if ok != catches || (ok && !slices.Equal(a.Paths, e2eGoldenView(t, time.Now()).Assets.Paths)) { |
| + t.Fatalf("policy For = %+v %v, want catching %v with the golden's paths", a, ok, catches) |
| + } |
| + for build, want := range declares { |
| + if got := g.policy.Declared(laHost, build, "/assets/app.js"); got != want { |
| + t.Fatalf("policy Declared(%s) = %v, want %v", build, got, want) |
| + } |
| + } |
| } |
| func (x *assetE2E) wantStoredNowhere(t *testing.T, path, what string) { |
| @@ -821,121 +1043,202 @@ func (x *assetE2E) wantStoredNowhere(t *testing.T, path, what string) { |
| } |
| } |
| +func (x *assetE2E) wantStoredOnBoth(t *testing.T, path string) { |
| + t.Helper() |
| + for name, st := range map[string]*assetcache.Store{"home": x.aStore, "far": x.bStore} { |
| + if _, ok := st.Get(x.entryKey(path)); !ok { |
| + t.Fatalf("the %s store holds no entry for %s", name, path) |
| + } |
| + } |
| +} |
| + |
| func TestAssetE2EUndeclaringBuildIsNotStored(t *testing.T) { |
| + golden := e2eGoldenView(t, time.Now()).Assets.Paths |
| t.Run("only the installed build declares", func(t *testing.T) { |
| - x, _, _, _, n1, n2 := newGenAssetE2E(t) |
| - x.local.declare("v1", false) |
| - x.local.declare("v2", true) |
| - for _, be := range []*backhaulEdge{x.b, x.a} { |
| - resp, body := x.fetch(t, be, "/assets/app.js") |
| + g := newGenAssetE2E(t, map[string][]string{"v2": golden}) |
| + // The installed build's prefixes are caught; the serving build |
| + // declares none of them. |
| + g.wantPolicy(t, true, map[string]bool{"v1": false, "v2": true}) |
| + for _, be := range []*backhaulEdge{g.b, g.a} { |
| + resp, body := g.fetch(t, be, "/assets/app.js") |
| wantE2ECache(t, resp, "bypass") |
| if body != "v1" { |
| t.Fatalf("body = %q, want the serving build's fallback", body) |
| } |
| } |
| - x.wantStoredNowhere(t, "/assets/app.js", "the serving build declared nothing") |
| - if n1.Load() != 2 || n2.Load() != 0 { |
| - t.Fatalf("v1 answered %d and v2 %d requests, want v1 both", n1.Load(), n2.Load()) |
| + g.wantStoredNowhere(t, "/assets/app.js", "the serving build declared nothing") |
| + if g.n1.Load() != 2 || g.n2.Load() != 0 { |
| + t.Fatalf("v1 answered %d and v2 %d requests, want v1 both", g.n1.Load(), g.n2.Load()) |
| + } |
| + |
| + // The update completes: v2 serves, and its answer is stored, before |
| + // the writer's next pass and after it. |
| + g.act.switchTo(laHost, GenInfo{Socket: g.v2.sock, N: 2, Build: "v2"}) |
| + resp, body := g.fetch(t, g.a, "/assets/app.js") |
| + wantE2ECache(t, resp, "miss") |
| + if body != "v2" { |
| + t.Fatalf("body = %q, want the installed build's", body) |
| + } |
| + g.settle(t) |
| + g.converge(t) |
| + g.wantPolicy(t, true, map[string]bool{"v1": false, "v2": true}) |
| + resp, _ = g.fetch(t, g.b, "/assets/app.js") |
| + wantE2ECache(t, resp, "miss") |
| + g.wantStoredOnBoth(t, "/assets/app.js") |
| + if n := g.n2.Load(); n != 1 { |
| + t.Fatalf("v2 answered %d requests, want 1", n) |
| } |
| }) |
| t.Run("only the retiring build declares", func(t *testing.T) { |
| - x, act, _, v2, _, _ := newGenAssetE2E(t) |
| - x.local.declare("v1", true) |
| - x.local.declare("v2", false) |
| + g := newGenAssetE2E(t, map[string][]string{"v1": golden}) |
| + // The serving build's prefixes are caught; the installed build |
| + // declares none of them. |
| + g.wantPolicy(t, true, map[string]bool{"v1": true, "v2": false}) |
| // The control: while the declaring build serves, the fixture stores. |
| - resp, _ := x.fetch(t, x.b, "/assets/ctl.js") |
| + resp, _ := g.fetch(t, g.b, "/assets/ctl.js") |
| wantE2ECache(t, resp, "miss") |
| - for name, st := range map[string]*assetcache.Store{"home": x.aStore, "far": x.bStore} { |
| - if _, ok := st.Get(x.entryKey("/assets/ctl.js")); !ok { |
| - t.Fatalf("test bug: the declaring build's response was not stored on the %s edge", name) |
| - } |
| - } |
| + g.wantStoredOnBoth(t, "/assets/ctl.js") |
| - act.switchTo(laHost, GenInfo{Socket: v2.sock, N: 2, Build: "v2"}) |
| - for _, be := range []*backhaulEdge{x.b, x.a} { |
| - resp, body := x.fetch(t, be, "/assets/app.js") |
| + // The switch. Until the writer's next pass the snapshot still |
| + // catches the retiring build's prefixes, and v2's answers are |
| + // refused: v2 declares none. |
| + g.act.switchTo(laHost, GenInfo{Socket: g.v2.sock, N: 2, Build: "v2"}) |
| + for _, be := range []*backhaulEdge{g.b, g.a} { |
| + resp, body := g.fetch(t, be, "/assets/app.js") |
| wantE2ECache(t, resp, "bypass") |
| if body != "v2" { |
| t.Fatalf("body = %q, want the installed build's answer", body) |
| } |
| } |
| - x.wantStoredNowhere(t, "/assets/app.js", "the installed build declared nothing") |
| + g.wantStoredNowhere(t, "/assets/app.js", "the installed build declared nothing") |
| + |
| + // The writer's pass after the switch: neither the installed nor the |
| + // serving build declares anything, so the host catches nothing on |
| + // any edge once the far edge has polled. |
| + g.settle(t) |
| + g.converge(t) |
| + g.wantPolicy(t, false, nil) |
| + if a, ok := g.b.homes.AssetsFor(laHost, time.Now()); ok { |
| + t.Fatalf("the far edge still holds an asset block %+v", a) |
| + } |
| + for _, be := range []*backhaulEdge{g.a, g.b} { |
| + resp, body := g.fetch(t, be, "/assets/app.js") |
| + wantE2ECache(t, resp, "") |
| + if body != "v2" { |
| + t.Fatalf("body = %q", body) |
| + } |
| + } |
| + g.wantStoredNowhere(t, "/assets/app.js", "a host that catches nothing") |
| }) |
| } |
| -// restartAssets is a box's edge process restarting with a warm disk: its |
| -// store reopened from the same directory, a fresh home map that has not read |
| -// anything yet, the route table reloaded, and the cache armed again. |
| -func (x *assetE2E) restartAssets(t *testing.T, be *backhaulEdge, dir string, local LocalAssets) *assetcache.Store { |
| +// restart is both boxes' edge processes restarting on their warm disks: new |
| +// Edges on the same registries, buckets and listeners (backhaulEdge.restart), |
| +// each box's policy back to no snapshot (its writer has not run yet), the |
| +// stores reopened from the same directories, and everything armed again. |
| +// The cert mirrors are refreshed (Part 2's subject, not this one's); the |
| +// home maps are left unread and neither policy has settled. |
| +func (x *assetE2E) restart(t *testing.T) { |
| t.Helper() |
| - be.edge.stopAssetSweep() |
| - st := openStoreAt(t, dir) |
| - be.homes = be.edge.newHomeMap() |
| - be.edge.homes.Store(be.homes) |
| - if err := be.edge.Reload(); err != nil { |
| - t.Fatal(err) |
| + for _, be := range []*backhaulEdge{x.a, x.b} { |
| + be.edge.stopAssetSweep() |
| + be.restart(t) |
| + if err := be.mirror.Refresh(context.Background()); err != nil { |
| + t.Fatalf("cert mirror refresh: %v", err) |
| + } |
| } |
| - armForTest(t, be.edge, st, local) |
| - return st |
| + x.policy, x.farPolicy = x.policy.cold(), x.farPolicy.cold() |
| + x.aStore, x.bStore = openStoreAt(t, x.aDir), openStoreAt(t, x.bDir) |
| + x.wire(t) |
| } |
| func TestAssetE2EWarmSleepRestart(t *testing.T) { |
| - x, act, v1, _, n1, _ := newGenAssetE2E(t) |
| + golden := e2eGoldenView(t, time.Now()).Assets.Paths |
| + g := newGenAssetE2E(t, map[string][]string{"v1": golden, "v2": golden}) |
| for _, want := range []string{"miss", "hit"} { |
| - for _, be := range []*backhaulEdge{x.a, x.b} { |
| - resp, body := x.fetch(t, be, "/assets/app.js") |
| + for _, be := range []*backhaulEdge{g.a, g.b} { |
| + resp, body := g.fetch(t, be, "/assets/app.js") |
| wantE2ECache(t, resp, want) |
| if body != "v1" { |
| t.Fatalf("body = %q", body) |
| } |
| } |
| } |
| - if n := n1.Load(); n != 1 { |
| + if n := g.n1.Load(); n != 1 { |
| t.Fatalf("the generation answered %d requests warming, want 1", n) |
| } |
| // Sleep: the activator takes the generation away and its process exits. |
| - act.sleep(laHost) |
| - v1.stop() |
| - served, asks := act.served.Load(), act.askCount() |
| + // A sleeping instance keeps its installed build's paths |
| + // (internal/host TestSleepingInstanceKeepsTheInstalledBuildsPaths). |
| + g.act.sleep(laHost) |
| + g.v1.stop() |
| - // Restart both edge processes on their warm disks. |
| - x.aStore = x.restartAssets(t, x.a, x.aDir, x.local) |
| - x.bStore = x.restartAssets(t, x.b, x.bDir, &fakeLocalAssets{hosts: map[string]HostAssets{}, builds: map[string]bool{}}) |
| - for name, st := range map[string]*assetcache.Store{"home": x.aStore, "far": x.bStore} { |
| + g.restart(t) |
| + if g.a.edge == nil || g.policy.AssetsReady() || g.farPolicy.AssetsReady() { |
| + t.Fatal("test bug: the restarted boxes' policies hold a snapshot") |
| + } |
| + for name, st := range map[string]*assetcache.Store{"home": g.aStore, "far": g.bStore} { |
| if s := st.Stats(); s.Entries != 1 { |
| t.Fatalf("the %s store reopened with %+v, want the warm entry", name, s) |
| } |
| } |
| - // D13: before the far edge's first home-map read, no disk answer and |
| - // today's unresolved-host behaviour. |
| - calls := x.a.calls.Load() |
| - resp, _ := x.fetch(t, x.b, "/assets/app.js") |
| + |
| + // Cold start, far (D13): before its first home-map read, no disk answer |
| + // and today's unresolved-host behaviour. |
| + calls := g.a.calls.Load() |
| + resp, _ := g.fetch(t, g.b, "/assets/app.js") |
| if resp.StatusCode != http.StatusNotFound || resp.Header.Get(assetCacheHeader) != "" { |
| t.Fatalf("before the first read the far edge answered %d (Carlos-Cache %q), want the unknown-host 404", |
| resp.StatusCode, resp.Header.Get(assetCacheHeader)) |
| } |
| - if got := x.a.calls.Load(); got != calls { |
| + if got := g.a.calls.Load(); got != calls { |
| t.Fatalf("the far edge reached the home %d time(s) before its first read", got-calls) |
| } |
| - x.converge(t) |
| - for _, be := range []*backhaulEdge{x.a, x.b} { |
| - calls := x.a.calls.Load() |
| - resp, body := x.fetch(t, be, "/assets/app.js") |
| + // Cold start, home: its policy has no snapshot, so the warm entry is not |
| + // served. The request is an ordinary one for a sleeping instance and |
| + // wakes it; the sweep keeps the warm store meanwhile. |
| + woken, nWoken := genAssetApp(t, filepath.Join(g.dir, "v1b.sock"), "v1") |
| + wake := func() { g.act.switchTo(laHost, GenInfo{Socket: woken.sock, N: 3, Build: "v1"}) } |
| + g.act.wake.Store(&wake) |
| + resp, body := g.fetch(t, g.a, "/assets/app.js") |
| + wantE2ECache(t, resp, "") |
| + if resp.StatusCode != http.StatusOK || body != "v1" || g.act.wakes.Load() != 1 || nWoken.Load() != 1 { |
| + t.Fatalf("before the first snapshot the home answered %d %q with %d wake(s), want the woken app's answer", |
| + resp.StatusCode, body, g.act.wakes.Load()) |
| + } |
| + g.act.wake.Store(nil) |
| + if n := g.a.edge.sweepAssets(time.Now()); n != 0 || g.aStore.Stats().Entries != 1 { |
| + t.Fatalf("a sweep before the first snapshot dropped %d entries", n) |
| + } |
| + // Back to sleep. |
| + g.act.sleep(laHost) |
| + woken.stop() |
| + |
| + // The writers' first passes, then one home-map poll: both edges hit, and |
| + // nothing wakes. |
| + g.settle(t) |
| + g.farPolicy.pass(time.Now()) |
| + g.converge(t) |
| + served, asks, wakes := g.act.served.Load(), g.act.askCount(), g.act.wakes.Load() |
| + for _, be := range []*backhaulEdge{g.a, g.b} { |
| + calls := g.a.calls.Load() |
| + resp, body := g.fetch(t, be, "/assets/app.js") |
| wantE2ECache(t, resp, "hit") |
| if body != "v1" { |
| t.Fatalf("body = %q", body) |
| } |
| // The client's own request is the home edge's only one: the far |
| // hit never crosses the hop. |
| - if want := map[bool]int64{true: 1, false: 0}[be == x.a]; x.a.calls.Load()-calls != want { |
| - t.Fatalf("the home edge saw %d request(s) for one hit, want %d", x.a.calls.Load()-calls, want) |
| + if want := map[bool]int64{true: 1, false: 0}[be == g.a]; g.a.calls.Load()-calls != want { |
| + t.Fatalf("the home edge saw %d request(s) for one hit, want %d", g.a.calls.Load()-calls, want) |
| } |
| } |
| - if act.served.Load() != served || act.askCount() != asks || n1.Load() != 1 { |
| - t.Fatalf("the activator was entered %d times and asked for a socket %d times after the sleep; want neither (no wake)", |
| - act.served.Load()-served, act.askCount()-asks) |
| + if g.act.served.Load() != served || g.act.askCount() != asks || g.act.wakes.Load() != wakes || |
| + g.n1.Load() != 1 || nWoken.Load() != 1 { |
| + t.Fatalf("after the first snapshot the activator was entered %d times, asked for a socket %d times and woke %d times; want none", |
| + g.act.served.Load()-served, g.act.askCount()-asks, g.act.wakes.Load()-wakes) |
| } |
| } |
| @@ -998,6 +1301,9 @@ func TestAssetE2EOversizedResponseIsStoredNowhere(t *testing.T) { |
| {"/assets/sized.js", "bypass"}, |
| // An unknown length is reserved at the cap and discarded past it, |
| // after the header (miss) went out. |
| + // Ruling (Task 9 review, fix round 1): a streamed response says miss |
| + // once an admitted fill was attempted; a later abandonment cannot |
| + // change a header already sent. |
| {"/assets/unsized.js", "miss"}, |
| } { |
| t.Run(tc.path, func(t *testing.T) { |
| @@ -1031,7 +1337,10 @@ func TestAssetE2EGenerationOrOwnerMismatchGetsNoAck(t *testing.T) { |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| x := newAssetE2E(t, assetE2EOpts{handler: assetOK(laBody)}) |
| + // The home's record moved and its writer has run (its policy |
| + // and its edges file both moved); the far edge has not polled. |
| x.setHome(t, tc.edit) |
| + x.settle(t) |
| if a, _ := x.b.homes.AssetsFor(laHost, time.Now()); a.Gen != laGen || a.App != "shop" { |
| t.Fatalf("test bug: the far edge's view moved with the home: %+v", a) |
| } |
| @@ -1044,6 +1353,10 @@ func TestAssetE2EGenerationOrOwnerMismatchGetsNoAck(t *testing.T) { |
| if req.Get(assetFillHeader) == "" { |
| t.Fatal("test bug: the far edge sent no fill") |
| } |
| + // Ruling (Task 9 review, fix round 1): a refused asserted fill |
| + // gets no acknowledgement, stores nothing under the asserted key |
| + // and is billed once as an ordinary hop; the home's own cache |
| + // then treats it as any ordinary request for its current key. |
| if out.Get(assetAckHeader) != "" || out.Get(assetExpiresHeader) != "" { |
| t.Fatalf("the home acknowledged a fill it does not hold: %v", out) |
| } |
| @@ -1055,6 +1368,10 @@ func TestAssetE2EGenerationOrOwnerMismatchGetsNoAck(t *testing.T) { |
| if s := x.bStore.Stats(); s.Entries != 0 || s.Reserved != 0 { |
| t.Fatalf("the far store holds %+v", s) |
| } |
| + // Ruling (Task 9 review, fix round 1): a refused asserted fill |
| + // gets no acknowledgement, stores nothing under the asserted key |
| + // and is billed once as an ordinary hop; the home's own cache |
| + // then treats it as any ordinary request for its current key. |
| own := assetcache.Key{Account: x.route.Account, App: x.route.App, Gen: x.route.AssetGen, Host: laHost, Path: "/assets/app.js"} |
| if _, ok := x.aStore.Get(own); !ok { |
| t.Fatalf("the home's own catch did not answer the ordinary hop under its current key %+v", own) |