Example #1
0
        public async Task LockWithRepeaterAsync()
        {
            using var waitAre     = new AutoResetEvent(false);
            using var waitInvoked = new AutoResetEvent(false);
            var mem = MemInstances(3);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.WaitRandomAsync(600, CancellationToken.None))
            .Returns(() =>
            {
                waitInvoked.Set();
                Assert.True(waitAre.WaitOne(2000));
                return(new ValueTask());
            });
            repeater.Setup(x => x.Next()).Returns(true);
            var lockTask = Task.Run(() => Redlock.LockAsync("r", "n", Ttl, mem.ToInstances(), _log, repeater.Object, 600));

            Assert.True(waitInvoked.WaitOne(2000));
            repeater.Verify(x => x.Next(), Times.Once);
            Unlock("r", mem);
            waitAre.Set();
            await lockTask;

            Assert.All(mem, i => Assert.True(i.Contains("r", "n")));
        }
Example #2
0
        public void TryLock_EmptyInstances()
        {
            var now = new DateTime(2020, 7, 21, 13, 00, 00, DateTimeKind.Utc);
            var l   = Redlock.TryLock("r", "n", Ttl, _emptyInstances, _log, () => now);

            Assert.Null(l);
        }
Example #3
0
        public async Task TryLockAsync_EmptyInstances()
        {
            var now = new DateTime(2020, 7, 21, 13, 00, 00, DateTimeKind.Utc);
            var l   = await Redlock.TryLockAsync("r", "n", Ttl, _emptyInstances, _log, () => now);

            Assert.Null(l);
        }
Example #4
0
        public async Task ExtendWithRepeaterAsync()
        {
            using var waitAre     = new AutoResetEvent(false);
            using var waitInvoked = new AutoResetEvent(false);
            var mem = MemInstances(3, (ttl, duration) => ttl);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.WaitRandomAsync(600, default)).Returns(new ValueTask()).Callback(() =>
            {
                waitInvoked.Set();
                Assert.True(waitAre.WaitOne(2000));
            });
            repeater.Setup(x => x.Next()).Returns(true);
            var task = Task.Run(async() =>
            {
                var l = new Redlock("r", "n", Ttl, _now, mem.ToInstances(), _log);
                return(await l.ExtendAsync(repeater.Object, 600, () => _now));
            });

            Assert.True(waitInvoked.WaitOne(2000));
            repeater.Verify(x => x.Next(), Times.Once);
            Unlock("r", mem);
            Lock("r", "n", mem);
            waitAre.Set();
            var actualValidUntil = await task;

            Assert.All(mem, i => Assert.True(i.Contains("r", "n")));
            Assert.Equal(_now.Add(Ttl), actualValidUntil);
        }
Example #5
0
        public async Task TryLockAsync()
        {
            var instances = MemInstances(3);

            var @lock = await Redlock.TryLockAsync("r", "n", Ttl, instances.ToInstances(), _log);

            Assert.NotNull(@lock);
            Assert.All(instances, i => Assert.True(i.Contains("r", "n")));
        }
Example #6
0
        public void TryLock()
        {
            var instances = MemInstances(3);

            var @lock = Redlock.TryLock("r", "n", Ttl, instances.ToInstances(), _log);

            Assert.NotNull(@lock);
            Assert.All(instances, i => Assert.True(i.Contains("r", "n")));
        }
Example #7
0
        public void TryLock_ValidUntil()
        {
            var mem  = MemInstances(3, (ttl, duration) => TimeSpan.FromSeconds(10));
            var impl = mem.ToInstances();
            var now  = new DateTime(2020, 7, 21, 13, 00, 00, DateTimeKind.Utc);
            var l    = Redlock.TryLock("r", "n", Ttl, impl, _log, () => now);

            Assert.NotNull(l);
            Assert.Equal(new DateTime(2020, 7, 21, 13, 00, 10, DateTimeKind.Utc), l !.Value.ValidUntilUtc);
        }
Example #8
0
        public async Task DisposeAsync()
        {
            var instances = MemInstances(3);
            var l         = new Redlock("r", "n", Ttl, _now, instances.ToInstances(), _log);

            Lock("r", "n", instances);

            await l.DisposeAsync();

            Assert.All(instances, i => Assert.False(i.Contains("r", "n")));
        }
Example #9
0
        public void DisposeTest()
        {
            var instances = MemInstances(3);
            var l         = new Redlock("r", "n", Ttl, _now, instances.ToInstances(), _log);

            Lock("r", "n", instances);

            l.Dispose();

            Assert.All(instances, i => Assert.False(i.Contains("r", "n")));
        }
