public async Task CanRunAndScheduleConcurrently()
        {
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Trace);

            int hits  = 0;
            var timer = new ScheduledTimer(async() => {
                _logger.Info("Starting work.");
                Interlocked.Increment(ref hits);
                await Task.Delay(1000);
                _logger.Info("Finished work.");
                return(null);
            }, loggerFactory: Log);

            timer.ScheduleNext();
            await Task.Delay(1);

            timer.ScheduleNext();

            await Task.Delay(50);

            Assert.Equal(1, hits);

            await Task.Delay(1000);

            Assert.Equal(2, hits);
        }
        protected override async Task OnEnqueued(object sender, EnqueuedEventArgs <T> enqueuedEventArgs)
        {
            _timer.ScheduleNext(SystemClock.UtcNow.Add(_reportInterval));

            string subMetricName = GetSubMetricName(enqueuedEventArgs.Entry.Value);

            if (!String.IsNullOrEmpty(subMetricName))
            {
                await _metricsClient.CounterAsync(GetFullMetricName(subMetricName, "enqueued")).AnyContext();
            }

            await _metricsClient.CounterAsync(GetFullMetricName("enqueued")).AnyContext();
        }
Beispiel #3
0
        protected override Task OnEnqueued(object sender, EnqueuedEventArgs <T> enqueuedEventArgs)
        {
            _timer.ScheduleNext(SystemClock.UtcNow.Add(_reportInterval));

            string subMetricName = GetSubMetricName(enqueuedEventArgs.Entry.Value);

            if (!String.IsNullOrEmpty(subMetricName))
            {
                _metricsClient.Counter(GetFullMetricName(subMetricName, "enqueued"));
            }

            _metricsClient.Counter(GetFullMetricName("enqueued"));
            return(Task.CompletedTask);
        }
Beispiel #4
0
        protected override async Task OnEnqueued(object sender, EnqueuedEventArgs <T> enqueuedEventArgs)
        {
            await base.OnEnqueued(sender, enqueuedEventArgs).AnyContext();

            _timer.ScheduleNext();

            string customMetricName = GetCustomMetricName(enqueuedEventArgs.Entry.Value);

            if (!String.IsNullOrEmpty(customMetricName))
            {
                await _metricsClient.CounterAsync(GetFullMetricName(customMetricName, "enqueued")).AnyContext();
            }

            await _metricsClient.CounterAsync(GetFullMetricName("enqueued")).AnyContext();
        }
Beispiel #5
0
        public async Task CanRecoverFromError()
        {
            int hits       = 0;
            var resetEvent = new AsyncAutoResetEvent(false);

            Task <DateTime?> Callback()
            {
                Interlocked.Increment(ref hits);
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Callback called for the #{Hits} time", hits);
                }
                if (hits == 1)
                {
                    throw new Exception("Error in callback");
                }

                resetEvent.Set();
                return(Task.FromResult <DateTime?>(null));
            }

            using (var timer = new ScheduledTimer(Callback, loggerFactory: Log)) {
                timer.ScheduleNext();
                await resetEvent.WaitAsync(new CancellationTokenSource(800).Token);

                Assert.Equal(2, hits);
            }
        }
        public async Task CanRunConcurrent()
        {
            int hits = 0;

            Log.SetLogLevel <ScheduledTimer>(LogLevel.Trace);

            var timer = new ScheduledTimer(() => {
                int i = Interlocked.Increment(ref hits);
                _logger.Info($"Running {i}...");
                return(Task.FromResult <DateTime?>(null));
            }, minimumIntervalTime: TimeSpan.FromMilliseconds(250), loggerFactory: Log);

            for (int i = 0; i < 5; i++)
            {
                timer.ScheduleNext();
                await Task.Delay(5);
            }

            await Task.Delay(1000);

            Assert.Equal(2, hits);

            await Task.Delay(100);

            Assert.Equal(2, hits);
        }
Beispiel #7
0
        public async Task CanRunWithMinimumInterval()
        {
            var countdown = new AsyncCountdownEvent(2);

            Func <Task <DateTime?> > callback = async() => {
                _logger.Info("Starting work.");
                countdown.Signal();
                await SystemClock.SleepAsync(500);

                _logger.Info("Finished work.");
                return(null);
            };

            using (var timer = new ScheduledTimer(callback, minimumIntervalTime: TimeSpan.FromMilliseconds(100), loggerFactory: Log)) {
                for (int i = 0; i < 4; i++)
                {
                    timer.ScheduleNext();
                    SystemClock.Sleep(1);
                }

                await countdown.WaitAsync(TimeSpan.FromMilliseconds(100));

                Assert.Equal(1, countdown.CurrentCount);

                await countdown.WaitAsync(TimeSpan.FromSeconds(1.5));

                Assert.Equal(0, countdown.CurrentCount);
            }
        }
Beispiel #8
0
        public async Task CanRecoverFromError()
        {
            var resetEvent = new AsyncAutoResetEvent(false);

            int hits = 0;
            Func <Task <DateTime?> > callback = () => {
                Interlocked.Increment(ref hits);
                _logger.Info("Callback called for the #{time} time", hits);
                if (hits == 1)
                {
                    throw new Exception("Error in callback");
                }

                resetEvent.Set();
                return(Task.FromResult <DateTime?>(null));
            };

            using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) {
                timer.ScheduleNext();

                await resetEvent.WaitAsync(new CancellationTokenSource(500).Token);

                Assert.Equal(2, hits);
            }
        }
