示例#1
0
        public async Task LockWithRepeater()
        {
            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.WaitRandom(600)).Callback(() =>
            {
                waitInvoked.Set();
                Assert.True(waitAre.WaitOne(2000));
            });
            repeater.Setup(x => x.Next()).Returns(true);
            var lockTask = Task.Run(() =>
                                    Redlock.Lock("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")));
        }
示例#2
0
        public void LockWithRepeater_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 = Assert.Throws <Exception>(() => Redlock.Lock("r", "n", Ttl, mem.ToInstances(), _log, repeater.Object, 600));

            Assert.Same(expected, actual);
        }
示例#3
0
 /// <inheritdoc />
 public Redlock Create <T>(string resource, TimeSpan lockTimeToLive, T repeater, int maxWaitMs, IReadOnlyDictionary <string, string>?meta = null)
     where T : IRedlockRepeater
 {
     return(Redlock.Lock(
                resource,
                Nonce(resource, lockTimeToLive),
                lockTimeToLive,
                _impl.Instances,
                _logger,
                repeater,
                maxWaitMs,
                _opt.Value.UtcNow,
                meta
                ));
 }