public void Can_Not_Acquire_Same_Lock_In_Same_Scope()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            try
            {
                // Now test other scenarios while another thread holds the lock.
                using (var testLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(testLock, "Unable to lock the repository");

                    //now that I have the test lock, it should fail if I try to get it again.
                    Assert.Catch <LockTimeoutException>(() =>
                    {
                        using (var failedLock = lockManager.Lock(this, MultiprocessLockName, 0))
                        {
                            Assert.IsNull(failedLock, "Duplicate lock was allowed.");
                        }
                    });
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
        public void LockRepositoryTimeout()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            using (var testLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(testLock, "Unable to lock the repository");

                //now when we try to get it we should not, and should wait at least our timeout
                var             lockStart = DateTimeOffset.Now;
                DistributedLock timeoutLock;
                Assert.IsFalse(lockManager.TryLock(this, MultiprocessLockName, 5, out timeoutLock));
                using (timeoutLock)
                {
                    //we shouldn't have the lock
                    Assert.IsNull(timeoutLock, "Duplicate lock allowed");

                    //and we should be within a reasonable delta of our timeout.
                    var delay = DateTimeOffset.Now - lockStart;
                    Trace.Write(string.Format("Repository Timeout Requested: {0} Actual: {1}", 5, delay.TotalSeconds));
                    Assert.Greater(delay.TotalSeconds, 4.5, "Timeout happened too fast - {0} seconds", delay.TotalSeconds);
                    Assert.Less(delay.TotalSeconds, 5.5, "Timeout happened too slow - {0} seconds", delay.TotalSeconds);
                }
            }
        }
Esempio n. 3
0
        public static OtherThreadLockHelper TryLock(object requester, DistributedLockManager lockManager, string multiprocessLockName, int timeout)
        {
            var helper = new OtherThreadLockHelper(requester, lockManager, multiprocessLockName, timeout);

            if (helper.GetMultiprocessLock())
            {
                return(helper);
            }

            helper.Dispose();
            return(null);
        }
        public void Can_Not_Acquire_Same_Lock_On_Another_Thread()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(outerLock, "Unable to acquire outer lock the repository");

                using (var otherLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
                {
                    Assert.IsNull(otherLock, "Another thread was allowed to get the lock");
                }
            }
        }
        public void Can_Acquire_Different_Lock_In_Same_Scope()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            // Now test other scenarios while another thread holds the lock.
            using (var otherLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName + "_alternate", 0))
            {
                Assert.IsNotNull(otherLock, "Unable to establish first lock in scope.");

                using (var testLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(testLock, "Unable to establish second lock in scope.");
                }
            }
        }
        public void Can_Not_Acquire_Same_Lock_In_Same_Scope()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            // Now test other scenarios while another thread holds the lock.
            using (var testLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(testLock, "Unable to lock the repository");

                //now that I have the test lock, it should fail if I try to get it again.
                Assert.Catch <LockTimeoutException>(() =>
                {
                    using (var failedLock = lockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNull(failedLock, "Duplicate lock was allowed.");
                    }
                });
            }
        }
        public void Can_Not_Aquire_Same_Lock_On_Another_Thread()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            try
            {
                using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    using (var otherLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
                    {
                        Assert.IsNull(otherLock, "Another thread was allowed to get the lock");
                    }
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
        public void LockRepositoryTimeout()
        {
            const string MultiprocessLockName = "LockRepositoryTimeout";

            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            try
            {
                var lockManager = new DistributedLockManager(new FileLockProvider(lockScopePath));

                using (var testLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(testLock, "Unable to lock the repository");

                    //now when we try to get it we should not, and should wait at least our timeout
                    var             lockStart = DateTimeOffset.Now;
                    DistributedLock timeoutLock;
                    Assert.IsFalse(lockManager.TryLock(this, MultiprocessLockName, 5, out timeoutLock));
                    using (timeoutLock)
                    {
                        //we shouldn't have the lock
                        Assert.IsNull(timeoutLock, "Duplicate lock allowed");

                        //and we should be within a reasonable delta of our timeout.
                        var delay = DateTimeOffset.Now - lockStart;
                        Trace.Write(string.Format("Repository Timeout Requested: {0} Actual: {1}", 5, delay.TotalSeconds));
                        Assert.Greater(delay.TotalSeconds, 4.5, "Timeout happened too fast - {0} seconds", delay.TotalSeconds);
                        Assert.Less(delay.TotalSeconds, 5.5, "Timeout happened too slow - {0} seconds", delay.TotalSeconds);
                    }
                }
            }
            finally
            {
                if (Directory.Exists(lockScopePath))
                {
                    Directory.Delete(lockScopePath, true);
                }
            }
        }
        public void Can_Acquire_Different_Lock_In_Same_Scope()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            try
            {
                // Now test other scenarios while another thread holds the lock.
                using (var otherLock = OtherThreadLockHelper.TryLock(this, lockManager, MultiprocessLockName + "_alternate", 0))
                {
                    Assert.IsNotNull(otherLock, "Unable to establish first lock in scope.");

                    using (var testLock = lockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(testLock, "Unable to establish second lock in scope.");
                    }
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }