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 async Task Executor_should_execute_tasks_until_disposal()
        {
            var task1Name = "task1";
            var task2Name = "task2";
            var countdown1 = new AsyncCountdown(task1Name, 10);
            var counter1 = new AsyncCounter();
            var countdown2 = new AsyncCountdown(task2Name, 10);
            var counter2 = new AsyncCounter();

            using (var executor = ContinuousTaskExecutor<string>.StartExecutor(Mock.Of<ITimeCoordinator>()))
            {
                Assert.True(executor.TryRegisterTaskFor(task1Name, (item, token) => StartTaskAsync(countdown1, counter1, token)));
                await countdown1.WaitAsync(_testTimeout);

                Assert.True(executor.TryRegisterTaskFor(task2Name, (item, token) => StartTaskAsync(countdown2, counter2, token)));
                await countdown2.WaitAsync(_testTimeout);

                // check that task 1 still works
                await countdown1.ResetTo(10).WaitAsync(_testTimeout);
            }

            var expected1 = counter1.Value;
            var expected2 = counter2.Value;
            await Task.Delay(200);

            Assert.Equal(expected1, counter1.Value);
            Assert.Equal(expected2, counter2.Value);
        }
Esempio n. 3
0
        public AwaitableBuilder <T> WithCounter(AsyncCounter counter)
        {
            var inner = _task;

            _task = async() =>
            {
                try
                {
                    return(await inner.Invoke());
                }
                finally
                {
                    counter.Increment();
                }
            };
            return(this);
        }
 private Task StartTaskAsync(AsyncCountdown countdown, AsyncCounter counter, CancellationToken token)
 {
     return StartTaskAsync(token, c => c.WithCountdown(countdown).WithCounter(counter));
 }