public void NoQuorumDoesNotObtainLock()
        {
            var l = Redlock.TryLock("r", "n", TimeSpan.FromSeconds(1), _noQuorum, _log);

            Assert.Null(l);
            Assert.NotEmpty(_log.Logs.Where(x => x.Exception != null && x.LogLevel == LogLevel.Error));
        }
 public void Sequential()
 {
     for (var i = 0; i < 10; i++)
     {
         var l = Redlock.TryLock("r", i.ToString(), TimeSpan.FromSeconds(10), _5Inst, _log);
         Assert.NotNull(l);
         l !.Value.Dispose();
     }
 }
        public void TestWhenLockedAnotherLockRequestIsRejected()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var locked = dlm.Lock(ResourceName, new TimeSpan(0, 0, 10), out lockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            locked = dlm.Lock(ResourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsFalse(locked, "lock taken, it shouldn't be possible");
            dlm.Unlock(lockObject);
        }
        public void TestThatSequenceLockedUnlockedAndLockedAgainIsSuccessfull()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var locked = dlm.Lock(ResourceName, new TimeSpan(0, 0, 10), out lockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(lockObject);
            locked = dlm.Lock(ResourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(newLockObject);
        }
Esempio n. 5
0
        public void TestWhenLockedAnotherLockRequestIsRejected()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var  locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);

            Assert.IsTrue(locked, "Unable to get lock");
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsFalse(locked, "lock taken, it shouldn't be possible");
            dlm.Unlock(lockObject);
        }
Esempio n. 6
0
        public void TestThatSequenceLockedUnlockedAndLockedAgainIsSuccessfull()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var  locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);

            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(lockObject);
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(newLockObject);
        }
        public void MultiThread_NoOverlaps()
        {
            const string resource     = nameof(MultiThread_NoOverlaps);
            var          threads      = new Thread[16];
            var          threadWaitMs = 100;

            using var cts = new CancellationTokenSource(threads.Length * threadWaitMs + 75000);
            var repeater   = new CancellationRedlockRepeater(cts.Token);
            var locksCount = 0;
            var exceptions = new ConcurrentBag <Exception>();

            for (var i = 0; i < threads.Length; i++)
            {
                var threadId = i;
                threads[i] = new Thread(() =>
                {
                    try
                    {
                        var nonce = threadId.ToString();
                        var ttl   = TimeSpan.FromSeconds(10);

                        using var l = Redlock.Lock(resource, nonce, ttl, _5Inst, _log, repeater, 50);
                        Assert.Equal(0, locksCount);
                        Interlocked.Increment(ref locksCount);
                        Thread.Sleep(threadWaitMs);
                        Assert.Equal(1, locksCount);
                        Interlocked.Decrement(ref locksCount);
                    }
                    catch (Exception e)
                    {
                        exceptions.Add(e);
                    }
                });
                threads[i].Start();
            }

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

            Assert.Empty(exceptions);
        }
Esempio n. 8
0
        public void TestWhenLockedAnotherLockRequestIsRejected()
        {
            var configBuilder = new ConfigurationBuilder();

            configBuilder
            .AddJsonFile("app.json", true);
            var configRoot = configBuilder.Build();

            var dlm = new Redlock(ConnectionMultiplexer.Connect(configRoot[ServerA_Key]));

            Lock lockObject;
            Lock newLockObject;
            var  locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);

            Assert.IsTrue(locked, "Unable to get lock");
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsFalse(locked, "lock taken, it shouldn't be possible");
            dlm.Unlock(lockObject);
        }
Esempio n. 9
0
        public void TestThatSequenceLockedUnlockedAndLockedAgainIsSuccessfull()
        {
            var configBuilder = new ConfigurationBuilder();

            configBuilder
            .AddJsonFile("app.json", true);
            var configRoot = configBuilder.Build();

            var dlm = new Redlock(ConnectionMultiplexer.Connect(configRoot[ServerA_Key]));

            Lock lockObject;
            Lock newLockObject;
            var  locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);

            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(lockObject);
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(newLockObject);
        }
 private static void LockExec(Action work, Redlock redLock, Lock lockObject, bool locked)
 {
     try
     {
         if (locked)
         {
             work();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         if (locked)
         {
             redLock.Unlock(lockObject);
         }
     }
 }
 public void IntegrationTest()
 {
     using var l = Redlock.Lock("r", "n", TimeSpan.FromSeconds(10), _5Inst, _log, NoopRedlockRepeater.Instance, 50);
 }
 public void TimeoutExpires_ThenNewLockCanBeObtained()
 {
     using var l1 = Redlock.Lock("r", "n1", TimeSpan.FromSeconds(0.5), _5Inst, _log, NoopRedlockRepeater.Instance, 50);
     Thread.Sleep(800);
     using var l2 = Redlock.Lock("r", "n2", TimeSpan.FromSeconds(1), _5Inst, _log, NoopRedlockRepeater.Instance, 50);
 }
Esempio n. 13
0
 private readonly Redlock _redlock; //分布式锁
 public ProcessMessageLockHandler(Redlock redlock)
 {
     _redlock = redlock;
 }
        private static void GetLockObjetc(string resource, TimeSpan expiryTime, TimeSpan waitTime, Redlock redLock, out Lock lockObject, out bool locked)
        {
            locked = redLock.Lock(resource, expiryTime, out lockObject);
            if (waitTime.Seconds == 0)
            {
                return;
            }
            int waitSecond = waitTime.Seconds;

            while (!locked && waitSecond > 1)
            {
                locked = redLock.Lock(resource, expiryTime, out lockObject);
                if (locked)
                {
                    break;
                }
                #region 超时
                Thread.Sleep(1000);
                waitSecond--;
                #endregion
            }
        }