@@ -869,6 +869,110 @@ public async Task GivenPackageHashPublicationInProgressThePackageIsNotReportedAs
869869 NupkgMetadataFileFormat . Read ( metadataPath ) . ContentHash . Should ( ) . Be ( expectedContentHash ) ;
870870 }
871871
872+ [ Theory ]
873+ [ InlineData ( false ) ]
874+ [ InlineData ( true ) ]
875+ public async Task GivenMetadataPublicationFailsItPreservesThePackageForAnotherInstall ( bool metadataPublished )
876+ {
877+ string source = GetTestLocalFeedPath ( ) ;
878+ var root = _testAssetsManager . CreateTestDirectory ( identifier : metadataPublished . ToString ( ) ) ;
879+ var packagesRoot = new DirectoryPath ( Path . Combine ( root . Path , "packages" ) ) ;
880+ string runtimeJsonPath = Path . Combine (
881+ SdkTestContext . Current . ToolsetUnderTest . SdkFolderUnderTest ,
882+ "RuntimeIdentifierGraph.json" ) ;
883+ var packageVersion = NuGetVersion . Parse ( TestPackageVersion ) ;
884+ var packageSourceLocation = new PackageSourceLocation ( additionalSourceFeeds : [ source ] , basePath : root . Path ) ;
885+ var pathResolver = new VersionFolderPathResolver ( packagesRoot . Value ) ;
886+ string hashPath = pathResolver . GetHashPath ( TestPackageId . ToString ( ) , packageVersion ) ;
887+ string metadataPath = pathResolver . GetNupkgMetadataPath ( TestPackageId . ToString ( ) , packageVersion ) ;
888+ var metadataPublicationReached = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
889+ using var continuePublication = new ManualResetEventSlim ( initialState : false ) ;
890+ Action failPublication = ( ) =>
891+ {
892+ metadataPublicationReached . SetResult ( ) ;
893+ continuePublication . Wait ( ) ;
894+ throw new IOException ( "Simulated metadata publication failure." ) ;
895+ } ;
896+ using var downloader = new TestableToolPackageDownloader (
897+ new ToolPackageStoreAndQuery ( new DirectoryPath ( Path . Combine ( root . Path , "tools" ) ) ) ,
898+ runtimeJsonPath ,
899+ root . Path ,
900+ beforeMetadataPublication : metadataPublished ? null : failPublication ,
901+ afterMetadataPublication : metadataPublished ? failPublication : null ) ;
902+ using var reusingDownloader = new TestableToolPackageDownloader (
903+ new ToolPackageStoreAndQuery ( new DirectoryPath ( Path . Combine ( root . Path , "reusing-tools" ) ) ) ,
904+ runtimeJsonPath ,
905+ root . Path ) ;
906+
907+ Task < NuGetVersion > download = Task . Run ( ( ) =>
908+ {
909+ using FileStream installLock = ToolPackageDownloaderBase . AcquireToolInstallLock (
910+ packagesRoot , TestPackageId , packageVersion ) ;
911+ return downloader . DownloadAndExtract (
912+ TestPackageId ,
913+ downloader . CreatePackageDownloader ( ) ,
914+ packagesRoot . Value ,
915+ packageVersion ,
916+ packageSourceLocation ) ;
917+ } ) ;
918+
919+ Task < IToolPackage > reuse ;
920+ string publishedHash ;
921+ try
922+ {
923+ Task firstCompleted = await Task . WhenAny ( metadataPublicationReached . Task , download )
924+ . WaitAsync ( TimeSpan . FromSeconds ( 30 ) ) ;
925+ firstCompleted . Should ( ) . Be ( metadataPublicationReached . Task ) ;
926+ publishedHash = File . ReadAllText ( hashPath ) ;
927+ File . Exists ( metadataPath ) . Should ( ) . Be ( metadataPublished ) ;
928+ reuse = Task . Run ( ( ) => reusingDownloader . InstallFromCache (
929+ TestPackageId ,
930+ packageVersion ,
931+ packagesRoot ,
932+ packageSourceLocation ,
933+ _testTargetframework ) ) ;
934+ if ( metadataPublished )
935+ {
936+ IToolPackage acceptedPackage = await reuse . WaitAsync ( TimeSpan . FromSeconds ( 30 ) ) ;
937+ File . Exists ( acceptedPackage . Command . Executable . Value ) . Should ( ) . BeTrue ( ) ;
938+ }
939+ else
940+ {
941+ ( await Task . WhenAny ( reuse , Task . Delay ( TimeSpan . FromMilliseconds ( 250 ) ) ) )
942+ . Should ( ) . NotBe ( reuse , "NuGet must wait for the writer before it can repair the missing metadata" ) ;
943+ }
944+ }
945+ finally
946+ {
947+ continuePublication . Set ( ) ;
948+ }
949+
950+ Func < Task > awaitDownload = async ( ) => await download ;
951+ await awaitDownload . Should ( ) . ThrowAsync < IOException > ( )
952+ . WithMessage ( "Simulated metadata publication failure." ) ;
953+ IToolPackage reusedPackage = await reuse . WaitAsync ( TimeSpan . FromSeconds ( 30 ) ) ;
954+ File . Exists ( reusedPackage . Command . Executable . Value ) . Should ( ) . BeTrue (
955+ "metadata publication failure must not delete a package accepted by another install" ) ;
956+ File . ReadAllText ( hashPath ) . Should ( ) . Be ( publishedHash ) ;
957+ File . Exists ( metadataPath ) . Should ( ) . BeTrue (
958+ "NuGet must preserve published metadata or repair it after the writer releases its lock" ) ;
959+ var packageDownloader = new StaticPackagePathNuGetPackageDownloader ( "must-not-download" ) ;
960+ reusingDownloader . DownloadAndExtract (
961+ TestPackageId ,
962+ packageDownloader ,
963+ packagesRoot . Value ,
964+ packageVersion ,
965+ packageSourceLocation ) . Should ( ) . Be ( packageVersion ) ;
966+ packageDownloader . ExtractCalled . Should ( ) . BeFalse ( ) ;
967+ File . ReadAllText ( hashPath ) . Should ( ) . Be ( publishedHash ) ;
968+ File . Exists ( reusedPackage . Command . Executable . Value ) . Should ( ) . BeTrue ( ) ;
969+ using FileStream packageStream = File . OpenRead (
970+ pathResolver . GetPackageFilePath ( TestPackageId . ToString ( ) , packageVersion ) ) ;
971+ using PackageArchiveReader reader = new ( packageStream ) ;
972+ NupkgMetadataFileFormat . Read ( metadataPath ) . ContentHash . Should ( ) . Be (
973+ reader . GetContentHash ( CancellationToken . None , ( ) => publishedHash ) ) ;
974+ }
975+
872976 [ Fact ]
873977 public async Task GivenNuGetsPackageLockToolInstallationWaitsForTheCanonicalWriter ( )
874978 {
@@ -1451,6 +1555,8 @@ private static string GetTestLocalFeedPath() =>
14511555 private sealed class TestableToolPackageDownloader : ToolPackageDownloader , IDisposable
14521556 {
14531557 private readonly bool _pauseHashPublication ;
1558+ private readonly Action _beforeMetadataPublication ;
1559+ private readonly Action _afterMetadataPublication ;
14541560 private readonly TaskCompletionSource _hashMoveStarted =
14551561 new ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
14561562 private readonly ManualResetEventSlim _continueHashMove = new ( initialState : false ) ;
@@ -1459,10 +1565,14 @@ public TestableToolPackageDownloader(
14591565 IToolPackageStore store ,
14601566 string runtimeJsonPathForTests ,
14611567 string currentWorkingDirectory ,
1462- bool pauseHashPublication = false )
1568+ bool pauseHashPublication = false ,
1569+ Action beforeMetadataPublication = null ,
1570+ Action afterMetadataPublication = null )
14631571 : base ( store , runtimeJsonPathForTests , currentWorkingDirectory )
14641572 {
14651573 _pauseHashPublication = pauseHashPublication ;
1574+ _beforeMetadataPublication = beforeMetadataPublication ;
1575+ _afterMetadataPublication = afterMetadataPublication ;
14661576 }
14671577
14681578 public Task HashMoveStarted => _hashMoveStarted . Task ;
@@ -1490,6 +1600,32 @@ public NuGetVersion DownloadAndExtract(
14901600 public bool IsInstalled ( PackageId packageId , NuGetVersion packageVersion , string packagesRootPath ) =>
14911601 IsPackageInstalled ( packageId , packageVersion , packagesRootPath ) ;
14921602
1603+ public IToolPackage InstallFromCache (
1604+ PackageId packageId ,
1605+ NuGetVersion packageVersion ,
1606+ DirectoryPath packagesRoot ,
1607+ PackageSourceLocation packageSourceLocation ,
1608+ string targetFramework )
1609+ {
1610+ DownloadTool (
1611+ packagesRoot ,
1612+ packageId ,
1613+ packageVersion ,
1614+ CreatePackageDownloader ( ) ,
1615+ packageSourceLocation ,
1616+ givenSpecificVersion : true ,
1617+ assetFileDirectory : _localToolAssetDir ,
1618+ acquireInstallLock : true ,
1619+ targetFramework ,
1620+ TestVerbosity ) ;
1621+ return new ToolPackageInstance (
1622+ packageId ,
1623+ packageVersion ,
1624+ packagesRoot ,
1625+ _localToolAssetDir ,
1626+ _fileSystem ) ;
1627+ }
1628+
14931629 public void ContinueHashMove ( ) => _continueHashMove . Set ( ) ;
14941630
14951631 protected override void MovePackageHash ( string temporaryHashPath , string hashPath )
@@ -1503,6 +1639,13 @@ protected override void MovePackageHash(string temporaryHashPath, string hashPat
15031639 base . MovePackageHash ( temporaryHashPath , hashPath ) ;
15041640 }
15051641
1642+ protected override void MovePackageMetadata ( string temporaryMetadataPath , string metadataPath )
1643+ {
1644+ _beforeMetadataPublication ? . Invoke ( ) ;
1645+ base . MovePackageMetadata ( temporaryMetadataPath , metadataPath ) ;
1646+ _afterMetadataPublication ? . Invoke ( ) ;
1647+ }
1648+
15061649 public void Dispose ( ) => _continueHashMove . Dispose ( ) ;
15071650 }
15081651
0 commit comments