Exemple #1
0
        public void TestSafeNaming()
        {
            var names = new[]
            {
                string.Empty,
                new string('a', 2000),
                "a/",
                "a\\",
                string.Join("/", Enumerable.Repeat("a", 254)),
                string.Join(@"\", Enumerable.Repeat("b", 254)),
                new string('/', 254),
                new string('\\', 254)
            };

            var containerClient = new BlobContainerClient(AzureCredentials.ConnectionString, AzureCredentials.DefaultBlobContainerName);

            foreach (var name in names)
            {
                var @lock = new AzureBlobLeaseDistributedLock(containerClient, name);
                Assert.DoesNotThrow(() => @lock.Acquire());
            }
        }
Exemple #2
0
        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());
        }