public async void LeaseNewOrExistingBlockBlobAndMaintainLeaseAsync_NonExistentBlob_CreatesBlobAndAcquiresLease() { var expectedException = new BlobNotFoundAzureException("1", System.Net.HttpStatusCode.NotFound, "status", new Dictionary<string, string>(), null); var expectedLeaseResponse = new FakeLeaseBlobAcquireResponse("unit-test-abc123"); var expectedBlobResponse = new PutBlobResponse(); var mockClientExtensions = new Mock<IBlobServiceClientEx>(); // LeaseBlobAcquire - blob not found on first call, good response on second mockClientExtensions.SetupSequence(ce => ce.LeaseBlobAcquireAsync("container", "blob", It.IsAny<int>(), It.IsAny<string>())) .Throws(expectedException) .Returns(Task.Run(() => (LeaseBlobAcquireResponse)expectedLeaseResponse)); // PutBlockBlob mockClientExtensions.Setup(ce => ce.PutBlockBlobAsync("container", "blob", It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>())) .ReturnsAsync(expectedBlobResponse); var leaseResult = await BlobLeaseMaintainer.LeaseNewOrExistingBlockBlobAndMaintainLeaseAsync(mockClientExtensions.Object, "container", "blob", 2); Assert.AreEqual(expectedLeaseResponse.LeaseId, leaseResult.LeaseId); // temporary until we fix lack of auto-release await leaseResult.StopMaintainingAndClearLeaseAsync(); }
public async Task LeaseNewOrExistingBlockBlobAsync_NonExistentBlob_CreatesBlobAndAcquiresLease() { var expectedException = new BlobNotFoundAzureException("1",System.Net.HttpStatusCode.NotFound, "status", new Dictionary<string,string>(), null); var expectedLeaseResponse = new FakeLeaseBlobAcquireResponse("unit-test-abc123"); var expectedBlobResponse = new PutBlobResponse(); var mockClientExtensions = new Mock<IBlobServiceClientEx>(); // LeaseBlobAcquire - blob not found on first call, good response on second mockClientExtensions.SetupSequence(ce => ce.LeaseBlobAcquireAsync("container", "blob", It.IsAny<int>(), It.IsAny<string>())) .Throws(expectedException) .Returns(Task.Run(() => (LeaseBlobAcquireResponse)expectedLeaseResponse)); // PutBlockBlob mockClientExtensions.Setup(ce => ce.PutBlockBlobAsync("container", "blob", It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>())) .ReturnsAsync(expectedBlobResponse); var leaseResult = await BlobLeaseMaintainer.LeaseNewOrExistingBlockBlobAsync(mockClientExtensions.Object, "container", "blob", 30); Assert.AreEqual(expectedLeaseResponse, leaseResult); // 2 attempts to lease mockClientExtensions.Verify(ce => ce.LeaseBlobAcquireAsync("container", "blob", It.IsAny<int>(), It.IsAny<string>()), Times.Exactly(2)); // 1 attempt to create blob mockClientExtensions.Verify(ce => ce.PutBlockBlobAsync("container", "blob", It.IsAny<byte[]>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<string>()), Times.Once()); }