Skip to content

Commit 93aba7b

Browse files
surayya-MSbaronfel
authored andcommitted
add test
1 parent dc3c015 commit 93aba7b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,34 @@ public void IsRegistryInsecure(string registryName, string? insecureRegistriesEn
539539
Assert.Equal(expectedInsecure, registrySettings.IsInsecure);
540540
}
541541

542+
[Fact]
543+
public async Task DownloadBlobAsync_RetriesOnFailure()
544+
{
545+
// Arrange
546+
var logger = _loggerFactory.CreateLogger(nameof(DownloadBlobAsync_RetriesOnFailure));
547+
548+
var repoName = "testRepo";
549+
var descriptor = new Descriptor("application/octet-stream", "sha256:testdigest", 1234);
550+
var cancellationToken = CancellationToken.None;
551+
552+
var mockRegistryAPI = new Mock<IRegistryAPI>(MockBehavior.Strict);
553+
mockRegistryAPI
554+
.SetupSequence(api => api.Blob.GetStreamAsync(repoName, descriptor.Digest, cancellationToken))
555+
.ThrowsAsync(new Exception("Simulated failure 1")) // First attempt fails
556+
.ThrowsAsync(new Exception("Simulated failure 2")) // Second attempt fails
557+
.ReturnsAsync(new MemoryStream(new byte[] { 1, 2, 3 })); // Third attempt succeeds
558+
559+
Registry registry = new(repoName, logger, mockRegistryAPI.Object);
560+
561+
// Act
562+
var result = await registry.DownloadBlobAsync(repoName, descriptor, cancellationToken);
563+
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
568+
}
569+
542570
private static NextChunkUploadInformation ChunkUploadSuccessful(Uri requestUri, Uri uploadUrl, int? contentLength, HttpStatusCode code = HttpStatusCode.Accepted)
543571
{
544572
return new(uploadUrl);

0 commit comments

Comments
 (0)