public async Task StartAsync_ExtendedScheduleInterval_TimerContinuesUntilTotalIntervalComplete()
        {
            // create a timer with an extended interval that exceeds the max
            TimeSpan interval = TimerListener.MaxTimerInterval + TimerListener.MaxTimerInterval + TimeSpan.FromDays(4);

            CreateTestListener(interval.ToString(), useMonitor: false);

            CancellationToken cancellationToken = new CancellationToken();
            await _listener.StartAsync(cancellationToken);

            Assert.Equal(TimerListener.MaxTimerInterval.TotalMilliseconds, _listener.Timer.Interval);

            // simulate first timer event - expect the timer to continue without
            // invoking the job function
            await _listener.HandleTimerEvent();

            Assert.Equal(TimerListener.MaxTimerInterval.TotalMilliseconds, _listener.Timer.Interval);

            // simulate second timer event - expect the timer to continue without
            // invoking the job function
            await _listener.HandleTimerEvent();

            Assert.Equal(TimeSpan.FromDays(4).TotalMilliseconds, _listener.Timer.Interval);

            // simulate final timer event for the interval - expect the job function to be executed now,
            // and the interval start from the beginning
            await _listener.HandleTimerEvent();

            Assert.Equal(TimerListener.MaxTimerInterval.TotalMilliseconds, _listener.Timer.Interval);

            // verify that the job function was only invoked once
            _mockTriggerExecutor.Verify(p => p.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>()), Times.Once());

            _listener.Dispose();
        }
Ejemplo n.º 2
0
        public async Task HandleTimerEvent_HandlesExceptions()
        {
            // force an exception to occur outside of the function invocation path
            var ex = new Exception("Kaboom!");

            _mockScheduleMonitor.Setup(p => p.UpdateStatusAsync(_testTimerName, It.IsAny <ScheduleStatus>())).ThrowsAsync(ex);

            var listener = new TimerListener(_attribute, _schedule, _testTimerName, _options, _mockTriggerExecutor.Object, _logger, _mockScheduleMonitor.Object, _functionShortName);

            Assert.Null(listener.Timer);

            await listener.HandleTimerEvent();

            // verify the timer was started
            Assert.NotNull(listener.Timer);
            Assert.True(listener.Timer.Enabled);

            var logs = _logger.GetLogMessages();
            var log  = logs[0];

            Assert.Equal(LogLevel.Error, log.Level);
            Assert.Equal("Error occurred during scheduled invocation for 'TimerFunctionShortName'.", log.FormattedMessage);
            Assert.Same(ex, log.Exception);
            log = logs[1];
            Assert.Equal(LogLevel.Debug, log.Level);
            Assert.True(log.FormattedMessage.StartsWith("Timer for 'TimerFunctionShortName' started with interval"));

            listener.Dispose();
        }