Skip to content

Commit 1f4bd2d

Browse files
authored
Update WinGet catalog snapshot and ensure successful index refresh (#5365)
1 parent ba61e91 commit 1f4bd2d

5 files changed

Lines changed: 99 additions & 15 deletions

File tree

src/UniGetUI.PackageEngine.Managers.WinGet/ClientHelpers/NativeWinGetHelper.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,19 +448,27 @@ nativePackage.AvailableVersions is { } versions
448448

449449
private IReadOnlyList<CatalogPackage> GetCachedLocalWinGetPackages(int? cacheSeconds = null)
450450
{
451-
if (_localPackagesProvider is not null)
452-
{
453-
return _localPackagesProvider();
454-
}
451+
long sourceIndexGeneration = WinGet.SourceIndexGeneration;
455452

456453
return cacheSeconds is null
457-
? TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(GetLocalWinGetPackages)
454+
? TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(
455+
EnumerateLocalWinGetPackages,
456+
sourceIndexGeneration
457+
)
458458
: TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(
459-
GetLocalWinGetPackages,
459+
EnumerateLocalWinGetPackages,
460+
sourceIndexGeneration,
460461
cacheSeconds.Value
461462
);
462463
}
463464

465+
private IReadOnlyList<CatalogPackage> EnumerateLocalWinGetPackages(long sourceIndexGeneration)
466+
{
467+
return _localPackagesProvider is not null
468+
? _localPackagesProvider()
469+
: GetLocalWinGetPackages();
470+
}
471+
464472
private IReadOnlyList<Package> GetAvailableUpdatesFromSystemCli(Exception ex)
465473
{
466474
var unwrappedException = UnwrapException(ex);

src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public void Dispose()
9292
}
9393
}
9494

95+
private static long _sourceIndexGeneration;
96+
97+
internal static long SourceIndexGeneration => Volatile.Read(ref _sourceIndexGeneration);
98+
99+
internal static void MarkSourceIndexRefreshed() =>
100+
Interlocked.Increment(ref _sourceIndexGeneration);
101+
95102
public WinGet()
96103
{
97104
Capabilities = new ManagerCapabilities
@@ -811,12 +818,19 @@ public override void RefreshPackageIndexes()
811818
p.StartInfo.Environment["TMP"] = WinGetTemp;
812819
}
813820

814-
p.Start();
815-
logger.AddToStdOut(p.StandardOutput.ReadToEnd());
816-
logger.AddToStdErr(p.StandardError.ReadToEnd());
817-
logger.Close(p.ExitCode);
818-
p.WaitForExit();
819-
p.Close();
821+
try
822+
{
823+
p.Start();
824+
logger.AddToStdOut(p.StandardOutput.ReadToEnd());
825+
logger.AddToStdErr(p.StandardError.ReadToEnd());
826+
logger.Close(p.ExitCode);
827+
p.WaitForExit();
828+
p.Close();
829+
}
830+
finally
831+
{
832+
MarkSourceIndexRefreshed();
833+
}
820834
}
821835

822836
private string GetCliToolProxyArgument()

src/UniGetUI.PackageEngine.PackageManagerClasses/Manager/PackageManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ private IReadOnlyList<IPackage> _getAvailableUpdates(bool SecondAttempt)
463463
}
464464
try
465465
{
466-
RefreshPackageIndexesSafely();
466+
if (!SecondAttempt)
467+
{
468+
RefreshPackageIndexesSafely();
469+
}
467470

468471
var packages = RunListingTaskWithTimeout(
469472
GetAvailableUpdates_UnSafe,

src/UniGetUI.PackageEngine.Tests/PackageManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void FindPackagesRetriesOnceAfterFailure()
266266
}
267267

268268
[Fact]
269-
public void GetAvailableUpdatesRetriesOnceAndRefreshesIndexesPerAttempt()
269+
public void GetAvailableUpdatesRetriesOnceWithoutRefreshingIndexesAgain()
270270
{
271271
var manager = CreateReadyManager();
272272
var attempts = 0;
@@ -283,7 +283,7 @@ public void GetAvailableUpdatesRetriesOnceAndRefreshesIndexesPerAttempt()
283283
var package = Assert.Single(packages);
284284
Assert.Equal("Contoso.Update", package.Id);
285285
Assert.Equal(1, manager.AttemptFastRepairCalls);
286-
Assert.Equal(2, manager.RefreshPackageIndexesCalls);
286+
Assert.Equal(1, manager.RefreshPackageIndexesCalls);
287287
}
288288

289289
[Fact]

src/UniGetUI.PackageEngine.Tests/WinGetManagerTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,65 @@ public void NativeWinGetHelperUsesSystemCliFallbackForUpdatesWhenCompositeCatalo
735735
Assert.Equal("2.0.0", package.NewVersionString);
736736
}
737737

738+
[Fact]
739+
public void NativeWinGetHelperTakesANewCatalogSnapshotAfterTheSourceIndexIsRefreshed()
740+
{
741+
WinGet.MarkSourceIndexRefreshed();
742+
int snapshots = 0;
743+
var helper = new NativeWinGetHelper(
744+
new TestableWinGet(),
745+
systemCliHelperFactory: null,
746+
skipInitialization: true,
747+
localPackagesProvider: () =>
748+
{
749+
snapshots++;
750+
return [];
751+
}
752+
);
753+
754+
helper.GetInstalledPackages_UnSafe();
755+
Assert.Equal(1, snapshots);
756+
757+
WinGet.MarkSourceIndexRefreshed();
758+
helper.GetAvailableUpdates_UnSafe();
759+
760+
Assert.Equal(2, snapshots);
761+
}
762+
763+
[Fact]
764+
public void NativeWinGetHelperReusesTheCatalogSnapshotWhileTheSourceIndexIsUnchanged()
765+
{
766+
WinGet.MarkSourceIndexRefreshed();
767+
int snapshots = 0;
768+
var helper = new NativeWinGetHelper(
769+
new TestableWinGet(),
770+
systemCliHelperFactory: null,
771+
skipInitialization: true,
772+
localPackagesProvider: () =>
773+
{
774+
snapshots++;
775+
return [];
776+
}
777+
);
778+
779+
helper.GetInstalledPackages_UnSafe();
780+
helper.GetAvailableUpdates_UnSafe();
781+
helper.GetAvailableUpdates_UnSafe();
782+
783+
Assert.Equal(1, snapshots);
784+
}
785+
786+
[Fact]
787+
public void RefreshPackageIndexesAdvancesTheSourceIndexGenerationWhenTheCliCallFails()
788+
{
789+
var manager = new TestableWinGet();
790+
long generationBefore = WinGet.SourceIndexGeneration;
791+
792+
Assert.ThrowsAny<Exception>(manager.RefreshPackageIndexes);
793+
794+
Assert.NotEqual(generationBefore, WinGet.SourceIndexGeneration);
795+
}
796+
738797
[Fact]
739798
public void NativeWinGetHelperSelectReachableCatalogsSkipsUnavailableSources()
740799
{

0 commit comments

Comments
 (0)