Exemple #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")));
        }
Exemple #2
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);
        }
 /// <inheritdoc />
 public Task <Redlock> CreateAsync <T>(string resource, TimeSpan lockTimeToLive, T repeater, int maxWaitMs, IReadOnlyDictionary <string, string>?meta = null)
     where T : IRedlockRepeater
 {
     return(Redlock.LockAsync(
                resource,
                Nonce(resource, lockTimeToLive),
                lockTimeToLive,
                _impl.Instances,
                _logger,
                repeater,
                maxWaitMs,
                _opt.Value.UtcNow,
                meta
                ));
 }