Example #10
0
        public void TryExtend()
        {
            var instances = MemInstances(3, (ttl, duration) => ttl);
            var impl      = instances.ToInstances();

            Lock("r", "n", instances);
            var l = new Redlock("r", "n", Ttl, _now.AddDays(10), impl, _log);
            var actualValidUntil = l.TryExtend(() => _now);

            Assert.Equal(_now.Add(Ttl), actualValidUntil);
            Assert.All(instances, i => Assert.True(i.Contains("r", "n")));
        }
Example #11
0
        public async Task TryLockWithRepeaterAsync_UnableToObtainLock()
        {
            var mem = MemInstances(3);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.Next()).Returns(false);
            var l = await Redlock.TryLockAsync("r", "n", Ttl, mem.ToInstances(), _log, repeater.Object, 600);

            Assert.Null(l);
        }
Example #12
0
 /// <inheritdoc />
 public Task <Redlock?> TryCreateAsync(string resource, TimeSpan lockTimeToLive, IReadOnlyDictionary <string, string>?meta = null)
 {
     return(Redlock.TryLockAsync(
                resource,
                Nonce(resource, lockTimeToLive),
                lockTimeToLive,
                _impl.Instances,
                _logger,
                _opt.Value.UtcNow,
                meta
                ));
 }
Example #13
0
        public async Task TryExtendAsync()
        {
            var instances = MemInstances(3, (ttl, duration) => ttl);
            var impl      = instances.ToInstances();

            Lock("r", "n", TimeSpan.FromSeconds(10), instances);
            var l = new Redlock("r", "n", Ttl, _now.AddDays(10), impl, _log);

            var actualValidUntil = await l.TryExtendAsync(() => _now);

            Assert.Equal(_now.Add(Ttl), actualValidUntil);
            Assert.All(instances, i => Assert.True(i.Contains("r", "n")));
        }
Example #14
0
        public async Task TryExtendWithRepeaterAsync_UnableToObtainLock()
        {
            var mem = MemInstances(3, (ttl, duration) => ttl);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.Next()).Returns(false);
            var l = new Redlock("r", "n", Ttl, _now, mem.ToInstances(), _log);
            var actualValidUntil = await l.TryExtendAsync(repeater.Object, 600);

            Assert.Null(actualValidUntil);
        }
Example #15
0
        public async Task TryLockAsync_NoQuorum()
        {
            var instances = MemInstances(3);

            Lock("r", "n2", instances[0], instances[1]);

            var l = await Redlock.TryLockAsync("r", "n", Ttl, instances.ToInstances(), _log);

            Assert.Null(l);
            Assert.All(instances, i => Assert.False(i.Contains("r", "n")));
            Assert.True(instances[0].Contains("r", "n2"));
            Assert.True(instances[1].Contains("r", "n2"));
            Assert.False(instances[2].Contains("r", "n2"));
        }
Example #16
0
        public async Task TryExtendAsync_NoQuorum_Errors()
        {
            var err = ErrInstances(3);
            var l   = new Redlock("r", "n", Ttl, _now, err.ToInstances(), _log);
            var actualValidUntil = await l.TryExtendAsync(() => _now);

            Assert.Null(actualValidUntil);
            var errorLogs = _log.Logs.Where(x => x.LogLevel == LogLevel.Error).ToArray();

            Assert.Equal(3, errorLogs.Length);
            Assert.Contains(errorLogs, e => e.Exception == err[0].TryExtendAsyncException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].TryExtendAsyncException);
            Assert.Contains(errorLogs, e => e.Exception == err[2].TryExtendAsyncException);
        }
Example #17
0
        public async Task TryLockAsync_ExceptionsOnLock_Quorum()
        {
            var mem = MemInstances(3);
            var err = ErrInstances(2);

            var l = await Redlock.TryLockAsync("r", "n", Ttl, TestRedlock.Instances(mem, err), _log);

            Assert.NotNull(l);
            Assert.All(mem, i => i.Contains("r", "n"));
            var errorLogs = _log.Logs.Where(x => x.LogLevel == LogLevel.Error).ToArray();

            Assert.Equal(2, errorLogs.Length);
            Assert.Contains(errorLogs, e => e.Exception == err[0].TryLockAsyncException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].TryLockAsyncException);
        }
