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

            try
            {
                // First test new re-entrant lock capability.
                using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(outerLock, "Unable to outer lock the repository");

                    // Now check that we can get the same lock on the same thread.
                    using (var middleLock = lockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(middleLock, "Unable to reenter the repository lock on the same thread");

                        using (var innerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                        {
                            Assert.IsNotNull(innerLock, "Unable to reenter the repository lock on the same thread twice");
                        }
                    }
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
コード例 #2
0
        public async Task Can_Acquire_Lock_Many_Times_Async()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            var lockIterations = 1000;

            for (var curIteration = 0; curIteration < lockIterations; curIteration++)
            {
                try
                {
                    var outerLock = lockManager.Lock(this, MultiprocessLockName, 0);

                    try
                    {
                        Assert.IsNotNull(outerLock, "Unable to acquire lock on iteration {0:N0}", curIteration);

                        //now we need to do something else async so we resume back.
                        await GratuitousWorkAsync(outerLock).ConfigureAwait(false);
                    }
                    finally
                    {
                        outerLock.Dispose();
                    }
                }
                catch (LockTimeoutException ex)
                {
                    throw new Exception("Unable to acquire the lock immediately on iteration " + curIteration, ex);
                }
            }
        }
        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 Can_Acquire_Same_Lock_In_Different_Scope()
        {
            var firstTestRepositoryPath  = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var secondTestRepositoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var thirdTestRepositoryPath  = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var fourthTestRepositoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            var firstLockManager = new DistributedLockManager(new FileLockProvider(firstTestRepositoryPath));

            try
            {
                // Now test other scenarios while another thread holds the lock.
                using (var testLock = firstLockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(testLock, "Unable to establish lock on first scope");

                    var secondLockManager = new DistributedLockManager(new FileLockProvider(secondTestRepositoryPath));
                    using (var secondTestLock = secondLockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(secondTestLock, "Unable to establish lock on second scope.");

                        var thirdLockManager = new DistributedLockManager(new FileLockProvider(thirdTestRepositoryPath));
                        using (var thirdTestLock = thirdLockManager.Lock(this, MultiprocessLockName, 0))
                        {
                            Assert.IsNotNull(thirdTestLock, "Unable to establish lock on third scope.");

                            var forthLockManager = new DistributedLockManager(new FileLockProvider(fourthTestRepositoryPath));
                            using (var fourthTestLock = forthLockManager.Lock(this, MultiprocessLockName, 0))
                            {
                                Assert.IsNotNull(fourthTestLock, "Unable to establish lock on fourth scope.");
                            }
                        }
                    }
                }
            }
            finally
            {
                //and clean up after ourselves.
                if (Directory.Exists(firstTestRepositoryPath))
                {
                    Directory.Delete(firstTestRepositoryPath, true);
                }

                if (Directory.Exists(secondTestRepositoryPath))
                {
                    Directory.Delete(secondTestRepositoryPath, true);
                }

                if (Directory.Exists(thirdTestRepositoryPath))
                {
                    Directory.Delete(thirdTestRepositoryPath, true);
                }

                if (Directory.Exists(fourthTestRepositoryPath))
                {
                    Directory.Delete(fourthTestRepositoryPath, true);
                }
            }
        }
コード例 #5
0
        public void Can_Acquire_Lock()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(outerLock, "Unable to acquire lock");
            }
        }
コード例 #6
0
        public void Can_Acquire_Lock_With_Unsafe_Name()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            var unsafeLockName = "\"M<>\"\\a/ry/ h**ad:>> a\\/:*?\"<>| li*tt|le|| la\"mb.?";

            using (var outerLock = lockManager.Lock(this, unsafeLockName, 0))
            {
                Assert.IsNotNull(outerLock, "Unable to acquire the lock");
            }
        }
コード例 #7
0
        public void Can_ReEnter_Lock_On_Same_Thread()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            // First test new re-entrant lock capability.
            using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(outerLock, "Unable to outer lock the repository");

                // Now check that we can get the same lock on the same thread.
                using (var middleLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(middleLock, "Unable to reenter the repository lock on the same thread");

                    using (var innerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(innerLock, "Unable to reenter the repository lock on the same thread twice");
                    }
                }
            }
        }
コード例 #8
0
        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");
                }
            }
        }
