package activator import ( "context" "errors" "fmt" "strings" "testing" "github.com/carlosframework/platform/internal/cgroup" ) func (a *Service) parentRaisedForTest(host string) bool { a.mu.Lock() defer a.mu.Unlock() e := a.entries[host] return e != nil && e.ops.raised } // Final review F11(a): a raise that failed partway (memory.max written, a // later write refused) may have changed the parent, so the overlap's // cleanup restores it. The parent is marked potentially changed before the // raise is asked. func TestSwapOverlapRestoresAPartlyRaisedParent(t *testing.T) { f := newSwapFixture(t) f.b.gmu.Lock() f.b.raiseErr = errors.New("cgroup: parent memory.swap.max: invalid argument") f.b.gmu.Unlock() res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest) if res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, "raise parent limit") { t.Fatalf("Swap = %+v, want abandoned at the raise", res) } gens, raises, restores := f.b.calls() if gens != 0 || raises != 1 || restores != 1 { t.Fatalf("StartStaged %d, raises %d, restores %d: a partly raised parent must be restored", gens, raises, restores) } if f.a.parentRaisedForTest(f.host) { t.Fatal("the restored parent is still marked raised") } } // Final review F11(b): a restore that fails after the last member left // keeps its obligation. The parent stays marked raised, no new overlap is // admitted onto it (it would raise again under a pending restore), and // the sweep retries the restore, outside a.mu, until it lands. func TestSwapOverlapRetriesAFailedParentRestore(t *testing.T) { t.Parallel() f := newSwapFixture(t) f.b.gmu.Lock() f.b.restoreFails = 1 f.b.gmu.Unlock() if res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest); res.Outcome != SwapDone { t.Fatalf("Swap = %+v", res) } if _, _, restores := f.b.calls(); restores != 1 { t.Fatalf("restores = %d, want the one that failed", restores) } if !f.a.parentRaisedForTest(f.host) { t.Fatal("a failed restore dropped its obligation: the parent is no longer marked raised") } if _, members := f.a.genStateForTest(f.host); len(members) != 0 { t.Fatalf("members = %v: the retired generation is proven, it leaves the set", members) } f.rev.Add(1) if res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest); res.Outcome != SwapAbandoned || !strings.Contains(res.Reason, errSwapBusy.Error()) { t.Fatalf("Swap over a pending restore = %+v, want abandoned: busy", res) } f.a.sweepOnce() waitFor(t, "the restore retry", func() bool { return !f.a.parentRaisedForTest(f.host) }) if _, raises, restores := f.b.calls(); raises != 1 || restores != 2 { t.Fatalf("raises %d, restores %d: want one raise and the retried restore", raises, restores) } } // A raise refused before any write (no generation containment, no // delegated manager) changed nothing: no restore is owed, and the next // overlap is not held busy by an obligation that could never clear. func TestSwapOverlapOwesNoRestoreForARaiseRefusedBeforeAnyWrite(t *testing.T) { t.Parallel() for _, refusal := range []error{errNoGenContainment, cgroup.ErrNoDelegation} { f := newSwapFixture(t) f.b.gmu.Lock() f.b.raiseErr = fmt.Errorf("parent: %w", refusal) f.b.gmu.Unlock() if res := f.a.Swap(context.Background(), f.attempt(ModeOverlap), f.newest); res.Outcome != SwapAbandoned { t.Fatalf("%v: Swap = %+v, want abandoned", refusal, res) } if _, raises, restores := f.b.calls(); raises != 1 || restores != 0 { t.Fatalf("%v: raises %d, restores %d: want no restore", refusal, raises, restores) } if f.a.parentRaisedForTest(f.host) { t.Fatalf("%v: an untouched parent is marked raised", refusal) } } }