Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,27 @@ nativePackage.AvailableVersions is { } versions

private IReadOnlyList<CatalogPackage> GetCachedLocalWinGetPackages(int? cacheSeconds = null)
{
if (_localPackagesProvider is not null)
{
return _localPackagesProvider();
}
long sourceIndexGeneration = WinGet.SourceIndexGeneration;

return cacheSeconds is null
? TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(GetLocalWinGetPackages)
? TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(
EnumerateLocalWinGetPackages,
sourceIndexGeneration
)
: TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(
GetLocalWinGetPackages,
EnumerateLocalWinGetPackages,
sourceIndexGeneration,
cacheSeconds.Value
);
}

private IReadOnlyList<CatalogPackage> EnumerateLocalWinGetPackages(long sourceIndexGeneration)
{
return _localPackagesProvider is not null
? _localPackagesProvider()
: GetLocalWinGetPackages();
}

private IReadOnlyList<Package> GetAvailableUpdatesFromSystemCli(Exception ex)
{
var unwrappedException = UnwrapException(ex);
Expand Down
26 changes: 20 additions & 6 deletions src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ public void Dispose()
}
}

private static long _sourceIndexGeneration;

internal static long SourceIndexGeneration => Volatile.Read(ref _sourceIndexGeneration);

internal static void MarkSourceIndexRefreshed() =>
Interlocked.Increment(ref _sourceIndexGeneration);

public WinGet()
{
Capabilities = new ManagerCapabilities
Expand Down Expand Up @@ -811,12 +818,19 @@ public override void RefreshPackageIndexes()
p.StartInfo.Environment["TMP"] = WinGetTemp;
}

p.Start();
logger.AddToStdOut(p.StandardOutput.ReadToEnd());
logger.AddToStdErr(p.StandardError.ReadToEnd());
logger.Close(p.ExitCode);
p.WaitForExit();
p.Close();
try
{
p.Start();
logger.AddToStdOut(p.StandardOutput.ReadToEnd());
logger.AddToStdErr(p.StandardError.ReadToEnd());
logger.Close(p.ExitCode);
p.WaitForExit();
p.Close();
}
finally
{
MarkSourceIndexRefreshed();
}
}

private string GetCliToolProxyArgument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ private IReadOnlyList<IPackage> _getAvailableUpdates(bool SecondAttempt)
}
try
{
RefreshPackageIndexesSafely();
if (!SecondAttempt)
{
RefreshPackageIndexesSafely();
}

var packages = RunListingTaskWithTimeout(
GetAvailableUpdates_UnSafe,
Expand Down
4 changes: 2 additions & 2 deletions src/UniGetUI.PackageEngine.Tests/PackageManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void FindPackagesRetriesOnceAfterFailure()
}

[Fact]
public void GetAvailableUpdatesRetriesOnceAndRefreshesIndexesPerAttempt()
public void GetAvailableUpdatesRetriesOnceWithoutRefreshingIndexesAgain()
{
var manager = CreateReadyManager();
var attempts = 0;
Expand All @@ -283,7 +283,7 @@ public void GetAvailableUpdatesRetriesOnceAndRefreshesIndexesPerAttempt()
var package = Assert.Single(packages);
Assert.Equal("Contoso.Update", package.Id);
Assert.Equal(1, manager.AttemptFastRepairCalls);
Assert.Equal(2, manager.RefreshPackageIndexesCalls);
Assert.Equal(1, manager.RefreshPackageIndexesCalls);
}

[Fact]
Expand Down
59 changes: 59 additions & 0 deletions src/UniGetUI.PackageEngine.Tests/WinGetManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,65 @@ public void NativeWinGetHelperUsesSystemCliFallbackForUpdatesWhenCompositeCatalo
Assert.Equal("2.0.0", package.NewVersionString);
}

[Fact]
public void NativeWinGetHelperTakesANewCatalogSnapshotAfterTheSourceIndexIsRefreshed()
{
WinGet.MarkSourceIndexRefreshed();
int snapshots = 0;
var helper = new NativeWinGetHelper(
new TestableWinGet(),
systemCliHelperFactory: null,
skipInitialization: true,
localPackagesProvider: () =>
{
snapshots++;
return [];
}
);

helper.GetInstalledPackages_UnSafe();
Assert.Equal(1, snapshots);

WinGet.MarkSourceIndexRefreshed();
helper.GetAvailableUpdates_UnSafe();

Assert.Equal(2, snapshots);
}

[Fact]
public void NativeWinGetHelperReusesTheCatalogSnapshotWhileTheSourceIndexIsUnchanged()
{
WinGet.MarkSourceIndexRefreshed();
int snapshots = 0;
var helper = new NativeWinGetHelper(
new TestableWinGet(),
systemCliHelperFactory: null,
skipInitialization: true,
localPackagesProvider: () =>
{
snapshots++;
return [];
}
);

helper.GetInstalledPackages_UnSafe();
helper.GetAvailableUpdates_UnSafe();
helper.GetAvailableUpdates_UnSafe();

Assert.Equal(1, snapshots);
}

[Fact]
public void RefreshPackageIndexesAdvancesTheSourceIndexGenerationWhenTheCliCallFails()
{
var manager = new TestableWinGet();
long generationBefore = WinGet.SourceIndexGeneration;

Assert.ThrowsAny<Exception>(manager.RefreshPackageIndexes);

Assert.NotEqual(generationBefore, WinGet.SourceIndexGeneration);
}

[Fact]
public void NativeWinGetHelperSelectReachableCatalogsSkipsUnavailableSources()
{
Expand Down
Loading