public async Task Agent_Monitor() { using (var logs = await _client.Agent.Monitor(LogLevel.Trace)) { var counter = 0; var logsTask = Task.Run(async() => { // to get some logs await _client.Agent.Self(); foreach (var line in logs) { // Make a request each time so we get more logs await _client.Agent.Self(); Assert.False(string.IsNullOrEmpty(await line)); counter++; if (counter > 5) { break; } } }); await TimeoutUtils.WithTimeout(logsTask); } }
public async Task Semaphore_ContendFast() { const string keyName = "test/semaphore/contend"; const int contenderPool = 15; var acquired = new System.Collections.Concurrent.ConcurrentDictionary <int, bool>(); var tasks = new List <Task>(); for (var i = 0; i < contenderPool; i++) { var v = i; tasks.Add(Task.Run(async() => { var semaphore = _client.Semaphore(keyName, 2); await semaphore.Acquire(CancellationToken.None); acquired[v] = semaphore.IsHeld; await semaphore.Release(); })); } await TimeoutUtils.WithTimeout(Task.WhenAll(tasks)); for (var i = 0; i < contenderPool; i++) { Assert.True(acquired.ContainsKey(i), $"Contender {i} did not acquire the lock"); Assert.True(acquired[i], $"IsHeld was false for contender {i}"); } }
public async Task Lock_ContendWait() { const string keyName = "test/lock/contendwait"; const int contenderPool = 3; var acquired = new System.Collections.Concurrent.ConcurrentDictionary <int, bool>(); var tasks = new List <Task>(); for (var i = 0; i < contenderPool; i++) { var v = i; acquired[v] = false; tasks.Add(Task.Run(async() => { var lockKey = _client.CreateLock(keyName); await lockKey.Acquire(CancellationToken.None); acquired[v] = lockKey.IsHeld; if (lockKey.IsHeld) { await Task.Delay(1000); await lockKey.Release(); } })); } await TimeoutUtils.WithTimeout(Task.WhenAll(tasks)); for (var i = 0; i < contenderPool; i++) { Assert.True(acquired[i], "Contender " + i.ToString() + " did not acquire the lock"); } }
public async Task Lock_OneShot() { const string keyName = "test/lock/oneshot"; var lockOptions = new LockOptions(keyName) { LockTryOnce = true }; Assert.Equal(Lock.DefaultLockWaitTime, lockOptions.LockWaitTime); lockOptions.LockWaitTime = TimeSpan.FromMilliseconds(1000); var lockKey = _client.CreateLock(lockOptions); await lockKey.Acquire(CancellationToken.None); var contender = _client.CreateLock(new LockOptions(keyName) { LockTryOnce = true, LockWaitTime = TimeSpan.FromMilliseconds(1000) }); var stopwatch = Stopwatch.StartNew(); Assert.True(lockKey.IsHeld); Assert.False(contender.IsHeld); await TimeoutUtils.WithTimeout( Assert.ThrowsAsync <LockMaxAttemptsReachedException>(async() => await contender.Acquire())); Assert.False(stopwatch.ElapsedMilliseconds < lockOptions.LockWaitTime.TotalMilliseconds); Assert.False(contender.IsHeld, "Contender should have failed to acquire"); Assert.True(lockKey.IsHeld); Assert.False(contender.IsHeld); await lockKey.Release(); Assert.False(lockKey.IsHeld); Assert.False(contender.IsHeld); while (contender.IsHeld == false) { try { await contender.Acquire(); Assert.False(lockKey.IsHeld); Assert.True(contender.IsHeld); } catch (LockMaxAttemptsReachedException) { // Ignore because lock delay might be in effect. } } await contender.Release(); await contender.Destroy(); }
public async Task Semaphore_OneShot() { const string keyName = "test/semaphore/oneshot"; TimeSpan waitTime = TimeSpan.FromMilliseconds(3000); var semaphoreOptions = new SemaphoreOptions(keyName, 2) { SemaphoreTryOnce = true }; semaphoreOptions.SemaphoreWaitTime = waitTime; var semaphoreKey = _client.Semaphore(semaphoreOptions); await semaphoreKey.Acquire(CancellationToken.None); Assert.True(semaphoreKey.IsHeld); var another = _client.Semaphore(new SemaphoreOptions(keyName, 2) { SemaphoreTryOnce = true, SemaphoreWaitTime = waitTime }); await another.Acquire(); Assert.True(another.IsHeld); Assert.True(semaphoreKey.IsHeld); var contender = _client.Semaphore(new SemaphoreOptions(keyName, 2) { SemaphoreTryOnce = true, SemaphoreWaitTime = waitTime }); var stopwatch = Stopwatch.StartNew(); await TimeoutUtils.WithTimeout( Assert.ThrowsAsync <SemaphoreMaxAttemptsReachedException>(async() => await contender.Acquire())); Assert.False(contender.IsHeld, "Contender should have failed to acquire"); Assert.False(stopwatch.ElapsedMilliseconds < semaphoreOptions.SemaphoreWaitTime.TotalMilliseconds); Assert.False(contender.IsHeld); Assert.True(another.IsHeld); Assert.True(semaphoreKey.IsHeld); await semaphoreKey.Release(); await another.Release(); await contender.Destroy(); }
public async Task Lock_ReclaimLock() { const string keyName = "test/lock/reclaim"; var sessionRequest = await _client.Session.Create(); var sessionId = sessionRequest.Response; try { var lock1 = _client.CreateLock(new LockOptions(keyName) { Session = sessionId }); var lock2 = _client.CreateLock(new LockOptions(keyName) { Session = sessionId }); try { await lock1.Acquire(CancellationToken.None); Assert.True(lock1.IsHeld); await TimeoutUtils.WithTimeout(lock2.Acquire(CancellationToken.None)); Assert.True(lock2.IsHeld); } finally { await lock1.Release(); } await TimeoutUtils.WaitFor(() => !lock1.IsHeld, "Lock 1 is still held"); // By releasing lock1, lock2 should also eventually be released as it is for the same session await TimeoutUtils.WaitFor(() => !lock2.IsHeld, "Lock 2 is still held"); } finally { var destroyResponse = await _client.Session.Destroy(sessionId); Assert.True(destroyResponse.Response, "Failed to destroy session"); } }
public async Task Lock_Contend_LockDelay() { const string keyName = "test/lock/contendlockdelay"; const int contenderPool = 3; var acquired = new System.Collections.Concurrent.ConcurrentDictionary <int, bool>(); var tasks = new List <Task>(); for (var i = 0; i < contenderPool; i++) { var v = i; tasks.Add(Task.Run(async() => { var lockKey = (Lock)_client.CreateLock(keyName); await lockKey.Acquire(CancellationToken.None); if (lockKey.IsHeld) { Assert.True(acquired.TryAdd(v, lockKey.IsHeld)); await _client.Session.Destroy(lockKey.LockSession); } })); } // (contenderPool - 1) tasks will need to wait for the default session lock delay // before the lock can be acquired. await TimeoutUtils.WithTimeout(Task.WhenAll(tasks)); for (var i = 0; i < contenderPool; i++) { bool didContend = false; if (acquired.TryGetValue(i, out didContend)) { Assert.True(didContend); } else { Assert.True(false, "Contender " + i.ToString() + " did not acquire the lock"); } } }
public async Task Lock_ContendFast() { const string keyName = "test/lock/contendfast"; const int contenderPool = 10; var acquired = new System.Collections.Concurrent.ConcurrentDictionary <int, bool>(); var tasks = new List <Task>(); for (var i = 0; i < contenderPool; i++) { var v = i; tasks.Add(Task.Run(async() => { var lockKey = _client.CreateLock(keyName); await lockKey.Acquire(CancellationToken.None); Assert.True(acquired.TryAdd(v, lockKey.IsHeld)); if (lockKey.IsHeld) { await lockKey.Release(); } })); } await TimeoutUtils.WithTimeout(Task.WhenAll(tasks)); for (var i = 0; i < contenderPool; i++) { if (acquired.ContainsKey(i)) { Assert.True(acquired[i]); } else { Assert.True(false, "Contender " + i.ToString() + " did not acquire the lock"); } } }