From e048f6e3fafcc99fa921df278723fcc444f2f9f9 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Fri, 20 Nov 2026 16:30:41 -0500 Subject: [PATCH 1/3] Report the first answer and a time stop from the upgrade pass Resolution.Upgrade carries the first solve's answer and whether time cut the pass short, so a caller can fall back without solving again (rstudio/package-manager#22820). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 + CLAUDE.md | 3 +- resolver/resolver.go | 21 ++- resolver/upgrade.go | 53 +++++++- resolver/upgrade_report_test.go | 223 ++++++++++++++++++++++++++++++++ 5 files changed, 297 insertions(+), 7 deletions(-) create mode 100644 resolver/upgrade_report_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b4ca385..84c194a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ served it. ## [Unreleased] +### Added + +- `Resolution.Upgrade` reports on the `UpgradeRoots` pass: `First` is the first solve's answer, the one the pass-off run gives, and `StoppedByTime` says whether `UpgradeTimeFactor`, `UpgradeMaxTime` or the end of `ctx` cut the pass short. A caller that wants a repeatable answer can use `First` when time stopped the pass, without solving again. It is nil with the pass off, and the kept answer does not change. + ## [0.27.0] - 2026-11-18 ### Fixed diff --git a/CLAUDE.md b/CLAUDE.md index 9dfb5ef..2039f82 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -7,7 +7,8 @@ Phase 3. > **Status: implemented end to end.** `pypirsf/`, `index/`, `pep440set/`, > `candidate/`, `provider/` and `resolver/` all carry code. `resolver.Resolve` > is the entry point; keep its exported surface to `Resolve`, `Options`, -> `Resolution`, `ResolutionError`, `MissingExtra` and `Requester`. +> `Resolution`, `ResolutionError`, `MissingExtra`, `YankedPin`, `UpgradeReport` +> and `Requester`. > > ⚠️ `resolver` is **no longer the only package Package Manager imports**. PPM > implements its own `MetadataIndex` (`src/pyindex`) and imports `index/` for the diff --git a/resolver/resolver.go b/resolver/resolver.go index f1beb3b..8be9b55 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -188,7 +188,8 @@ type Options struct { // re-solve still spends budget. Every re-solve uses these same Options. // // It never fails a resolution: if the budget or ctx ends, or no re-solve - // helps, the best complete answer so far is returned. + // helps, the best complete answer so far is returned. Resolution.Upgrade + // holds the first answer and whether time cut the pass short. UpgradeRoots int // UpgradeTimeFactor, when above zero, bounds the UpgradeRoots pass in time: @@ -326,6 +327,24 @@ type Resolution struct { // requirer in the solution that pins it exactly, root or transitive, so a // caller can warn. Sorted by (package, requester). YankedPins []YankedPin + + // Upgrade reports on the UpgradeRoots pass. It is nil when the pass is off. + Upgrade *UpgradeReport +} + +// UpgradeReport says how the UpgradeRoots pass went. +type UpgradeReport struct { + // First is the first solve's answer, the one Resolve returns with the pass + // off. Its Upgrade is nil, and it shares no maps or slices with the + // Resolution that holds it. + First *Resolution + + // StoppedByTime is true when time cut the pass short: UpgradeTimeFactor or + // UpgradeMaxTime was reached, or ctx ended before the pass did. The kept + // answer then depends on timing; use First for a repeatable one. A pass + // ended by the step bounds, the UpgradeRoots budget, or running out of + // roots to try is not stopped by time. + StoppedByTime bool } // YankedPin is a selected yanked version and one requirer that pins it. diff --git a/resolver/upgrade.go b/resolver/upgrade.go index 5804224..4fc41a7 100644 --- a/resolver/upgrade.go +++ b/resolver/upgrade.go @@ -4,6 +4,7 @@ package resolver import ( "context" + "maps" "math" "slices" "strconv" @@ -246,8 +247,8 @@ func upgradeStepLimit(opts Options, baseSteps int) (limit int, ok bool) { } // upgradeRoots is the pass behind Options.UpgradeRoots. base is a valid answer -// to reqs, and baseDur and baseSteps are what it took; the result is base or a -// valid answer that scores strictly better. +// to reqs, and baseDur and baseSteps are what it took; the result is base's +// answer or a valid one that scores strictly better, with an UpgradeReport. func upgradeRoots( ctx context.Context, reqs []requirement.Requirement, @@ -257,6 +258,26 @@ func upgradeRoots( baseDur time.Duration, baseSteps int, ) *Resolution { + best, byTime := upgradePass(ctx, reqs, idx, opts, base, baseDur, baseSteps) + if best == base { + // First must not alias the answer the caller gets. + best = cloneResolution(base) + } + // ctx errors are sticky, so any way a ctx end reached the pass shows here. + best.Upgrade = &UpgradeReport{First: base, StoppedByTime: byTime || ctx.Err() != nil} + return best +} + +// upgradePass runs the pass. byTime is whether a time bound ended it. +func upgradePass( + ctx context.Context, + reqs []requirement.Requirement, + idx index.MetadataIndex, + opts Options, + base *Resolution, + baseDur time.Duration, + baseSteps int, +) (_ *Resolution, byTime bool) { passStart := now() limit, timed := upgradeTimeLimit(opts, baseDur) stepLimit, stepped := upgradeStepLimit(opts, baseSteps) @@ -264,8 +285,13 @@ func upgradeRoots( // cut is set once a re-solve is stopped by the time bound; the pass then // ends. A re-solve stopped by the step bound leaves spent at stepLimit. cut := false + clockUp := false // the time bound was reached between re-solves timeUp := func() bool { - return cut || (stepped && spent >= stepLimit) || (timed && now().Sub(passStart) >= limit) + if cut || (stepped && spent >= stepLimit) { + return true + } + clockUp = timed && now().Sub(passStart) >= limit + return clockUp } u := &usability{ p: provider.New(ctx, idx, providerOptions(reqs, opts)), @@ -273,7 +299,7 @@ func upgradeRoots( } roots := upgradeRootSet(ctx, reqs, idx, opts, base, u) if len(roots) == 0 { - return base + return base, false } best := base // best is scored only once a re-solve has an answer to compare it with: @@ -396,7 +422,24 @@ func upgradeRoots( } } } - return best + return best, cut || clockUp +} + +// cloneResolution copies r's maps and slices, so the copy shares none with r. +func cloneResolution(r *Resolution) *Resolution { + c := *r + c.Pinned = maps.Clone(r.Pinned) + c.Order = slices.Clone(r.Order) + if r.Extras != nil { + c.Extras = make(map[index.PackageName][]string, len(r.Extras)) + for k, v := range r.Extras { + c.Extras[k] = slices.Clone(v) + } + } + c.Unusable = slices.Clone(r.Unusable) + c.MissingExtras = slices.Clone(r.MissingExtras) + c.YankedPins = slices.Clone(r.YankedPins) + return &c } // upgradeRootSet lists, sorted by name, each root project that base pinned, diff --git a/resolver/upgrade_report_test.go b/resolver/upgrade_report_test.go new file mode 100644 index 0000000..fde1429 --- /dev/null +++ b/resolver/upgrade_report_test.go @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package resolver_test + +import ( + "context" + "reflect" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/posit-dev/go-pyresolver/index" + "github.com/posit-dev/go-pyresolver/resolver" + "github.com/posit-dev/go-python-packaging/version" +) + +// expiringCtx is a ctx the test expires by hand, as a deadline would. +type expiringCtx struct { + context.Context + done chan struct{} + once sync.Once + hit atomic.Bool +} + +func newExpiringCtx() *expiringCtx { + return &expiringCtx{Context: context.Background(), done: make(chan struct{})} +} + +func (c *expiringCtx) Done() <-chan struct{} { return c.done } + +func (c *expiringCtx) Err() error { + if c.hit.Load() { + return context.DeadlineExceeded + } + return nil +} + +func (c *expiringCtx) expire() { + c.once.Do(func() { c.hit.Store(true); close(c.done) }) +} + +// passOff resolves reqs with the pass off. +func passOff(t *testing.T, idx index.MetadataIndex, reqs []string) *resolver.Resolution { + t.Helper() + res, err := resolver.Resolve(context.Background(), mustRequirements(t, reqs...), idx, testOptions(t)) + if err != nil { + t.Fatal(err) + } + return res +} + +func TestUpgradeReportIsNilWithThePassOff(t *testing.T) { + idx, reqs := earlyGain() + if res := passOff(t, idx, reqs); res.Upgrade != nil { + t.Errorf("Upgrade = %+v with the pass off, want nil", res.Upgrade) + } +} + +// A ctx that ends during a re-solve stops the pass by time. The pass keeps its +// best answer so far, and First is the pass-off answer. +func TestUpgradeReportCtxEndedMidReSolve(t *testing.T) { + for _, tc := range []struct { + name string + ctx func() (context.Context, func()) + }{ + {"deadline", func() (context.Context, func()) { c := newExpiringCtx(); return c, c.expire }}, + {"cancel", func() (context.Context, func()) { return context.WithCancel(context.Background()) }}, + } { + t.Run(tc.name, func(t *testing.T) { + idx, reqs := earlyGain() + want := passOff(t, idx, reqs) + ctx, stop := tc.ctx() + defer stop() + solves := 0 + // Solve 1 is the first solve, 2 raises a, 3 is stopped. + defer resolver.OnSolve(func() { + if solves++; solves == 3 { + stop() + } + })() + opts := testOptions(t) + opts.UpgradeRoots = 40 + opts.UpgradeStepFactor = 100 + res, err := resolver.Resolve(ctx, mustRequirements(t, reqs...), idx, opts) + if err != nil { + t.Fatal(err) + } + if got := pins(t, res); !reflect.DeepEqual(got, earlyA) { + t.Errorf("pins %v, want the best so far %v", got, earlyA) + } + if res.Upgrade == nil { + t.Fatal("Upgrade is nil") + } + if !res.Upgrade.StoppedByTime { + t.Error("StoppedByTime = false, want true") + } + if !reflect.DeepEqual(res.Upgrade.First, want) { + t.Errorf("First = %+v, want the pass-off answer %+v", res.Upgrade.First, want) + } + }) + } +} + +// resolveClocked resolves reqs with the pass on, on a fake clock where each +// solve takes solveCost. +func resolveClocked(t *testing.T, idx index.MetadataIndex, reqs []string, mutate func(*resolver.Options)) *resolver.Resolution { + t.Helper() + clock := time.Unix(0, 0) + defer resolver.SetClock(func() time.Time { return clock })() + defer resolver.OnSolve(func() { clock = clock.Add(solveCost) })() + opts := testOptions(t) + opts.UpgradeRoots = 40 + mutate(&opts) + res, err := resolver.Resolve(context.Background(), mustRequirements(t, reqs...), idx, opts) + if err != nil { + t.Fatal(err) + } + if res.Upgrade == nil { + t.Fatal("Upgrade is nil with the pass on") + } + return res +} + +func TestUpgradeReportTimeBoundsStopByTime(t *testing.T) { + idx, reqs := lateGain() + want := passOff(t, idx, reqs) + for name, set := range map[string]func(*resolver.Options){ + "max time": func(o *resolver.Options) { o.UpgradeMaxTime = solveCost }, + "factor": func(o *resolver.Options) { o.UpgradeTimeFactor = 1 }, + } { + res := resolveClocked(t, idx, reqs, set) + if !res.Upgrade.StoppedByTime { + t.Errorf("%s: StoppedByTime = false, want true", name) + } + if !reflect.DeepEqual(res.Upgrade.First, want) { + t.Errorf("%s: First = %+v, want %+v", name, res.Upgrade.First, want) + } + } +} + +// A re-solve cut by the time bound while it runs also stops the pass by time. +// The clock moves on every read, so the bound passes between the check that +// starts the re-solve and the re-solve itself. +func TestUpgradeReportReSolveCutByTime(t *testing.T) { + idx, reqs := lateGain() + const tick = time.Second + clock := time.Unix(0, 0) + defer resolver.SetClock(func() time.Time { c := clock; clock = clock.Add(tick); return c })() + solves := 0 + defer resolver.OnSolve(func() { solves++ })() + opts := testOptions(t) + opts.UpgradeRoots = 40 + opts.UpgradeMaxTime = tick + tick/2 + res, err := resolver.Resolve(context.Background(), mustRequirements(t, reqs...), idx, opts) + if err != nil { + t.Fatal(err) + } + if solves != 2 { + t.Fatalf("%d solves, want the first solve and one cut re-solve", solves) + } + if got := pins(t, res); !reflect.DeepEqual(got, lateBase) { + t.Errorf("pins %v, want %v", got, lateBase) + } + if res.Upgrade == nil || !res.Upgrade.StoppedByTime { + t.Errorf("Upgrade = %+v, want StoppedByTime", res.Upgrade) + } +} + +// A pass that ends on the step bound, the budget, or by itself is not stopped +// by time, even when each solve is slow. +func TestUpgradeReportOtherEndsAreNotTime(t *testing.T) { + _, re := unbounded(t) + idx, reqs := lateGain() + for _, tc := range []struct { + name string + set func(*resolver.Options) + want map[string]string + }{ + {"step bound", func(o *resolver.Options) { o.UpgradeMaxSteps = re[0] + re[1] - 1 }, lateBase}, + {"step bound, loose time bound", func(o *resolver.Options) { + o.UpgradeMaxSteps = re[0] + re[1] - 1 + o.UpgradeMaxTime = 100 * solveCost + }, lateBase}, + {"budget", func(o *resolver.Options) { o.UpgradeRoots = 1 }, lateBase}, + {"no candidates left", func(*resolver.Options) {}, lateZ}, + } { + res := resolveClocked(t, idx, reqs, tc.set) + if got := pins(t, res); !reflect.DeepEqual(got, tc.want) { + t.Errorf("%s: pins %v, want %v", tc.name, got, tc.want) + } + if res.Upgrade.StoppedByTime { + t.Errorf("%s: StoppedByTime = true, want false", tc.name) + } + } +} + +// First and the outer answer share no maps or slices, even when the pass +// keeps the first answer. +func TestUpgradeReportFirstIsNotAliased(t *testing.T) { + idx, reqs := lateGain() + res := resolveClocked(t, idx, reqs, func(o *resolver.Options) { o.UpgradeRoots = 1 }) + first := res.Upgrade.First + if first == res { + t.Fatal("First is the outer Resolution") + } + if first.Upgrade != nil { + t.Errorf("First.Upgrade = %+v, want nil", first.Upgrade) + } + if !reflect.DeepEqual(pins(t, first), lateBase) { + t.Fatalf("First pins %v, want %v", pins(t, first), lateBase) + } + res.Pinned[index.NewPackageName("a")] = version.MustParse("9.0") + if len(res.Order) > 0 { + res.Order[0] = index.NewPackageName("zzz") + } + if !reflect.DeepEqual(pins(t, first), lateBase) { + t.Errorf("mutating the outer answer changed First: %v", pins(t, first)) + } + if want := passOff(t, idx, reqs); !reflect.DeepEqual(first.Order, want.Order) { + t.Errorf("mutating the outer answer changed First.Order: %v", first.Order) + } +} From 70d2b47c3e3c490769fe256489ae74e70a3494d2 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Fri, 20 Nov 2026 16:33:44 -0500 Subject: [PATCH 2/3] Count only a ctx end the upgrade pass ran into as a time stop A ctx that ends just after the pass returned did not cut it short. Co-Authored-By: Claude Opus 5.5 --- resolver/resolver.go | 2 +- resolver/upgrade.go | 34 ++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/resolver/resolver.go b/resolver/resolver.go index 8be9b55..fc9a71b 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -340,7 +340,7 @@ type UpgradeReport struct { First *Resolution // StoppedByTime is true when time cut the pass short: UpgradeTimeFactor or - // UpgradeMaxTime was reached, or ctx ended before the pass did. The kept + // UpgradeMaxTime was reached, or the pass ran into the end of ctx. The kept // answer then depends on timing; use First for a repeatable one. A pass // ended by the step bounds, the UpgradeRoots budget, or running out of // roots to try is not stopped by time. diff --git a/resolver/upgrade.go b/resolver/upgrade.go index 4fc41a7..d2dd75f 100644 --- a/resolver/upgrade.go +++ b/resolver/upgrade.go @@ -42,6 +42,9 @@ func levelsBetween(hi, lo level) int { type usability struct { p *provider.Provider memo map[usableKey]bool + + // failed, if set, is called when an index lookup fails. + failed func() } type usableKey struct { @@ -58,6 +61,9 @@ func (u *usability) ok(name index.PackageName, v version.Version) bool { } got, err := u.p.Usable(name, v) if err != nil { + if u.failed != nil { + u.failed() + } return false } u.memo[k] = got @@ -263,12 +269,12 @@ func upgradeRoots( // First must not alias the answer the caller gets. best = cloneResolution(base) } - // ctx errors are sticky, so any way a ctx end reached the pass shows here. - best.Upgrade = &UpgradeReport{First: base, StoppedByTime: byTime || ctx.Err() != nil} + best.Upgrade = &UpgradeReport{First: base, StoppedByTime: byTime} return best } -// upgradePass runs the pass. byTime is whether a time bound ended it. +// upgradePass runs the pass. byTime is whether a time bound or the end of ctx +// cut it short. func upgradePass( ctx context.Context, reqs []requirement.Requirement, @@ -286,6 +292,14 @@ func upgradePass( // ends. A re-solve stopped by the step bound leaves spent at stepLimit. cut := false clockUp := false // the time bound was reached between re-solves + // ctxEnded notes a ctx end the pass ran into, as opposed to one after it. + ctxCut := false + ctxEnded := func() bool { + if ctx.Err() != nil { + ctxCut = true + } + return ctxCut + } timeUp := func() bool { if cut || (stepped && spent >= stepLimit) { return true @@ -297,9 +311,10 @@ func upgradePass( p: provider.New(ctx, idx, providerOptions(reqs, opts)), memo: map[usableKey]bool{}, } + u.failed = func() { ctxEnded() } roots := upgradeRootSet(ctx, reqs, idx, opts, base, u) if len(roots) == 0 { - return base, false + return base, ctxCut } best := base // best is scored only once a re-solve has an answer to compare it with: @@ -342,7 +357,7 @@ func upgradePass( spent += steps.used if err != nil { // Stopped by the time bound, not the caller: no gain, and the pass ends. - if ctx.Err() == nil && solveCtx.Err() != nil { + if !ctxEnded() && solveCtx.Err() != nil { cut = true } return nil @@ -361,7 +376,7 @@ func upgradePass( return got } - for budget > 0 && ctx.Err() == nil && !timeUp() { + for budget > 0 && !ctxEnded() && !timeUp() { // The untried root furthest behind (at least one level); the first by // name on a tie. A root behind only in versions is not tried. var pick *upRoot @@ -409,7 +424,7 @@ func upgradePass( lo := accept(got) // highest level index known to work hi := len(bounds) // lowest level index known to fail triedTop := false - for hi-lo > 1 && budget > 0 && ctx.Err() == nil && !timeUp() { + for hi-lo > 1 && budget > 0 && !ctxEnded() && !timeUp() { mid := hi - 1 if triedTop { mid = (lo + hi) / 2 @@ -422,7 +437,7 @@ func upgradePass( } } } - return best, cut || clockUp + return best, cut || clockUp || ctxCut } // cloneResolution copies r's maps and slices, so the copy shares none with r. @@ -475,6 +490,9 @@ func upgradeRootSet( } vs, err := idx.Versions(ctx, n) if err != nil { + if u.failed != nil { + u.failed() + } continue } if len(vs) == 0 { From 3f80c7de60de0d646aa7e33616b202d4002bc9a1 Mon Sep 17 00:00:00 2001 From: Jonathan Yoder Date: Fri, 20 Nov 2026 16:51:50 -0500 Subject: [PATCH 3/3] Catch a ctx end hidden by scoring; widen upgrade report tests An index can report a ended ctx as unusable metadata, so check ctx after scoring a re-solve. Test ctx ends at a loop check, in the root set and in a usability lookup; test that cloneResolution copies every field. Make the StoppedByTime doc and the CHANGELOG entry match. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- resolver/export_test.go | 7 ++ resolver/resolver.go | 7 +- resolver/upgrade.go | 5 +- resolver/upgrade_clone_internal_test.go | 75 ++++++++++++++++ resolver/upgrade_report_test.go | 108 ++++++++++++++++++++++++ 6 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 resolver/upgrade_clone_internal_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c194a..9e7aa51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ served it. ### Added -- `Resolution.Upgrade` reports on the `UpgradeRoots` pass: `First` is the first solve's answer, the one the pass-off run gives, and `StoppedByTime` says whether `UpgradeTimeFactor`, `UpgradeMaxTime` or the end of `ctx` cut the pass short. A caller that wants a repeatable answer can use `First` when time stopped the pass, without solving again. It is nil with the pass off, and the kept answer does not change. +- `Resolution.Upgrade` holds the `UpgradeRoots` pass's first answer (`First`) and whether a time bound or the end of `ctx` cut the pass short (`StoppedByTime`), so a caller can fall back to the first answer without solving again. It is nil with the pass off, and the answer the pass keeps does not change. ## [0.27.0] - 2026-11-18 diff --git a/resolver/export_test.go b/resolver/export_test.go index 03c5ed1..6ef5678 100644 --- a/resolver/export_test.go +++ b/resolver/export_test.go @@ -62,3 +62,10 @@ func OnCandidate(f func(got *Resolution, kept bool)) (restore func()) { onCandidate = f return func() { onCandidate = nil } } + +// AfterSolve calls f after each solve Resolve runs until restore is called. +// Not safe with parallel tests. +func AfterSolve(f func()) (restore func()) { + onSteps = func(int) { f() } + return func() { onSteps = nil } +} diff --git a/resolver/resolver.go b/resolver/resolver.go index fc9a71b..25a82a1 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -328,7 +328,8 @@ type Resolution struct { // caller can warn. Sorted by (package, requester). YankedPins []YankedPin - // Upgrade reports on the UpgradeRoots pass. It is nil when the pass is off. + // Upgrade reports on the UpgradeRoots pass. It is nil when the pass is off + // or the first solve fails. Upgrade *UpgradeReport } @@ -343,7 +344,9 @@ type UpgradeReport struct { // UpgradeMaxTime was reached, or the pass ran into the end of ctx. The kept // answer then depends on timing; use First for a repeatable one. A pass // ended by the step bounds, the UpgradeRoots budget, or running out of - // roots to try is not stopped by time. + // roots to try is not stopped by time. It can still be true if time ran out + // just as the pass would have ended anyway; a caller then only falls back + // to First. StoppedByTime bool } diff --git a/resolver/upgrade.go b/resolver/upgrade.go index d2dd75f..8f28287 100644 --- a/resolver/upgrade.go +++ b/resolver/upgrade.go @@ -314,7 +314,7 @@ func upgradePass( u.failed = func() { ctxEnded() } roots := upgradeRootSet(ctx, reqs, idx, opts, base, u) if len(roots) == 0 { - return base, ctxCut + return base, ctxEnded() } best := base // best is scored only once a re-solve has an answer to compare it with: @@ -367,6 +367,8 @@ func upgradePass( scored = true } kept := objective(roots, got).less(bestScore) + // An index may report a ctx end as unusable metadata, so scoring hides it. + ctxEnded() if onCandidate != nil { onCandidate(got, kept) } @@ -390,6 +392,7 @@ func upgradePass( } } if pick == nil { + ctxEnded() // the pick read the index too break } done[pick.name] = true diff --git a/resolver/upgrade_clone_internal_test.go b/resolver/upgrade_clone_internal_test.go new file mode 100644 index 0000000..d373f49 --- /dev/null +++ b/resolver/upgrade_clone_internal_test.go @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +package resolver + +import ( + "reflect" + "testing" + + "github.com/posit-dev/go-pyresolver/index" + "github.com/posit-dev/go-pyresolver/provider" + "github.com/posit-dev/go-python-packaging/version" +) + +// fullResolution fills every map and slice field of a Resolution. +func fullResolution() *Resolution { + a := index.NewPackageName("a") + v := version.MustParse("1.0") + root := Requester{Root: true} + return &Resolution{ + Pinned: map[index.PackageName]version.Version{a: v}, + Order: []index.PackageName{a}, + Extras: map[index.PackageName][]string{a: {"x"}}, + Unusable: []provider.Unusable{{Package: provider.Project(a), Version: v, Reason: "r"}}, + MissingExtras: []MissingExtra{{Package: a, Version: v, Extra: "y", RequestedBy: root}}, + YankedPins: []YankedPin{{Package: a, Version: v, RequestedBy: root}}, + } +} + +func TestCloneResolutionSharesNothing(t *testing.T) { + r, want := fullResolution(), fullResolution() + c := cloneResolution(r) + a := index.NewPackageName("a") + c.Pinned[a] = version.MustParse("9.0") + c.Order[0] = index.NewPackageName("z") + c.Extras[a][0] = "changed" + c.Unusable[0].Reason = "changed" + c.MissingExtras[0].Extra = "changed" + c.YankedPins[0].Package = index.NewPackageName("z") + if !reflect.DeepEqual(r, want) { + t.Errorf("changing the copy changed the original: %+v", r) + } +} + +// Fails when Resolution gains a map, slice or pointer field that +// cloneResolution does not copy. +func TestCloneResolutionCoversEveryField(t *testing.T) { + r := fullResolution() + c := cloneResolution(r) + rv, cv := reflect.ValueOf(r).Elem(), reflect.ValueOf(c).Elem() + for i := range rv.NumField() { + name := rv.Type().Field(i).Name + f, g := rv.Field(i), cv.Field(i) + switch f.Kind() { + case reflect.Map, reflect.Slice: + if f.Len() == 0 { + t.Errorf("%s: fill it in fullResolution", name) + continue + } + if f.UnsafePointer() == g.UnsafePointer() { + t.Errorf("%s: cloneResolution shares it", name) + } + if f.Kind() == reflect.Map && f.Type().Elem().Kind() == reflect.Slice { + for _, k := range f.MapKeys() { + if f.MapIndex(k).UnsafePointer() == g.MapIndex(k).UnsafePointer() { + t.Errorf("%s[%v]: cloneResolution shares the inner slice", name, k) + } + } + } + case reflect.Pointer, reflect.Interface, reflect.Chan, reflect.Func: + if name != "Upgrade" { // nil on the answer upgradeRoots copies + t.Errorf("%s: decide whether cloneResolution must copy it", name) + } + } + } +} diff --git a/resolver/upgrade_report_test.go b/resolver/upgrade_report_test.go index fde1429..39aff66 100644 --- a/resolver/upgrade_report_test.go +++ b/resolver/upgrade_report_test.go @@ -221,3 +221,111 @@ func TestUpgradeReportFirstIsNotAliased(t *testing.T) { t.Errorf("mutating the outer answer changed First.Order: %v", first.Order) } } + +// hookIndex calls hooks on index reads once armed. With hideCtx, it reports a +// ctx end as unusable metadata, as an index may. +type hookIndex struct { + *index.MockIndex + armed bool + hideCtx bool + onVersions func(index.PackageName) + onMetadata func(index.PackageName, version.Version) +} + +func (h *hookIndex) Versions(ctx context.Context, pkg index.PackageName) ([]version.Version, error) { + if h.armed && h.onVersions != nil { + h.onVersions(pkg) + } + return h.MockIndex.Versions(ctx, pkg) +} + +func (h *hookIndex) Metadata(ctx context.Context, pkg index.PackageName, ver version.Version) (index.PackageMetadata, error) { + if h.armed && h.onMetadata != nil { + h.onMetadata(pkg, ver) + } + if h.hideCtx && ctx.Err() != nil { + return index.PackageMetadata{}, index.ErrMetadataUnavailable + } + return h.MockIndex.Metadata(ctx, pkg, ver) +} + +// resolveCtx resolves reqs with the pass on and ctx, and checks the report. +func resolveCtx(t *testing.T, ctx context.Context, idx index.MetadataIndex, reqs []string, roots int) *resolver.Resolution { + t.Helper() + opts := testOptions(t) + opts.UpgradeRoots = roots + res, err := resolver.Resolve(ctx, mustRequirements(t, reqs...), idx, opts) + if err != nil { + t.Fatal(err) + } + if res.Upgrade == nil { + t.Fatal("Upgrade is nil with the pass on") + } + return res +} + +// A ctx that ends after the last budgeted re-solve is still seen when scoring +// that re-solve reads the index, even if the index hides the ctx error. +func TestUpgradeReportCtxEndedWhileScoringLastReSolve(t *testing.T) { + mock, reqs := earlyGain() + idx := &hookIndex{MockIndex: mock, hideCtx: true} + ctx := newExpiringCtx() + solves := 0 + defer resolver.AfterSolve(func() { + if solves++; solves == 2 { + ctx.expire() + } + })() + res := resolveCtx(t, ctx, idx, reqs, 1) + if !res.Upgrade.StoppedByTime { + t.Error("StoppedByTime = false, want true") + } +} + +// A ctx that ends between re-solves is seen at the next loop check. +func TestUpgradeReportCtxEndedBetweenReSolves(t *testing.T) { + idx, reqs := earlyGain() + ctx := newExpiringCtx() + defer resolver.OnCandidate(func(*resolver.Resolution, bool) { ctx.expire() })() + res := resolveCtx(t, ctx, idx, reqs, 40) + if got := pins(t, res); !reflect.DeepEqual(got, earlyA) { + t.Errorf("pins %v, want %v", got, earlyA) + } + if !res.Upgrade.StoppedByTime { + t.Error("StoppedByTime = false, want true") + } +} + +// A ctx that ends while the pass lists the roots' versions. +func TestUpgradeReportCtxEndedBuildingRoots(t *testing.T) { + mock, reqs := earlyGain() + idx := &hookIndex{MockIndex: mock} + ctx := newExpiringCtx() + idx.onVersions = func(pkg index.PackageName) { + if pkg == index.NewPackageName("d") { // the last root + ctx.expire() + } + } + defer resolver.AfterSolve(func() { idx.armed = true })() + res := resolveCtx(t, ctx, idx, reqs, 40) + if !res.Upgrade.StoppedByTime { + t.Error("StoppedByTime = false, want true") + } +} + +// A ctx that ends during a usability lookup for the last root. +func TestUpgradeReportCtxEndedInUsabilityLookup(t *testing.T) { + mock, reqs := lateGain() + idx := &hookIndex{MockIndex: mock} + ctx := newExpiringCtx() + idx.onMetadata = func(pkg index.PackageName, v version.Version) { + if pkg == index.NewPackageName("z") && v.String() == "3.0" { + ctx.expire() + } + } + defer resolver.AfterSolve(func() { idx.armed = true })() + res := resolveCtx(t, ctx, idx, reqs, 40) + if !res.Upgrade.StoppedByTime { + t.Error("StoppedByTime = false, want true") + } +}