@@ -558,16 +558,56 @@ public async Task DownloadBlobAsync_RetriesOnFailure()
558
558
559
559
Registry registry = new ( repoName , logger , mockRegistryAPI . Object , null , ( ) => TimeSpan . Zero ) ;
560
560
561
- // Act
562
- var result = await registry . DownloadBlobAsync ( repoName , descriptor , cancellationToken ) ;
561
+ string ? result = null ;
562
+ try
563
+ {
564
+ // Act
565
+ result = await registry . DownloadBlobAsync ( repoName , descriptor , cancellationToken ) ;
566
+
567
+ // Assert
568
+ Assert . NotNull ( result ) ;
569
+ Assert . True ( File . Exists ( result ) ) ; // Ensure the file was successfully downloaded
570
+ mockRegistryAPI . Verify ( api => api . Blob . GetStreamAsync ( repoName , descriptor . Digest , cancellationToken ) , Times . Exactly ( 3 ) ) ; // Verify retries
571
+ }
572
+ finally
573
+ {
574
+ // Cleanup
575
+ if ( result != null )
576
+ {
577
+ File . Delete ( result ) ;
578
+ }
579
+ }
580
+ }
581
+
582
+ [ Fact ]
583
+ public async Task DownloadBlobAsync_ThrowsAfterMaxRetries ( )
584
+ {
585
+ // Arrange
586
+ var logger = _loggerFactory . CreateLogger ( nameof ( DownloadBlobAsync_ThrowsAfterMaxRetries ) ) ;
563
587
564
- // Assert
565
- Assert . NotNull ( result ) ;
566
- Assert . True ( File . Exists ( result ) ) ; // Ensure the file was successfully downloaded
567
- mockRegistryAPI . Verify ( api => api . Blob . GetStreamAsync ( repoName , descriptor . Digest , cancellationToken ) , Times . Exactly ( 3 ) ) ; // Verify retries
588
+ var repoName = "testRepo" ;
589
+ var descriptor = new Descriptor ( SchemaTypes . OciLayerGzipV1 , "sha256:testdigest1234" , 1234 ) ;
590
+ var cancellationToken = CancellationToken . None ;
591
+
592
+ var mockRegistryAPI = new Mock < IRegistryAPI > ( MockBehavior . Strict ) ;
593
+ // Simulate 5 failures (assuming your retry logic attempts 5 times before throwing)
594
+ mockRegistryAPI
595
+ . SetupSequence ( api => api . Blob . GetStreamAsync ( repoName , descriptor . Digest , cancellationToken ) )
596
+ . ThrowsAsync ( new Exception ( "Simulated failure 1" ) )
597
+ . ThrowsAsync ( new Exception ( "Simulated failure 2" ) )
598
+ . ThrowsAsync ( new Exception ( "Simulated failure 3" ) )
599
+ . ThrowsAsync ( new Exception ( "Simulated failure 4" ) )
600
+ . ThrowsAsync ( new Exception ( "Simulated failure 5" ) ) ;
601
+
602
+ Registry registry = new ( repoName , logger , mockRegistryAPI . Object , null , ( ) => TimeSpan . Zero ) ;
603
+
604
+ // Act & Assert
605
+ await Assert . ThrowsAsync < UnableToDownloadFromRepositoryException > ( async ( ) =>
606
+ {
607
+ await registry . DownloadBlobAsync ( repoName , descriptor , cancellationToken ) ;
608
+ } ) ;
568
609
569
- //Cleanup
570
- File . Delete ( result ) ;
610
+ mockRegistryAPI . Verify ( api => api . Blob . GetStreamAsync ( repoName , descriptor . Digest , cancellationToken ) , Times . Exactly ( 5 ) ) ;
571
611
}
572
612
573
613
private static NextChunkUploadInformation ChunkUploadSuccessful ( Uri requestUri , Uri uploadUrl , int ? contentLength , HttpStatusCode code = HttpStatusCode . Accepted )
0 commit comments