Fixes failing unit test due to null reference.

Amended signatures of method under test to support testing without the previously observed behaviour of an erroring test passing but crashing the test runner process.
This commit is contained in:
Andy Butland
2020-11-02 11:59:39 +01:00
parent 978b8eeda7
commit 21c29d6446
9 changed files with 68 additions and 45 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Core;
@@ -52,9 +53,8 @@ namespace Umbraco.Infrastructure.HostedServices
_logger = logger;
_profilingLogger = profilingLogger;
}
public override async void ExecuteAsync(object state)
internal override async Task PerformExecuteAsync(object state)
{
if (_healthChecksSettings.Notification.Enabled == false)
{

View File

@@ -1,5 +1,6 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Core;
@@ -35,7 +36,7 @@ namespace Umbraco.Infrastructure.HostedServices
_httpClientFactory = httpClientFactory;
}
public override async void ExecuteAsync(object state)
internal override async Task PerformExecuteAsync(object state)
{
if (_keepAliveSettings.DisableKeepAliveTask)
{
@@ -50,7 +51,7 @@ namespace Umbraco.Infrastructure.HostedServices
return;
case ServerRole.Unknown:
_logger.LogDebug("Does not run on servers with unknown role.");
return;
return;
}
// Ensure we do not run if not main domain, but do NOT lock it

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Core;
@@ -38,7 +39,7 @@ namespace Umbraco.Infrastructure.HostedServices
_profilingLogger = profilingLogger;
}
public override async void ExecuteAsync(object state)
internal override async Task PerformExecuteAsync(object state)
{
switch (_serverRegistrar.GetCurrentServerRole())
{

View File

@@ -31,7 +31,16 @@ namespace Umbraco.Infrastructure.HostedServices
return Task.CompletedTask;
}
public abstract void ExecuteAsync(object state);
public async void ExecuteAsync(object state)
{
// Delegate work to method returning a task, that can be called and asserted in a unit test.
// Without this there can be behaviour where tests pass, but an error within them causes the test
// running process to crash.
// Hat-tip: https://stackoverflow.com/a/14207615/489433
await PerformExecuteAsync(state);
}
internal abstract Task PerformExecuteAsync(object state);
public Task StopAsync(CancellationToken cancellationToken)
{

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Umbraco.Core;
using Umbraco.Core.IO;
@@ -32,7 +33,7 @@ namespace Umbraco.Infrastructure.HostedServices
_tempFolders = _ioHelper.GetTempFolders();
}
public override async void ExecuteAsync(object state)
internal override async Task PerformExecuteAsync(object state)
{
// Ensure we do not run if not main domain
if (_mainDom.IsMainDom == false)
@@ -45,6 +46,8 @@ namespace Umbraco.Infrastructure.HostedServices
{
CleanupFolder(folder);
}
return;
}
private void CleanupFolder(DirectoryInfo folder)

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
@@ -28,66 +29,66 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
private const string _check3Id = "00000000-0000-0000-0000-000000000003";
[Test]
public void Does_Not_Execute_When_Not_Enabled()
public async Task Does_Not_Execute_When_Not_Enabled()
{
var sut = CreateHealthCheckNotifier(enabled: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Does_Not_Execute_When_Runtime_State_Is_Not_Run()
public async Task Does_Not_Execute_When_Runtime_State_Is_Not_Run()
{
var sut = CreateHealthCheckNotifier(runtimeLevel: RuntimeLevel.Boot);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Replica()
public async Task Does_Not_Execute_When_Server_Role_Is_Replica()
{
var sut = CreateHealthCheckNotifier(serverRole: ServerRole.Replica);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Unknown()
public async Task Does_Not_Execute_When_Server_Role_Is_Unknown()
{
var sut = CreateHealthCheckNotifier(serverRole: ServerRole.Unknown);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Does_Not_Execute_When_Not_Main_Dom()
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
var sut = CreateHealthCheckNotifier(isMainDom: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Does_Not_Execute_With_No_Enabled_Notification_Methods()
public async Task Does_Not_Execute_With_No_Enabled_Notification_Methods()
{
var sut = CreateHealthCheckNotifier(notificationEnabled: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsNotSent();
}
[Test]
public void Executes_With_Enabled_Notification_Methods()
public async Task Executes_With_Enabled_Notification_Methods()
{
var sut = CreateHealthCheckNotifier();
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyNotificationsSent();
}
[Test]
public void Executes_Only_Enabled_Checks()
public async Task Executes_Only_Enabled_Checks()
{
var sut = CreateHealthCheckNotifier();
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
_mockNotificationMethod.Verify(x => x.SendAsync(It.Is<HealthCheckResults>(
y => y.ResultsAsDictionary.Count == 1 && y.ResultsAsDictionary.ContainsKey("Check1"))), Times.Once);
}

View File

@@ -26,42 +26,42 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
private const string _applicationUrl = "https://mysite.com";
[Test]
public void Does_Not_Execute_When_Not_Enabled()
public async Task Does_Not_Execute_When_Not_Enabled()
{
var sut = CreateKeepAlive(enabled: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyKeepAliveRequestNotSent();
}
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Replica()
public async Task Does_Not_Execute_When_Server_Role_Is_Replica()
{
var sut = CreateKeepAlive(serverRole: ServerRole.Replica);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyKeepAliveRequestNotSent();
}
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Unknown()
public async Task Does_Not_Execute_When_Server_Role_Is_Unknown()
{
var sut = CreateKeepAlive(serverRole: ServerRole.Unknown);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyKeepAliveRequestNotSent();
}
[Test]
public void Does_Not_Execute_When_Not_Main_Dom()
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
var sut = CreateKeepAlive(isMainDom: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyKeepAliveRequestNotSent();
}
[Test]
public void Executes_And_Calls_Ping_Url()
public async Task Executes_And_Calls_Ping_Url()
{
var sut = CreateKeepAlive();
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyKeepAliveRequestSent();
}

View File

@@ -1,10 +1,13 @@
using System;
using System.Data;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Configuration.Models;
using Umbraco.Core.Events;
using Umbraco.Core.Logging;
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
@@ -21,34 +24,34 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
const int _maxLogAgeInMinutes = 60;
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Replica()
public async Task Does_Not_Execute_When_Server_Role_Is_Replica()
{
var sut = CreateLogScrubber(serverRole: ServerRole.Replica);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyLogsNotScrubbed();
}
[Test]
public void Does_Not_Execute_When_Server_Role_Is_Unknown()
public async Task Does_Not_Execute_When_Server_Role_Is_Unknown()
{
var sut = CreateLogScrubber(serverRole: ServerRole.Unknown);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyLogsNotScrubbed();
}
[Test]
public void Does_Not_Execute_When_Not_Main_Dom()
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
var sut = CreateLogScrubber(isMainDom: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyLogsNotScrubbed();
}
[Test]
public void Executes_And_Scrubs_Logs()
public async Task Executes_And_Scrubs_Logs()
{
var sut = CreateLogScrubber();
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyLogsScrubbed();
}
@@ -67,7 +70,11 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
var mockMainDom = new Mock<IMainDom>();
mockMainDom.SetupGet(x => x.IsMainDom).Returns(isMainDom);
var mockScope = new Mock<IScope>();
var mockScopeProvider = new Mock<IScopeProvider>();
mockScopeProvider
.Setup(x => x.CreateScope(It.IsAny<IsolationLevel>(), It.IsAny<RepositoryCacheMode>(), It.IsAny<IEventDispatcher>(), It.IsAny<bool?>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Returns(mockScope.Object);
var mockLogger = new Mock<ILogger<LogScrubber>>();
var mockProfilingLogger = new Mock<IProfilingLogger>();

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
@@ -17,18 +18,18 @@ namespace Umbraco.Tests.UnitTests.Umbraco.Infrastructure.HostedServices
private string _testPath = @"c:\test\temp\path";
[Test]
public void Does_Not_Execute_When_Not_Main_Dom()
public async Task Does_Not_Execute_When_Not_Main_Dom()
{
var sut = CreateTempFileCleanup(isMainDom: false);
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyFilesNotCleaned();
}
[Test]
public void Executes_And_Cleans_Files()
public async Task Executes_And_Cleans_Files()
{
var sut = CreateTempFileCleanup();
sut.ExecuteAsync(null);
await sut.PerformExecuteAsync(null);
VerifyFilesCleaned();
}