Beispiel #9
0
        private async Task CanRunConcurrentlyAsync(TimeSpan?minimumIntervalTime = null)
        {
            Log.MinimumLevel = LogLevel.Trace;
            const int iterations = 2;
            var       countdown  = new AsyncCountdownEvent(iterations);

            async Task <DateTime?> Callback()
            {
                _logger.LogInformation("Starting work.");
                await SystemClock.SleepAsync(250);

                countdown.Signal();
                _logger.LogInformation("Finished work.");
                return(null);
            }

            using (var timer = new ScheduledTimer(Callback, minimumIntervalTime: minimumIntervalTime, loggerFactory: Log)) {
                timer.ScheduleNext();
                var t = Task.Run(async() => {
                    for (int i = 0; i < iterations; i++)
                    {
                        await SystemClock.SleepAsync(10);
                        timer.ScheduleNext();
                    }
                });

                _logger.LogInformation("Waiting for 300ms");
                await countdown.WaitAsync(TimeSpan.FromMilliseconds(300));

                _logger.LogInformation("Finished waiting for 300ms");
                Assert.Equal(iterations - 1, countdown.CurrentCount);

                _logger.LogInformation("Waiting for 1.5 seconds");
                await countdown.WaitAsync(TimeSpan.FromSeconds(1.5));

                _logger.LogInformation("Finished waiting for 1.5 seconds");
                Assert.Equal(0, countdown.CurrentCount);
            }
        }
        public async Task CanRunWithMinimumInterval()
        {
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Trace);
            var resetEvent = new AsyncAutoResetEvent(false);

            int hits  = 0;
            var timer = new ScheduledTimer(() => {
                Interlocked.Increment(ref hits);
                resetEvent.Set();
                return(Task.FromResult <DateTime?>(null));
            }, minimumIntervalTime: TimeSpan.FromMilliseconds(100), loggerFactory: Log);

            timer.ScheduleNext();
            await Task.Delay(1);

            timer.ScheduleNext();
            await Task.Delay(1);

            timer.ScheduleNext();

            await resetEvent.WaitAsync(new CancellationTokenSource(100).Token);

            var sw = Stopwatch.StartNew();

            Assert.Equal(1, hits);

            await resetEvent.WaitAsync(new CancellationTokenSource(500).Token);

            sw.Stop();
            Assert.Equal(2, hits);

            Assert.Throws <TaskCanceledException>(() => {
                resetEvent.Wait(new CancellationTokenSource(100).Token);
            });

            await Task.Delay(110);

            Assert.Equal(2, hits);
        }
Beispiel #11
0
        public async Task CanRun()
        {
            var resetEvent = new AsyncAutoResetEvent();
            Func <Task <DateTime?> > callback = () => {
                resetEvent.Set();
                return(null);
            };

            using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) {
                timer.ScheduleNext();
                await resetEvent.WaitAsync(new CancellationTokenSource(500).Token);
            }
        }
Beispiel #12
0
        private async Task CanRunConcurrentlyAsync(TimeSpan?minimumIntervalTime = null)
        {
            var countdown = new AsyncCountdownEvent(2);

            Func <Task <DateTime?> > callback = async() => {
                _logger.Info("Starting work.");
                countdown.Signal();
                await SystemClock.SleepAsync(500);

                _logger.Info("Finished work.");
                return(null);
            };

            using (var timer = new ScheduledTimer(callback, minimumIntervalTime: minimumIntervalTime, loggerFactory: Log)) {
                timer.ScheduleNext();
                var t = Task.Run(async() => {
                    for (int i = 0; i < 3; i++)
                    {
                        await SystemClock.SleepAsync(10);
                        timer.ScheduleNext();
                    }
                });

                _logger.Info("Waiting for 300ms");
                await countdown.WaitAsync(TimeSpan.FromMilliseconds(300));

                _logger.Info("Finished waiting for 300ms");
                Assert.Equal(1, countdown.CurrentCount);

                _logger.Info("Waiting for 1.5 seconds");
                await countdown.WaitAsync(TimeSpan.FromSeconds(1.5));

                _logger.Info("Finished waiting for 1.5 seconds");
                Assert.Equal(0, countdown.CurrentCount);
            }
        }
Beispiel #13
0
        public async Task CanRun()
        {
            var countdown = new AsyncCountdownEvent(1);
            Func <Task <DateTime?> > callback = () => {
                countdown.Signal();
                return(null);
            };

            using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) {
                timer.ScheduleNext();
                await countdown.WaitAsync(TimeSpan.FromMilliseconds(100));

                Assert.Equal(0, countdown.CurrentCount);
            }
        }
        public async Task CanRun()
        {
            Log.SetLogLevel <ScheduledTimer>(LogLevel.Trace);

            int hits  = 0;
            var timer = new ScheduledTimer(async() => {
                Interlocked.Increment(ref hits);
                await Task.Delay(50);
                return(null);
            }, loggerFactory: Log);

            timer.ScheduleNext();

            await Task.Delay(50);

            Assert.Equal(1, hits);
        }