Merge pull request #9329 from AndyButland/feature/amend-health-check-notifier-first-run-time-to-crontab

.NetCore: change health check notifier first run time to cron expression
This commit is contained in:
Bjarke Berg
2020-11-03 12:06:34 +01:00
committed by GitHub
16 changed files with 128 additions and 143 deletions

View File

@@ -1,26 +1,33 @@
using System;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Configuration.Models.Extensions;
using Umbraco.Infrastructure.Configuration.Extensions;
namespace Umbraco.Tests.UnitTests.Umbraco.Core.Configuration.Models.Extensions
{
[TestFixture]
public class HealthCheckSettingsExtensionsTests
{
[Test]
public void Returns_Notification_Delay_From_Provided_Time()
private ICronTabParser CronTabParser => new NCronTabParser();
[TestCase("30 12 * * *", 30)]
[TestCase("15 18 * * *", 60 * 6 + 15)]
[TestCase("0 3 * * *", 60 * 15)]
[TestCase("0 3 2 * *", 24 * 60 * 1 + 60 * 15)]
[TestCase("0 6 * * 3", 24 * 60 * 3 + 60 * 18)]
public void Returns_Notification_Delay_From_Provided_Time(string firstRunTimeCronExpression, int expectedDelayInMinutes)
{
var settings = new HealthChecksSettings
{
Notification = new HealthChecksNotificationSettings
{
FirstRunTime = "1230",
FirstRunTime = firstRunTimeCronExpression,
}
};
var now = DateTime.Now.Date.AddHours(12);
var result = settings.GetNotificationDelay(now, TimeSpan.Zero);
Assert.AreEqual(30, result.Minutes);
var now = new DateTime(2020, 10, 31, 12, 0, 0);
var result = settings.GetNotificationDelay(CronTabParser, now, TimeSpan.Zero);
Assert.AreEqual(expectedDelayInMinutes, result.TotalMinutes);
}
[Test]
@@ -30,12 +37,12 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Configuration.Models.Extensions
{
Notification = new HealthChecksNotificationSettings
{
FirstRunTime = "1230",
FirstRunTime = "30 12 * * *",
}
};
var now = DateTime.Now.Date.AddHours(12).AddMinutes(25);
var result = settings.GetNotificationDelay(now, TimeSpan.FromMinutes(10));
Assert.AreEqual(10, result.Minutes);
var now = new DateTime(2020, 10, 31, 12, 25, 0);
var result = settings.GetNotificationDelay(CronTabParser, now, TimeSpan.FromMinutes(10));
Assert.AreEqual(10, result.TotalMinutes);
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using NUnit.Framework;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Configuration.Models.Validation;
@@ -11,7 +12,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Configuration.Models.Validation
[Test]
public void Returns_Success_ForValid_Configuration()
{
var validator = new HealthChecksSettingsValidator();
var validator = new HealthChecksSettingsValidator(new NCronTabParser());
var options = BuildHealthChecksSettings();
var result = validator.Validate("settings", options);
Assert.True(result.Succeeded);
@@ -20,13 +21,13 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.Configuration.Models.Validation
[Test]
public void Returns_Fail_For_Configuration_With_Invalid_Notification_FirstRunTime()
{
var validator = new HealthChecksSettingsValidator();
var options = BuildHealthChecksSettings(firstRunTime: "25:00");
var validator = new HealthChecksSettingsValidator(new NCronTabParser());
var options = BuildHealthChecksSettings(firstRunTime: "0 3 *");
var result = validator.Validate("settings", options);
Assert.False(result.Succeeded);
}
private static HealthChecksSettings BuildHealthChecksSettings(string firstRunTime = "12:00")
private static HealthChecksSettings BuildHealthChecksSettings(string firstRunTime = "0 3 * * *")
{
return new HealthChecksSettings
{

View File

@@ -0,0 +1,32 @@
using NUnit.Framework;
using Umbraco.Core.Configuration;
namespace Umbraco.Tests.UnitTests.Umbraco.Core.Configurations
{
[TestFixture]
public class NCronTabParserTests
{
private ICronTabParser Sut => new NCronTabParser();
[TestCase("", ExpectedResult = false)]
[TestCase("* * * * 1", ExpectedResult = true)]
[TestCase("* * * * * 1", ExpectedResult = false)]
[TestCase("* * * 1", ExpectedResult = false)]
[TestCase("Invalid", ExpectedResult = false)]
[TestCase("I n v a l", ExpectedResult = false)]
[TestCase("23 0-20/2 * * *", ExpectedResult = true)]
[TestCase("5 4 * * sun", ExpectedResult = true)]
[TestCase("0 0,12 1 */2 *", ExpectedResult = true)]
[TestCase("0 0 1,15 * 3", ExpectedResult = true)]
[TestCase("5 0 * 8 *", ExpectedResult = true)]
[TestCase("22 * * 1-5 *", ExpectedResult = true)]
[TestCase("23 0-20/2 * * *", ExpectedResult = true)]
[TestCase("23 0-20/2 * * sun-sat", ExpectedResult = true)]
[TestCase("23 0-20/2 * jan-dec sun-sat", ExpectedResult = true)]
[TestCase("* * 32 * *", ExpectedResult = false)]
public bool IsValidCronTab(string input)
{
return Sut.IsValidCronTab(input);
}
}
}

View File

@@ -1,46 +0,0 @@
using System;
using NUnit.Framework;
using Umbraco.Core;
namespace Umbraco.Tests.UnitTests.Umbraco.Core
{
[TestFixture]
public class DateTimeExtensionsTests
{
[Test]
public void PeriodicMinutesFrom_PostTime_CalculatesMinutesBetween()
{
var nowDateTime = new DateTime(2017, 1, 1, 10, 30, 0);
var scheduledTime = "1145";
var minutesBetween = nowDateTime.PeriodicMinutesFrom(scheduledTime);
Assert.AreEqual(75, minutesBetween);
}
[Test]
public void PeriodicMinutesFrom_PriorTime_CalculatesMinutesBetween()
{
var nowDateTime = new DateTime(2017, 1, 1, 10, 30, 0);
var scheduledTime = "900";
var minutesBetween = nowDateTime.PeriodicMinutesFrom(scheduledTime);
Assert.AreEqual(1350, minutesBetween);
}
[Test]
public void PeriodicMinutesFrom_PriorTime_WithLeadingZero_CalculatesMinutesBetween()
{
var nowDateTime = new DateTime(2017, 1, 1, 10, 30, 0);
var scheduledTime = "0900";
var minutesBetween = nowDateTime.PeriodicMinutesFrom(scheduledTime);
Assert.AreEqual(1350, minutesBetween);
}
[Test]
public void PeriodicMinutesFrom_SameTime_CalculatesMinutesBetween()
{
var nowDateTime = new DateTime(2017, 1, 1, 10, 30, 0);
var scheduledTime = "1030";
var minutesBetween = nowDateTime.PeriodicMinutesFrom(scheduledTime);
Assert.AreEqual(0, minutesBetween);
}
}
}

View File

@@ -301,15 +301,5 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Core.ShortStringHelper
Assert.AreEqual(expected, output);
}
[TestCase("", false)]
[TestCase("12:34", true)]
[TestCase("1:14:23", true)]
[TestCase("25:03", false)]
[TestCase("18:61", false)]
public void IsValidTimeSpan(string input, bool expected)
{
var result = input.IsValidTimeSpan();
Assert.AreEqual(expected, result);
}
}
}

View File

@@ -7,6 +7,7 @@ using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.HealthCheck;
using Umbraco.Core.Logging;
@@ -141,7 +142,7 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
return new HealthCheckNotifier(Options.Create(settings), checks, notifications,
mockRunTimeState.Object, mockServerRegistrar.Object, mockMainDom.Object, mockScopeProvider.Object,
mockLogger.Object, mockProfilingLogger.Object);
mockLogger.Object, mockProfilingLogger.Object, Mock.Of<ICronTabParser>());
}
private void VerifyNotificationsNotSent()