Beispiel #1
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);
        }
Beispiel #2
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")));
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public void TryLockWithRepeater_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 = Redlock.TryLock("r", "n", Ttl, mem.ToInstances(), _log, repeater.Object, 600);

            Assert.Null(l);
        }
Beispiel #5
0
        public void TryLock_NoQuorum()
        {
            var instances = MemInstances(3);

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

            var l = Redlock.TryLock("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"));
        }
Beispiel #6
0
        public void TryLock_ExceptionsOnLock_Quorum()
        {
            var mem = MemInstances(3);
            var err = ErrInstances(2);

            var l = Redlock.TryLock("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].TryLockException);
            Assert.Contains(errorLogs, e => e.Exception == err[1].TryLockException);
        }
Beispiel #7
0
 /// <inheritdoc />
 public Redlock?TryCreate <T>(string resource, TimeSpan lockTimeToLive, T repeater, IReadOnlyDictionary <string, string>?meta, int maxWaitMs)
     where T : IRedlockRepeater
 {
     return(Redlock.TryLock(
                resource,
                Nonce(resource, lockTimeToLive),
                lockTimeToLive,
                _impl.Instances,
                _logger,
                repeater,
                maxWaitMs,
                _opt.Value.UtcNow,
                meta
                ));
 }
Beispiel #8
0
 /// <inheritdoc />
 public Redlock?TryCreate(string resource, TimeSpan lockTimeToLive, IReadOnlyDictionary <string, string>?meta)
 {
     return(Redlock.TryLock(resource, Nonce(resource, lockTimeToLive), lockTimeToLive, _impl.Instances, _logger,
                            _opt.Value.UtcNow, meta));
 }