Пример #1
0
 public void RedisLockAllowsAcquireWithTimeout()
 {
     using (var storage = new RedisStorage(Host))
     {
         var lockKey = Guid.NewGuid().ToString();
         using (var @lock = storage.AcquireLock(lockKey))
         {
             IStorageLock anotherLock;
             var acquireResult =
                 storage.AcquireLock(lockKey, TimeSpan.FromMilliseconds(10), out anotherLock);
             acquireResult.ShouldBe(false);
             anotherLock.IsAcquired.ShouldBe(false);
         }
     }
 }
Пример #2
0
        public void RedisLockAllowsWaitingAndWakingALock()
        {
            using (var storage = new RedisStorage(Host))
            {
                var lockKey = Guid.NewGuid().ToString();
                bool isWaiting = false;
                bool isFinished = false;
                var waitingThread = new Thread(() =>
                {
                    using (var @lock = storage.AcquireLock(lockKey))
                    {
                        isWaiting = true;
                        @lock.Wait();
                    }

                    isFinished = true;
                });
                waitingThread.Start();
                TestHelpers.RepeatUntilTrue(() => isWaiting, times: 5);
                isWaiting.ShouldBe(true);
                isFinished.ShouldBe(false);
                using (var anotherLock = storage.AcquireLock(lockKey))
                {
                    anotherLock.PulseAll();
                }

                TestHelpers.RepeatUntilTrue(() => isFinished, times: 5);
                isFinished.ShouldBe(true);
            }
        }
Пример #3
0
 public void RedisLockAllowsAcquireAfterReleaseTimeout()
 {
     using (var storage = new RedisStorage(Host))
     {
         var lockKey = Guid.NewGuid().ToString();
         var @lock = storage.AcquireLock(lockKey);
         @lock.IsAcquired.ShouldBe(true);
         @lock.Release();
         @lock.IsAcquired.ShouldBe(false);
         @lock.Acquire();
         @lock.IsAcquired.ShouldBe(true);
         @lock.Dispose();
     }
 }
Пример #4
0
        public void RedisLockPulseWakesSingleThread()
        {
            using (var storage = new RedisStorage(Host))
            {
                var lockKey = Guid.NewGuid().ToString();
                bool isWaiting1 = false;
                bool isFinished1 = false;
                var waitingThread1 = new Thread(() =>
                {
                    using (var @lock = storage.AcquireLock(lockKey))
                    {
                        isWaiting1 = true;
                        @lock.Wait();
                    }

                    isFinished1 = true;
                });
                bool isWaiting2 = false;
                bool isFinished2 = false;
                var waitingThread2 = new Thread(() =>
                {
                    using (var @lock = storage.AcquireLock(lockKey))
                    {
                        isWaiting2 = true;
                        @lock.Wait();
                    }

                    isFinished2 = true;
                });
                waitingThread1.Start();
                waitingThread2.Start();
                TestHelpers.RepeatUntilTrue(() => isWaiting1 && isWaiting2, times: 5);
                isWaiting1.ShouldBe(true);
                isWaiting2.ShouldBe(true);
                isFinished1.ShouldBe(false);
                isFinished2.ShouldBe(false);
                using (var anotherLock = storage.AcquireLock(lockKey))
                {
                    anotherLock.Pulse();
                }

                TestHelpers.RepeatUntilTrue(() => ((isFinished1 || isFinished2) && !(isFinished1 && isFinished2)), times: 5);
                ((isFinished1 || isFinished2) && !(isFinished1 && isFinished2)).ShouldBe(true);
                using (var anotherLock = storage.AcquireLock(lockKey))
                {
                    anotherLock.Pulse();
                }

                TestHelpers.RepeatUntilTrue(() => isFinished1 && isFinished2, times: 5);
                isFinished1.ShouldBe(true);
                isFinished2.ShouldBe(true);
            }
        }