コード例 #9
0
        public void Can_Acquire_Lock_Many_Times()
        {
            var lockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            var lockIterations = 1000;

            for (var curIteration = 0; curIteration < lockIterations; curIteration++)
            {
                using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(outerLock, "Unable to acquire lock on iteration {0:N0}", curIteration);
                }
            }
        }
コード例 #10
0
        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_Acquire_Lock()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            try
            {
                using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(outerLock, "Unable to acquire the lock");
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
コード例 #12
0
        private void GenerateLocks(CancellationTokenSource cancellationTokenSource)
        {
            try
            {
                while (cancellationTokenSource.IsCancellationRequested == false)
                {
                    var lockTimespan = new TimeSpan(GetRandomNumber(0, (int)_maxLockDuration.Ticks));
                    var nameIndex    = GetRandomNumber(1, _maxLockNumber).ToString();
                    var name         = _lockPrefix + "-" + nameIndex;
                    try
                    {
                        var stopwatch = Stopwatch.StartNew();
                        using (var newLock = _lockManager.Lock(this, name, (int)_lockTimeout.TotalSeconds))
                        {
                            stopwatch.Stop();

#if DEBUG
                            System.Console.WriteLine("{4} Thread {1} - Acquired lock {0} in {2:N0}ms, will hold for {3:N0}ms",
                                                     newLock.Name, Thread.CurrentThread.ManagedThreadId, stopwatch.ElapsedMilliseconds, lockTimespan.TotalMilliseconds, DateTime.Now);
#endif

                            Thread.Sleep(lockTimespan);

                            //Task.Delay(lockTimespan, cancellationTokenSource.Token).Wait(cancellationTokenSource.Token);
#if DEBUG
                            System.Console.WriteLine("{2} Thread {1} - Releasing lock {0}", newLock.Name, Thread.CurrentThread.ManagedThreadId, DateTime.Now);
#endif
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.ForegroundColor = ConsoleColor.Red;
                        System.Console.WriteLine("{3} Thread {1} - Unable to acquire lock {0} due to {2}",
                                                 name, Thread.CurrentThread.ManagedThreadId, ex.GetBaseException().GetType(), DateTime.Now);
                        System.Console.ForegroundColor = _defaultForeground;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.ForegroundColor = ConsoleColor.Red;
                System.Console.WriteLine("{3} Thread {1} - Locking task failed due to {0}:\r\n{2}",
                                         ex.GetBaseException().GetType(), Thread.CurrentThread.ManagedThreadId, ex.StackTrace, DateTime.Now);
                System.Console.ForegroundColor = _defaultForeground;
            }
        }
コード例 #13
0
        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_Acquire_Lock_With_Unsafe_Name()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            try
            {
                var unsafeLockName = "\"M<>\"\\a/ry/ h**ad:>> a\\/:*?\"<>| li*tt|le|| la\"mb.?";

                using (var outerLock = lockManager.Lock(this, unsafeLockName, 0))
                {
                    Assert.IsNotNull(outerLock, "Unable to acquire the lock");
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
        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 Can_Acquire_Lock_Many_Times()
        {
            var lockScopePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var lockManager   = new DistributedLockManager(new FileLockProvider(lockScopePath));

            var lockIterations = 1000;

            try
            {
                for (var curIteration = 0; curIteration < lockIterations; curIteration++)
                {
                    using (var outerLock = lockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(outerLock, "Unable to acquire lock");
                    }
                }
            }
            finally
            {
                Directory.Delete(lockScopePath);
            }
        }
コード例 #17
0
        public void Can_Acquire_Same_Lock_In_Different_Scope()
        {
            var firstLockManager = new DistributedLockManager(GetLockProvider(DefaultLockDatabase));

            // Now test other scenarios while another thread holds the lock.
            using (var testLock = firstLockManager.Lock(this, MultiprocessLockName, 0))
            {
                Assert.IsNotNull(testLock, "Unable to establish lock on first scope");

                var secondLockManager = new DistributedLockManager(GetLockProvider(SecondLockDatabase));
                using (var secondTestLock = secondLockManager.Lock(this, MultiprocessLockName, 0))
                {
                    Assert.IsNotNull(secondTestLock, "Unable to establish lock on second scope.");

                    var thirdLockManager = new DistributedLockManager(GetLockProvider(ThirdLockDatabase));
                    using (var thirdTestLock = thirdLockManager.Lock(this, MultiprocessLockName, 0))
                    {
                        Assert.IsNotNull(thirdTestLock, "Unable to establish lock on third scope.");
                    }
                }
            }
        }
        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);
            }
        }