public void Can_AcquireLock_TimeOut() { Redis.IncrementValue("key"); //1 var acquiredLock = Redis.AcquireLock("testlock"); var waitFor = TimeSpan.FromMilliseconds(1000); var now = DateTime.Now; try { using (var client = new RedisClient(TestConfig.SingleHost)) { using (client.AcquireLock("testlock", waitFor)) { Redis.IncrementValue("key"); //2 } } } catch (TimeoutException tex) { var val = Redis.Get <int>("key"); Assert.That(val, Is.EqualTo(1)); var timeTaken = DateTime.Now - now; Assert.That(timeTaken.TotalMilliseconds > waitFor.TotalMilliseconds, Is.True); Assert.That(timeTaken.TotalMilliseconds < waitFor.TotalMilliseconds + 1000, Is.True); return; } Assert.Fail("should have Timed out"); }
public void Can_AcquireLock_TimeOut() { // guid here is to prevent competition between concurrent runtime tests var key = PrefixedKey("AcquireLockKeyTimeOut:" + Guid.NewGuid()); var lockKey = PrefixedKey("Can_AcquireLock_TimeOut:" + Guid.NewGuid()); Redis.IncrementValue(key); //1 var acquiredLock = Redis.AcquireLock(lockKey); var waitFor = TimeSpan.FromMilliseconds(1000); var now = DateTime.Now; try { using (var client = new RedisClient(TestConfig.SingleHost)) { using (client.AcquireLock(lockKey, waitFor)) { Redis.IncrementValue(key); //2 } } } catch (TimeoutException) { var val = Redis.Get <int>(key); Assert.That(val, Is.EqualTo(1)); var timeTaken = DateTime.Now - now; Assert.That(timeTaken.TotalMilliseconds > waitFor.TotalMilliseconds, Is.True); Assert.That(timeTaken.TotalMilliseconds < waitFor.TotalMilliseconds + 3000, Is.True); return; } finally { Redis.Remove(key); Redis.Remove(lockKey); } Assert.Fail("should have Timed out"); }