Skip to content

Commit

Permalink
Add extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart ten Wolde authored and Lennart ten Wolde committed Apr 29, 2023
1 parent 784e0e6 commit e1a8611
Showing 1 changed file with 37 additions and 0 deletions.
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

0 comments on commit e1a8611

Please sign in to comment.