elsa-core/test/unit/Elsa.Scheduling.UnitTests/ScheduledTasks/ScheduledCronTaskTests.cs

190 lines
7.1 KiB
C#
Raw Normal View History

using Elsa.Common;
using Elsa.Mediator.Contracts;
using Elsa.Scheduling.ScheduledTasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
namespace Elsa.Scheduling.UnitTests.ScheduledTasks;
/// <summary>
/// Tests for ScheduledCronTask to ensure cron triggers continue to fire even in edge cases.
/// </summary>
public class ScheduledCronTaskTests : IDisposable
{
private const string DefaultCronExpression = "0 */5 * * * *";
private static readonly DateTimeOffset DefaultNow = new(2025, 11, 06, 22, 50, 00, 0, TimeSpan.Zero);
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
private readonly ServiceProvider _serviceProvider;
private readonly ISystemClock _systemClock;
private readonly ICronParser _cronParser;
private readonly ILogger<ScheduledCronTask> _logger;
private readonly List<ScheduledCronTask> _tasksToDispose = new();
public ScheduledCronTaskTests()
{
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
var services = new ServiceCollection();
_systemClock = Substitute.For<ISystemClock>();
_cronParser = Substitute.For<ICronParser>();
_logger = Substitute.For<ILogger<ScheduledCronTask>>();
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
services.AddSingleton(Substitute.For<ICommandSender>());
_serviceProvider = services.BuildServiceProvider();
}
private ScheduledCronTask CreateScheduledTask(
string? cronExpression = null,
ICronParser? cronParser = null,
ISystemClock? systemClock = null)
{
var task = Substitute.For<ITask>();
var scheduledTask = new ScheduledCronTask(
task,
cronExpression ?? DefaultCronExpression,
cronParser ?? _cronParser,
_serviceProvider.CreateScope().ServiceProvider.GetRequiredService<IServiceScopeFactory>(),
systemClock ?? _systemClock,
_logger
);
_tasksToDispose.Add(scheduledTask);
return scheduledTask;
}
private void SetupCronParser(params DateTimeOffset[] occurrences)
{
_cronParser.GetNextOccurrence(Arg.Any<string>()).Returns(occurrences[0], occurrences.Skip(1).ToArray());
}
private void SetupSystemClock(params DateTimeOffset[] times)
{
_systemClock.UtcNow.Returns(times[0], times.Skip(1).ToArray());
}
private void AssertNoErrorLogged()
{
_logger.DidNotReceive().Log(
LogLevel.Error,
Arg.Any<EventId>(),
Arg.Any<object>(),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception?, string>>());
}
private void AssertWarningLogged(int expectedCount = 1)
{
_logger.Received(expectedCount).Log(
LogLevel.Warning,
Arg.Any<EventId>(),
Arg.Any<object>(),
Arg.Any<Exception>(),
Arg.Any<Func<object, Exception?, string>>());
}
[Fact]
public void Schedule_WithVerySmallDelay_ShouldStillSetupTimer()
{
// Arrange - simulate a case where the delay is very small (1 tick = 100ns)
SetupSystemClock(DefaultNow);
SetupCronParser(DefaultNow.AddTicks(1)); // Only 1 tick in the future (100 nanoseconds)
// Act
CreateScheduledTask();
// Assert - Verify that no error was logged (timer should be set up successfully)
AssertNoErrorLogged();
}
[Fact]
public void Schedule_WithZeroDelay_ShouldRetryAndSetupTimer()
{
// Arrange - simulate a case where the first call returns exactly now
// but the second call returns a proper future time
SetupSystemClock(DefaultNow, DefaultNow);
SetupCronParser(DefaultNow, DefaultNow.AddMinutes(5)); // First: delay=0, Second: proper future time
// Act
CreateScheduledTask();
// Assert - Should call GetNextOccurrence twice (once for initial delay=0, once for retry)
_cronParser.Received(2).GetNextOccurrence(DefaultCronExpression);
}
[Fact]
public void Schedule_WithNegativeDelay_ShouldRetryAndSetupTimer()
{
// Arrange - simulate a case where the first call returns a time in the past
SetupSystemClock(DefaultNow, DefaultNow);
SetupCronParser(DefaultNow.AddMinutes(-1), DefaultNow.AddMinutes(5)); // First: past, Second: future
// Act
CreateScheduledTask();
// Assert - Should call GetNextOccurrence twice
_cronParser.Received(2).GetNextOccurrence(DefaultCronExpression);
}
[Fact]
public void Schedule_WithPersistentZeroDelay_ShouldLogWarningAndUseMinimumDelay()
{
// Arrange - simulate the bug scenario: both attempts return zero/negative delay
// This can happen if the system clock doesn't advance or if there's clock drift
SetupSystemClock(DefaultNow);
SetupCronParser(DefaultNow); // Both calls return exactly now (delay = 0)
// Act - This should not crash and should set up a timer with minimum delay
CreateScheduledTask();
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
// Assert - Should call GetNextOccurrence twice (initial + retry) and log warning once (on final attempt)
_cronParser.Received(2).GetNextOccurrence(DefaultCronExpression);
AssertWarningLogged();
}
[Fact]
public void Schedule_WithNegativeDelayAfterRetry_ShouldLogWarningAndUseMinimumDelay()
{
// Arrange - simulate a case where even after retry, delay is negative
// This could happen due to system clock adjustments
SetupSystemClock(DefaultNow, DefaultNow);
SetupCronParser(DefaultNow.AddMilliseconds(-100), DefaultNow.AddMilliseconds(-50));
// Act - Should handle negative delay gracefully
CreateScheduledTask();
// Assert - Should log a warning and still set up timer
AssertWarningLogged();
}
[Fact]
public void ReproduceOriginalIssue_WithRealCronParser_DemonstratesBugScenario()
{
// This test reproduces the exact scenario from the original issue report
// Using the real CronosCronParser with specific times that trigger the bug
// Arrange - Use the exact times from the issue that cause delay.Milliseconds to be 0
var now = new DateTimeOffset(2025, 11, 06, 22, 50, 00, 0, TimeZoneInfo.Utc.GetUtcOffset(DateTimeOffset.UtcNow));
var systemClock = Substitute.For<ISystemClock>();
systemClock.UtcNow.Returns(now);
var realCronParser = new Elsa.Scheduling.Services.CronosCronParser(systemClock);
// Act - Create scheduled task with real cron parser
// Before the fix: This would silently fail to schedule if delay <= 0
// After the fix: This should log warning and use minimum delay
CreateScheduledTask(cronParser: realCronParser, systemClock: systemClock);
// Assert - The key is that the task was created successfully without throwing or silently failing
// This test passes with the fix, demonstrating the issue is resolved
}
public void Dispose()
{
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
// Dispose tasks first to stop timers before disposing ServiceProvider
foreach (var task in _tasksToDispose)
{
((IDisposable)task).Dispose();
}
Added IEnumerableTypeConverter (#7020) * Added IEnumerableTypeConverter * fixed bug and added tests * Add resource disposal mechanism in ScheduledCronTask - Introduced `_disposed` flag to prevent execution after disposal. - Updated disposal logic to ensure proper release of resources. - Adjusted unit tests to verify new disposal behavior and prevent unintended timer actions. * Enhance scheduling tasks with edge case handling and disposal improvements - Added `_disposed` flag to `ScheduledRecurringTask` and `ScheduledSpecificInstantTask` to prevent execution after disposal. - Adjusted timer setup logic to handle zero/negative delays with a minimum delay of 1ms. - Updated disposal logic to ensure proper resource cleanup even during timer callbacks. - Introduced extensive unit tests for edge cases such as small, zero, or negative delay scenarios and proper disposal behavior. * Update test/component/Elsa.Workflows.ComponentTests/Scenarios/VariablesArray/Activities/RemoveTopElementStep.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/modules/Elsa.Common/Serialization/IEnumerableTypeConverter.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add integration tests for EnumerableTypeConverter and update solution file - Introduced `Elsa.Common.IntegrationTests` project for testing serialization behavior in `EnumerableTypeConverter`. - Added tests to verify proper handling of strings, byte arrays, and collections during JSON serialization. - Registered `EnumerableTypeConverter` in `DefaultFormattersFeature`. - Renamed `IEnumerableTypeConverter` to `EnumerableTypeConverter` for consistency. - Updated solution file to include the new integration test project. --------- Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:07:33 +00:00
// Small delay to ensure any timer callbacks have completed
Thread.Sleep(10);
_serviceProvider.Dispose();
}
}