Skip to content

Commit f75e4c3

Browse files
surayya-MSbaronfel
authored andcommitted
cleanup inside finally block; add test to verify throw after max retries
1 parent ca1f8c4 commit f75e4c3

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

src/Tests/Microsoft.NET.Build.Containers.UnitTests/RegistryTests.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,56 @@ public async Task DownloadBlobAsync_RetriesOnFailure()
558558

559559
Registry registry = new(repoName, logger, mockRegistryAPI.Object, null, () => TimeSpan.Zero);
560560

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));
563587

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+
});
568609

569-
//Cleanup
570-
File.Delete(result);
610+
mockRegistryAPI.Verify(api => api.Blob.GetStreamAsync(repoName, descriptor.Digest, cancellationToken), Times.Exactly(5));
571611
}
572612

573613
private static NextChunkUploadInformation ChunkUploadSuccessful(Uri requestUri, Uri uploadUrl, int? contentLength, HttpStatusCode code = HttpStatusCode.Accepted)

0 commit comments

Comments
 (0)