| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/activator" |
| 9 | "github.com/carlosframework/platform/internal/hostclaim" |
| 10 | "github.com/carlosframework/platform/internal/registry" |
| 11 | ) |
| 12 | |
| 13 | type plan9D2RefuseSelect struct{ ClaimWakeInputs } |
| 14 | |
| 15 | func (p plan9D2RefuseSelect) Select(context.Context, registry.Route, hostclaim.Observed, activator.StartPolicy) (ClaimLaunchInputs, error) { |
| 16 | return ClaimLaunchInputs{}, errors.New("selected input disappeared") |
| 17 | } |
| 18 | |
| 19 | func plan9D2Unused(t *testing.T, c *recoveryController, o hostclaim.Observed) *claimWakeAttempt { |
| 20 | t.Helper() |
| 21 | live, cancel := context.WithCancel(context.Background()) |
| 22 | done := make(chan struct{}) |
| 23 | go func() { <-live.Done(); close(done) }() |
| 24 | a := &claimWakeAttempt{observed: o, held: recoveryHeld{}, ctx: live, cancel: cancel, done: done} |
| 25 | c.wake.mu.Lock() |
| 26 | c.wake.pending[o.Host] = a |
| 27 | c.wake.mu.Unlock() |
| 28 | t.Cleanup(cancel) |
| 29 | return a |
| 30 | } |
| 31 | |
| 32 | func TestPlan9D2AdmissionFailureReleasesUnusedOperationLease(t *testing.T) { |
| 33 | for _, stage := range []string{"select", "after-cas"} { |
| 34 | t.Run(stage, func(t *testing.T) { |
| 35 | p := activator.StartPolicy{Probe: true} |
| 36 | f, c, _, _, _ := plan9D2Landing(t, "probing", p, true) |
| 37 | if err := c.wake.release(t.Context(), recoveryHost, c.wake.get(recoveryHost)); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | before := f.read() |
| 41 | a := plan9D2Unused(t, c, before) |
| 42 | stops := 0 |
| 43 | c.wake.Stop = func(context.Context, hostclaim.Observed) error { stops++; return errors.New("unexpected root stop") } |
| 44 | if stage == "select" { |
| 45 | c.wake.Inputs = plan9D2RefuseSelect{c.wake.Inputs} |
| 46 | } else { |
| 47 | c.wake.AfterAdmission = func(context.Context, hostclaim.Observed, hostclaim.Observed) error { |
| 48 | return errors.New("source evidence temporarily unavailable") |
| 49 | } |
| 50 | } |
| 51 | if _, err := c.runtime.Admit(t.Context(), registry.Route{Host: recoveryHost}, before, p); err == nil { |
| 52 | t.Fatal("admission unexpectedly succeeded") |
| 53 | } |
| 54 | if c.wake.get(recoveryHost) != nil || !a.released || a.ctx.Err() == nil || stops != 0 { |
| 55 | t.Fatalf("unused operation retained or foreign stop attempted: pending=%v released=%v stopped=%d", c.wake.get(recoveryHost) != nil, a.released, stops) |
| 56 | } |
| 57 | after := f.read() |
| 58 | if stage == "select" && after.ETag != before.ETag { |
| 59 | t.Fatal("failed selection changed the claim") |
| 60 | } |
| 61 | if stage == "after-cas" && after.Claim.Holder.Session <= before.Claim.Holder.Session { |
| 62 | t.Fatal("cleanup refunded or hid the successful admission") |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestPlan9D2CloseNeverStopsAnUnusedForeignPredecessor(t *testing.T) { |
| 69 | p := activator.StartPolicy{Probe: true} |
| 70 | f, c, _, _, _ := plan9D2Landing(t, "probing", p, true) |
| 71 | if err := c.wake.release(t.Context(), recoveryHost, c.wake.get(recoveryHost)); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | o := f.read() |
| 75 | o.Claim.Holder.WriterBox = "source" |
| 76 | a := plan9D2Unused(t, c, o) |
| 77 | c.wake.Stop = func(context.Context, hostclaim.Observed) error { |
| 78 | t.Fatal("foreign predecessor sent to local root") |
| 79 | return nil |
| 80 | } |
| 81 | if err := c.wake.Close(t.Context()); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if !a.released || c.wake.get(recoveryHost) != nil { |
| 85 | t.Fatal("unused foreign predecessor kept the lease") |
| 86 | } |
| 87 | } |
| 88 | |