Пример #5
0
        public void RedisLockStressTest()
        {
            var lockKey = Guid.NewGuid().ToString();
            int noFinished = 0;
            int threadCount = 100;
            int sleepMiliseconds = 1;
            int shouldFinishModifier = 3;
            List<Thread> threads = new List<Thread>();
            for (int i = 0; i < threadCount; i++)
            {
                var waitingThread = new Thread(() =>
                    {
                        using (var storage = new RedisStorage(Host))
                        {

                            using (var @lock = storage.AcquireLock(lockKey))
                            {
                                noFinished++;
                                Thread.Sleep(sleepMiliseconds);
                            }

                        }
                    });
                waitingThread.Name = string.Format("RedisLockStressTest thread no {0}/{1}", i, threadCount);
                threads.Add(waitingThread);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            var joinThread = new Thread(() =>
            {
                foreach (var thread in threads)
                {
                    thread.Join();
                }
            });
            joinThread.Name = string.Format("RedisLockStressTest join thread");
            joinThread.Start();

            //while (!joinThread.Join(threadCount * sleepMiliseconds * shouldFinishModifier)) ;
            joinThread.Join();
            //joined.ShouldBe(true);
            noFinished.ShouldBe(threadCount);
        }
Пример #6
0
        public void RedisLockLongDurationLockShouldPrevail()
        {
            var lockKey = Guid.NewGuid().ToString();
            int sleepMiliseconds = 5500;
            using (var storage = new RedisStorage(Host))
            {
                using (var @lock = storage.AcquireLock(lockKey))
                {
                    Thread.Sleep(sleepMiliseconds);
                    IStorageLock anotherLock;
                    var isSecondLockTaken = storage.AcquireLock(lockKey, TimeSpan.FromMilliseconds(1000), out anotherLock);
                    isSecondLockTaken.ShouldBe(false);
                }

            }
        }
Пример #7
0
        public void RedisLockIsProperlyReleased()
        {
            using (var storage = new RedisStorage(Host))
            {
                var lockKey = Guid.NewGuid().ToString();
                using (var @lock = storage.AcquireLock(lockKey))
                {
                    @lock.IsAcquired.ShouldBe(true);
                }

                using (var @lock = storage.AcquireLock(lockKey))
                {
                    @lock.IsAcquired.ShouldBe(true);
                }
            }
        }
Пример #8
0
        public void RedisLockCannotBeAcquiredWhenKeyIsLocked()
        {
            using (var storage = new RedisStorage(Host))
            {
                var lockKey = Guid.NewGuid().ToString();
                using (var @lock = storage.AcquireLock(lockKey))
                {
                    bool isAnotherLockAcquired = false;
                    var acquireThread = new System.Threading.Thread(() =>
                    {
                        using (var anotherLock = storage.AcquireLock(lockKey))
                        {
                            isAnotherLockAcquired = true;
                        }
                    });

                    acquireThread.Start();
                    System.Threading.Thread.Sleep(100);

                    isAnotherLockAcquired.ShouldBe(false);
                }
            }
        }
Пример #9
0
 public void RedisLockCannotBeAcquiredTwice()
 {
     using (var storage = new RedisStorage(Host))
     {
         var lockKey = Guid.NewGuid().ToString();
         using (var @lock = storage.AcquireLock(lockKey))
         {
             Assert.That(() => @lock.Acquire(), Throws.Exception);
         }
     }
 }
Пример #10
0
        public void RedisLockAllowsWaitingOnALockWithTimeout()
        {
            using (var storage = new RedisStorage(Host))
            {
                var lockKey = Guid.NewGuid().ToString();
                bool isWaiting = false;
                bool isFinished = false;
                var waitingThread = new Thread(() =>
                {
                    using (var @lock = storage.AcquireLock(lockKey))
                    {
                        isWaiting = true;
                        @lock.Wait(TimeSpan.FromMilliseconds(100));
                    }

                    isFinished = true;
                });
                waitingThread.Start();
                Thread.Sleep(300);
                isWaiting.ShouldBe(true);
                isFinished.ShouldBe(true);
            }
        }