Example #18
0
        public async Task LockWithRepeaterAsync_UnableToObtainLock()
        {
            var mem = MemInstances(3);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.Next()).Returns(false);
            var expected = new Exception();

            repeater.Setup(x => x.CreateException("r", "n", 1)).Returns(expected);
            var actual = await Assert.ThrowsAsync <Exception>(() => Redlock.LockAsync("r", "n", Ttl, mem.ToInstances(), _log, repeater.Object, 600));

            Assert.Same(expected, actual);
        }
Example #19
0
 /// <inheritdoc />
 public Task <Redlock?> TryCreateAsync <T>(string resource, TimeSpan lockTimeToLive, T repeater, int maxWaitMs, IReadOnlyDictionary <string, string>?meta = null)
     where T : IRedlockRepeater
 {
     return(Redlock.TryLockAsync(
                resource,
                Nonce(resource, lockTimeToLive),
                lockTimeToLive,
                _impl.Instances,
                _logger,
                repeater,
                maxWaitMs,
                _opt.Value.UtcNow,
                meta
                ));
 }
Example #20
0
        public void ExtendWithRepeater_UnableToObtainLock()
        {
            var mem = MemInstances(3, (ttl, duration) => ttl);

            Lock("r", "n2", mem);
            var repeater = new Mock <IRedlockRepeater>(MockBehavior.Strict);

            repeater.Setup(x => x.Next()).Returns(false);
            var expected = new Exception();

            repeater.Setup(x => x.CreateException("r", "n", 1)).Returns(expected);
            var l      = new Redlock("r", "n", Ttl, _now, mem.ToInstances(), _log);
            var actual = Assert.Throws <Exception>(() => l.Extend(repeater.Object, 600));

            Assert.Same(expected, actual);
        }
Example #21
0
        public async Task DisposeAsync_ExceptionsOnUnlock()
        {
            var mem = MemInstances(3);
            var err = ErrInstances(2);
            var l   = new Redlock("r", "n", Ttl, _now, TestRedlock.Instances(mem, err), _log);

            Lock("r", "n", mem);

            await l.DisposeAsync();

            Assert.All(mem, i => Assert.False(i.Contains("r", "n")));
            var errorLogs = _log.Logs.Where(x => x.LogLevel == LogLevel.Error).ToArray();

            Assert.Equal(2, errorLogs.Length);
            Assert.Contains(errorLogs, e => e.Exception == err[0].UnlockAsyncException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].UnlockAsyncException);
        }
Example #22
0
        public async Task TryExtendAsync_Quorum_Errors()
        {
            var err = ErrInstances(2, (ttl, duration) => ttl);
            var mem = MemInstances(3, (ttl, duration) => ttl);

            Lock("r", "n", mem);
            var l = new Redlock("r", "n", Ttl, _now, TestRedlock.Instances(mem, err), _log);
            var actualValidUntil = await l.TryExtendAsync(() => _now);

            Assert.NotNull(actualValidUntil);
            Assert.Equal(_now.Add(Ttl), actualValidUntil);
            var errorLogs = _log.Logs.Where(x => x.LogLevel == LogLevel.Error).ToArray();

            Assert.Equal(2, errorLogs.Length);
            Assert.Contains(errorLogs, e => e.Exception == err[0].TryExtendAsyncException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].TryExtendAsyncException);
        }
Example #23
0
        public void TryLock_ExceptionsOnLock_NoQuorum()
        {
            var mem = MemInstances(2);
            var err = ErrInstances(3);

            var l = Redlock.TryLock("r", "n", Ttl, TestRedlock.Instances(mem, err), _log);

            Assert.Null(l);
            Assert.All(mem, i => Assert.False(i.Contains("r", "n")));
            var errorLogs = _log.Logs.Where(x => x.LogLevel == LogLevel.Error).ToArray();

            Assert.Equal(6, errorLogs.Length);
            Assert.Contains(errorLogs, e => e.Exception == err[0].TryLockException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].TryLockException);
            Assert.Contains(errorLogs, e => e.Exception == err[2].TryLockException);
            Assert.Contains(errorLogs, e => e.Exception == err[0].UnlockException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].UnlockException);
            Assert.Contains(errorLogs, e => e.Exception == err[2].UnlockException);
        }
Example #24
0
        public async Task TryExtendAsync_EmptyInstances()
        {
            var l = new Redlock("r", "n", Ttl, _now, _emptyInstances, _log);

            Assert.Null(await l.TryExtendAsync());
        }
Example #25
0
        public void TryExtend_EmptyInstances()
        {
            var l = new Redlock("r", "n", Ttl, _now, _emptyInstances, _log);

            Assert.Null(l.TryExtend());
        }