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); }
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); }
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); }
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); }
/// <summary> /// 锁定方法 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="method">方法信息</param> /// <param name="lockobj">redlock所需的lockobj</param> /// <returns>false表示锁定失败</returns> private bool LockMethod <T>(MethodInfo method, out Lock lockobj) where T : Attribute { lockobj = null; Attribute attrib = method.GetCustomAttribute(typeof(T)); if (attrib != null) { var keyname = attrib.GetType().GetProperty("keyname").GetValue(attrib).ToString(); var exptime = (int)attrib.GetType().GetProperty("exptime").GetValue(attrib); return(_redlock != null?_redlock.Lock(keyname, new TimeSpan(0, 0, exptime), out lockobj) : false); } return(true); }
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 } }
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); }
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); }