Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing singleton tests #13

Merged
merged 4 commits into from
Apr 29, 2023
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 @@ -118,8 +118,7 @@ private static async Task<bool> TryCreateBlobAsync(BlobClient blobClient, Cancel
}
catch (RequestFailedException exception) when (exception.Status == 404)
{
var containerClient = blobClient.GetParentBlobContainerClient();
await containerClient.CreateAsync(cancellationToken: cancellationToken);
await TryCreateBlobContainerAsync(blobClient, cancellationToken);

using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(string.Empty)))
{
Expand All @@ -133,5 +132,19 @@ private static async Task<bool> TryCreateBlobAsync(BlobClient blobClient, Cancel
return false;
}
}

private static async Task TryCreateBlobContainerAsync(BlobClient blobClient, CancellationToken cancellationToken)
{
try
{
var containerClient = blobClient.GetParentBlobContainerClient();
await containerClient.CreateAsync(cancellationToken: cancellationToken);
}
catch (RequestFailedException exception) when (exception.Status == 409 || exception.Status == 412)
{
// The container already exists
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async Task Given_a_Function_with_a_singleton_attribute_when_a_queue_has_t
await host.HandleAsync(CancellationToken.None);

// Assert
fakeServiceMock.Verify(f => f.Execute(It.IsAny<Message>()), Times.AtLeastOnce());
fakeServiceMock.Verify(f => f.Execute(It.IsAny<Message>()), Times.Exactly(messages.Length));
var peekedMessage = await _queueClient.PeekMessageAsync();
peekedMessage.Value.Should().BeNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public async Task Given_a_Function_with_a_singleton_attribute_when_a_queue_has_t
await host.HandleAsync(CancellationToken.None);

// Assert
fakeServiceMock.Verify(f => f.Execute(It.IsAny<Message>()), Times.AtLeastOnce());
fakeServiceMock.Verify(f => f.Execute(It.IsAny<Message>()), Times.Exactly(messages.Length));
var peekedMessage = await _queueClient.PeekMessageAsync();
peekedMessage.Value.Should().BeNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,43 @@ public async Task Given_a_DistributedLockManager_when_AcquireAsync_is_called_for
result.Should().Be(leaseId);
}

[Fact]
public async Task Given_a_DistributedLockManager_when_AcquireAsync_is_called_and_the_created_container_already_exists_then_the_lease_is_acquired_correctly()
{
// Arrange
var leaseId = "someId";
var blobPropertiesFake = BlobsModelFactory.BlobProperties(leaseState: LeaseState.Available);
var blobLeaseFake = BlobsModelFactory.BlobLease(new ETag(), DateTimeOffset.Now, leaseId: leaseId);
var blobClientFake = new Mock<BlobClient>(MockBehavior.Strict);
var blobLeaseClientFake = new Mock<BlobLeaseClient>(MockBehavior.Strict);
var blobContainerClientFake = new Mock<BlobContainerClient>(MockBehavior.Strict);
var blobLeaseResponseFake = new Mock<Response<BlobLease>>(MockBehavior.Strict);

var loggerMock = new Mock<ILogger>();

blobLeaseResponseFake.SetupGet(p => p.Value).Returns(blobLeaseFake);
blobLeaseClientFake.Setup(b => b.AcquireAsync(TimeSpan.FromSeconds(60), null, It.IsAny<CancellationToken>())).ReturnsAsync(blobLeaseResponseFake.Object);

blobClientFake.Protected().Setup<BlobLeaseClient>("GetBlobLeaseClientCore", ItExpr.IsAny<string>())
.Returns<string>((leaseId) => blobLeaseClientFake.Object);
blobClientFake.Setup(b => b.GetPropertiesAsync(null, It.IsAny<CancellationToken>())).ThrowsAsync(new RequestFailedException(404, "blob not found"));
blobClientFake.SetupSequence(b => b.UploadAsync(It.IsAny<Stream>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new RequestFailedException(404, "container not found"))
.ReturnsAsync(new Mock<Response<BlobContentInfo>>(MockBehavior.Strict).Object);

blobContainerClientFake.Setup(c => c.CreateAsync(PublicAccessType.None, null, null, It.IsAny<CancellationToken>())).ThrowsAsync(new RequestFailedException(409, "container already exists"));
blobClientFake.Protected().Setup<BlobContainerClient>("GetParentBlobContainerClientCore")
.Returns(() => blobContainerClientFake.Object);

var sut = new DistributedLockManager(blobClientFake.Object, loggerMock.Object);

// Act
var result = await sut.AcquireAsync(CancellationToken.None);

// Assert
result.Should().Be(leaseId);
}

[Theory]
[InlineData(409)]
[InlineData(412)]
Expand Down