コード例 #1
0
        public void AcquireWait()
        {
            var lockName = "AcquireWait" + DateTime.Now.Ticks;

            var collection = GetCollection();

            collection.Should().NotBeNull();

            var locker = new DistributedLock(collection);

            locker.Should().NotBeNull();

            var result = locker.Acquire(lockName, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            result.Should().BeTrue();

            var status = locker.Status(lockName);

            status.Should().NotBeNull();
            status.IsLocked.Should().BeTrue();

            var waited = locker.Acquire(lockName, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            waited.Should().BeTrue();
        }
コード例 #2
0
        public void AcquireExpire()
        {
            var lockName = "AcquireExpire" + DateTime.Now.Ticks;

            var collection = GetCollection();

            collection.Should().NotBeNull();

            var locker = new DistributedLock(collection);

            locker.Should().NotBeNull();

            var result = locker.Acquire(lockName, TimeSpan.FromMilliseconds(5));

            result.Should().BeTrue();

            var status = locker.Status(lockName);

            status.Should().NotBeNull();
            status.IsLocked.Should().BeTrue();

            // wait for expire
            Thread.Sleep(5);

            var blocked = locker.Acquire(lockName, TimeSpan.FromMilliseconds(5));

            blocked.Should().BeTrue();
        }
コード例 #3
0
        public void AcquireBlock()
        {
            var lockName = "AcquireBlock" + DateTime.Now.Ticks;

            var collection = GetCollection();

            collection.Should().NotBeNull();

            var locker = new DistributedLock(collection, TimeSpan.FromMinutes(5));

            locker.Should().NotBeNull();

            var result = locker.Acquire(lockName);

            result.Should().BeTrue();

            var status = locker.Status(lockName);

            status.Should().NotBeNull();
            status.IsLocked.Should().BeTrue();

            var blocked = locker.Acquire(lockName);

            blocked.Should().BeFalse();
        }