public async Task TestLockOnDifferentBlobClientTypes( [Values] BlobClientType type, [Values] bool isAsync) { if (isAsync) { await TestAsync(); } else { SyncViaAsync.Run(_ => TestAsync(), default(object)); } async ValueTask TestAsync() { using var provider = new TestingAzureBlobLeaseDistributedLockProvider(); var name = provider.GetUniqueSafeName(); var client = CreateClient(type, name); if (client is AppendBlobClient appendClient) { Assert.That( Assert.Throws <RequestFailedException>(() => appendClient.CreateIfNotExists()).ToString(), Does.Contain("This feature is not currently supported by the Storage Emulator") ); return; } if (client.GetType() == typeof(BlobBaseClient)) { // work around inability to do CreateIfNotExists for the base client new BlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name).Upload(Stream.Null); } var @lock = new AzureBlobLeaseDistributedLock(client); await using var handle = await @lock.TryAcquireAsync(); Assert.IsNotNull(handle); await using var nestedHandle = await @lock.TryAcquireAsync(); Assert.IsNull(nestedHandle); } }
public void TestCanUseLeaseIdForBlobOperations() { using var provider = new TestingAzureBlobLeaseDistributedLockProvider(); var name = provider.GetUniqueSafeName(); var client = new PageBlobClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName, name); const int BlobSize = 512; client.Create(size: BlobSize); var @lock = new AzureBlobLeaseDistributedLock(client); using var handle = @lock.Acquire(); Assert.Throws <RequestFailedException>(() => client.UploadPages(new MemoryStream(new byte[BlobSize]), offset: 0)) .ErrorCode.ShouldEqual(AzureErrors.LeaseIdMissing); Assert.DoesNotThrow( () => client.UploadPages(new MemoryStream(new byte[BlobSize]), offset: 0, conditions: new PageBlobRequestConditions { LeaseId = handle.LeaseId }) ); handle.Dispose(); Assert.Throws <ObjectDisposedException>(() => handle.LeaseId.ToString()); }
public async Task TestWrapperCreateIfNotExists([Values] BlobClientType type) { using var provider = new TestingAzureBlobLeaseDistributedLockProvider(); var name = provider.GetUniqueSafeName(); var client = CreateClient(type, name); var wrapper = new BlobClientWrapper(client); var metadata = new Dictionary <string, string> { ["abc"] = "123" }; if (client is AppendBlobClient) { Assert.That( Assert.ThrowsAsync <RequestFailedException>(async() => await wrapper.CreateIfNotExistsAsync(metadata, CancellationToken.None)).ToString(), Does.Contain("This feature is not currently supported by the Storage Emulator") ); return; } if (client.GetType() == typeof(BlobBaseClient)) { Assert.That( Assert.ThrowsAsync <InvalidOperationException>(async() => await wrapper.CreateIfNotExistsAsync(metadata, CancellationToken.None)).ToString(), Does.Contain("Either ensure that the blob exists or use a non-base client type") ); return; } await wrapper.CreateIfNotExistsAsync(metadata, CancellationToken.None); Assert.IsTrue((await client.ExistsAsync()).Value); CollectionAssert.AreEqual(metadata, (await client.GetPropertiesAsync()).Value.Metadata); Assert.DoesNotThrowAsync(async() => await wrapper.CreateIfNotExistsAsync(metadata, CancellationToken.None)); Assert.IsTrue((await client.ExistsAsync()).Value); }