public async Task DecrementMultiCancel() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** using (var MySource = new CancellationTokenSource()) { var MyTask1 = MyCounter.Decrement(MySource.Token); var MyTask2 = MyCounter.Decrement(); Assert.IsFalse(MyTask1.IsCompleted, "Decremented too early"); Assert.IsFalse(MyTask2.IsCompleted, "Decremented too early"); MySource.Cancel(); Assert.IsTrue(MyTask1.IsCanceled, "Wait not cancelled"); Assert.IsFalse(MyTask2.IsCanceled, "Wait cancelled"); MyCounter.Increment(); await MyTask2; } //**************************************** Assert.AreEqual(0, MyCounter.CurrentCount, "Counter not decremented"); Assert.AreEqual(0, MyCounter.WaitingCount, "Tasks unexpectedly waiting"); }
public void DecrementCancel() { //**************************************** var MyCounter = new AsyncCounter(); ValueTask MyTask; //**************************************** using (var MySource = new CancellationTokenSource()) { MyTask = MyCounter.Decrement(MySource.Token); Assert.IsFalse(MyTask.IsCompleted, "Decremented too early"); MySource.Cancel(); Assert.IsTrue(MyTask.IsCanceled, "Wait not cancelled"); } // Since there are no waiters that aren't cancelled, this should succeed immediately MyCounter.Increment(); //**************************************** Assert.AreEqual(1, MyCounter.CurrentCount, "Counter still decremented"); Assert.AreEqual(0, MyCounter.WaitingCount, "Tasks unexpectedly waiting"); }
public void Increment() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** MyCounter.Increment(); //**************************************** Assert.AreEqual(1, MyCounter.CurrentCount, "Counter not incremented"); Assert.AreEqual(0, MyCounter.WaitingCount, "Tasks unexpectedly waiting"); }
public void Decrement() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** MyCounter.Increment(); var MyTask = MyCounter.Decrement(); //**************************************** Assert.IsTrue(MyTask.IsCompleted, "Still waiting to decrement"); Assert.AreEqual(0, MyCounter.CurrentCount, "Counter not decremented"); Assert.AreEqual(0, MyCounter.WaitingCount, "Tasks unexpectedly waiting"); }
public async Task StackBlow() { //**************************************** var MyLock = new AsyncCounter(); Task ResultTask; Task[] Results; //**************************************** Results = Enumerable.Range(1, 40000).Select( async count => { await MyLock.Decrement(); MyLock.Increment(); }).ToArray(); ResultTask = Results[^ 1].ContinueWith(task => System.Diagnostics.Debug.WriteLine("Done to " + new System.Diagnostics.StackTrace(false).FrameCount.ToString()), TaskContinuationOptions.ExecuteSynchronously);
public void IncrementDispose() { //**************************************** var MyLock = new AsyncCounter(); //**************************************** MyLock.Increment(); var DisposeTask = MyLock.DisposeAsync(); Assert.IsFalse(DisposeTask.IsCompleted, "Dispose completed early"); //**************************************** Assert.AreEqual(0, MyLock.WaitingCount, "Waiter still registered"); Assert.AreEqual(1, MyLock.CurrentCount, "Count not one"); }
public void DecrementTimeoutNone() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** MyCounter.Increment(); var MyTask = MyCounter.Decrement(TimeSpan.FromMilliseconds(50)); //**************************************** Assert.IsTrue(MyTask.IsCompleted, "Still waiting to decrement"); Assert.AreEqual(0, MyCounter.WaitingCount, "Waiter still registered"); Assert.AreEqual(0, MyCounter.CurrentCount, "Count not zero"); }
public async Task DecrementTimeoutDelayed() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** var MyTask = MyCounter.Decrement(TimeSpan.FromMilliseconds(50)); MyCounter.Increment(); //**************************************** await MyTask; //**************************************** Assert.AreEqual(0, MyCounter.WaitingCount, "Waiter still registered"); Assert.AreEqual(0, MyCounter.CurrentCount, "Count not zero"); }
public async Task DecrementIncrement() { //**************************************** var MyCounter = new AsyncCounter(); //**************************************** var MyTask = MyCounter.Decrement(); Assert.IsFalse(MyTask.IsCompleted, "Decremented too early"); MyCounter.Increment(); //**************************************** await MyTask; Assert.AreEqual(0, MyCounter.CurrentCount, "Counter not decremented"); Assert.AreEqual(0, MyCounter.WaitingCount, "Tasks unexpectedly waiting"); }
public void IncrementDisposeTryDecrement() { //**************************************** var MyLock = new AsyncCounter(); //**************************************** MyLock.Increment(); _ = MyLock.DisposeAsync(); Assert.IsTrue(MyLock.TryDecrement()); Assert.IsFalse(MyLock.TryDecrement()); //**************************************** Assert.AreEqual(0, MyLock.WaitingCount, "Waiter still registered"); Assert.AreEqual(0, MyLock.CurrentCount, "Count not zero"); }
public async Task Manager_should_delete_old_stats() { const int expectedRepeats = 3; var age = TimeSpan.FromHours(1); var utcNow = DateTime.UtcNow; var asyncCountdown = new AsyncCountdown("delete old stats", expectedRepeats); var asyncCounter = new AsyncCounter(); var monitorSettings = new Mock <IMonitorSettings>(); monitorSettings.Setup(s => s.StatsHistoryMaxAge).Returns(age); var timeCoordinator = new Mock <ITimeCoordinator>(); timeCoordinator.Setup(c => c.UtcNow).Returns(utcNow); var endpointMetricsCoordinator = new Mock <IEndpointMetricsForwarderCoordinator>(); var repository = new Mock <IEndpointStatsRepository>(); repository.Setup(r => r.DeleteStatisticsOlderThan(utcNow - age, It.IsAny <int>())).Returns(() => { asyncCountdown.Decrement(); asyncCounter.Increment(); const int deletedItemsCount = 127; return(asyncCounter.Value >= expectedRepeats ? 0 : deletedItemsCount); }); using (new EndpointStatsManager(repository.Object, monitorSettings.Object, timeCoordinator.Object, endpointMetricsCoordinator.Object)) await asyncCountdown.WaitAsync(MaxTestTime); await Task.Delay(TimeSpan.FromMilliseconds(500)); //it should stop calling cleanup when there are no more items to be cleaned Assert.Equal(expectedRepeats, asyncCounter.Value); }
public void DisposeIncrement() { //**************************************** var MyLock = new AsyncCounter(); //**************************************** MyLock.DisposeAsync(); try { MyLock.Increment(); Assert.Fail("Should never reach this point"); } catch (ObjectDisposedException) { } //**************************************** Assert.AreEqual(0, MyLock.WaitingCount, "Waiter still registered"); Assert.AreEqual(0, MyLock.CurrentCount, "Count not zero"); }