public static void NextTest_Canceled() { var r = new CancellationRedlockRepeater(new CancellationToken(true)); Assert.False(r.Next()); Assert.False(r.Next()); Assert.False(r.Next()); }
public static void NextTest_NoCancel() { var r = new CancellationRedlockRepeater(CancellationToken.None); Assert.True(r.Next()); Assert.True(r.Next()); Assert.True(r.Next()); }
public static void NextTest_ChangedCancellation() { using var cts = new CancellationTokenSource(); var r = new CancellationRedlockRepeater(cts.Token); Assert.True(r.Next()); Assert.True(r.Next()); cts.Cancel(); Assert.False(r.Next()); }
public static void CreateException() { using var cts = new CancellationTokenSource(); var r = new CancellationRedlockRepeater(cts.Token); var exception = r.CreateException("rrrr", "nnnnn", 10000); var oce = Assert.IsType <OperationCanceledException>(exception); Assert.Equal(cts.Token, oce.CancellationToken); Assert.Contains("rrrr", oce.Message); Assert.Contains("nnnnn", oce.Message); Assert.Contains("10000", oce.Message); }
public async Task CreateAsync_WithCancellation() { var redlock = MockLock(); using var cts = new CancellationTokenSource(); _f.Setup(x => x.DefaultTtl("a")).Returns(_defaultTtl).Verifiable(); _f.Setup(x => x.DefaultMaxWaitMsBetweenReplays("a", _defaultTtl)).Returns(_defaultMaxWait).Verifiable(); var expectedRepeater = new CancellationRedlockRepeater(cts.Token); _f.Setup(x => x.CreateAsync("a", _defaultTtl, expectedRepeater, _defaultMaxWait, null)) .ReturnsAsync(redlock).Verifiable(); Assert.Equal(redlock, await _f.Object.CreateAsync("a", cts.Token)); }
public void MultiThread_NoOverlaps() { const string resource = nameof(MultiThread_NoOverlaps); var threads = new Thread[16]; var threadWaitMs = 100; using var cts = new CancellationTokenSource(threads.Length * threadWaitMs + 75000); var repeater = new CancellationRedlockRepeater(cts.Token); var locksCount = 0; var exceptions = new ConcurrentBag <Exception>(); for (var i = 0; i < threads.Length; i++) { var threadId = i; threads[i] = new Thread(() => { try { var nonce = threadId.ToString(); var ttl = TimeSpan.FromSeconds(10); using var l = Redlock.Lock(resource, nonce, ttl, _5Inst, _log, repeater, 50); Assert.Equal(0, locksCount); Interlocked.Increment(ref locksCount); Thread.Sleep(threadWaitMs); Assert.Equal(1, locksCount); Interlocked.Decrement(ref locksCount); } catch (Exception e) { exceptions.Add(e); } }); threads[i].Start(); } foreach (var thread in threads) { thread.Join(); } Assert.Empty(exceptions); }