Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
 private static BlobBaseClient CreateClient([Values] BlobClientType type, string name) => type switch
 {