Ejemplo n.º 1
0
        public void TestThrowsIfContainerDoesNotExist()
        {
            using var provider = new TestingAzureBlobLeaseDistributedLockProvider();
            provider.Strategy.ContainerName = "does-not-exist";
            var @lock = provider.CreateLock(nameof(TestThrowsIfContainerDoesNotExist));

            Assert.Throws <RequestFailedException>(() => @lock.TryAcquire()?.Dispose())
            .ErrorCode.ShouldEqual("ContainerNotFound");
        }
Ejemplo n.º 2
0
        public async Task TestSuccessfulRenewal()
        {
            using var provider        = new TestingAzureBlobLeaseDistributedLockProvider();
            provider.Strategy.Options = o => o.RenewalCadence(TimeSpan.FromSeconds(.05));
            var @lock = provider.CreateLock(nameof(TestSuccessfulRenewal));

            using var handle = @lock.Acquire();
            await Task.Delay(TimeSpan.FromSeconds(.2)); // long enough for renewal to run

            Assert.DoesNotThrow(handle.Dispose);        // observes the result of the renewal task
        }
Ejemplo n.º 3
0
        public void TestExitsDespiteLongSleepTime()
        {
            using var provider        = new TestingAzureBlobLeaseDistributedLockProvider();
            provider.Strategy.Options = o => o.BusyWaitSleepTime(TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(1));
            var @lock = provider.CreateLock(nameof(TestExitsDespiteLongSleepTime));

            using var handle1 = @lock.Acquire();

            var handle2Task = @lock.TryAcquireAsync(TimeSpan.FromSeconds(2)).AsTask();

            Assert.IsFalse(handle2Task.Wait(TimeSpan.FromSeconds(.05)));

            handle1.Dispose();
            Assert.IsTrue(handle2Task.Wait(TimeSpan.FromSeconds(5)));
        }
Ejemplo n.º 4
0
        [NonParallelizable, Retry(tryCount: 3)] // timing-sensitive
        public void TestTriggersHandleLostIfLeaseExpiresNaturally()
        {
            using var provider        = new TestingAzureBlobLeaseDistributedLockProvider();
            provider.Strategy.Options = o => o.RenewalCadence(Timeout.InfiniteTimeSpan).Duration(TimeSpan.FromSeconds(15));
            var @lock = provider.CreateLock(nameof(TestTriggersHandleLostIfLeaseExpiresNaturally));

            using var handle               = @lock.Acquire();
            using var @event               = new ManualResetEventSlim(initialState: false);
            using var registration         = handle.HandleLostToken.Register(@event.Set);
            using var faultingRegistration = handle.HandleLostToken.Register(() => throw new TimeZoneNotFoundException());

            Assert.IsTrue(@event.Wait(TimeSpan.FromSeconds(15.1)));

            Assert.Throws <RequestFailedException>(handle.Dispose)
            .ErrorCode.ShouldEqual("LeaseLost");
        }
Ejemplo n.º 5
0
        public void TestCanAcquireIfContainerLeased()
        {
            using var provider = new TestingAzureBlobLeaseDistributedLockProvider();
            provider.Strategy.ContainerName = "leased-container" + TargetFramework.Current.Replace('.', '-');

            var containerClient      = new BlobContainerClient(AzureCredentials.ConnectionString, provider.Strategy.ContainerName);
            var containerLeaseClient = new BlobLeaseClient(containerClient);

            try
            {
                containerClient.CreateIfNotExists();
                containerLeaseClient.Acquire(TimeSpan.FromSeconds(60));

                var @lock = provider.CreateLock(nameof(TestCanAcquireIfContainerLeased));

                using var handle = @lock.TryAcquire();
                Assert.IsNotNull(handle);
            }
            finally
            {
                try { containerLeaseClient.Release(); }
                finally { containerClient.DeleteIfExists(); }
            }
        }