Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/HostedServices/ServerRegistration/TouchServerTaskTests.cs
Andrew McKaskill 04ac1542aa Refactor hostedServices into background jobs (#14291)
* Refactor jobs from HostedServices into BackgroundJobs

* Clean up generics and DI setup

* Add RecurringBackgroundJob Unit Tests

* Add ServiceCollection helper

* Add Obsolete attributes

* Add Notification Classes

* Add UnitTests for RecurringBackgroundJob HostedService

* Add NotificationEvents

* Add state to notifications

* Update UnitTests

* Add Obsolete Attributes to old hosted service classes

* Updated xmldoc in IRecurringBackgroundJob.cs

* Update Obsolete attribute messages to indicate classes will be removed in Umbraco 14

(cherry picked from commit c30ffa9ac3)
2023-11-13 11:10:58 +01:00

108 lines
3.7 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.HostedServices.ServerRegistration;
using Umbraco.Cms.Tests.Common;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.HostedServices.ServerRegistration;
[TestFixture]
[Obsolete("Replaced by BackgroundJobs.Jobs.ServerRegistration.TouchServerJobTests")]
public class TouchServerTaskTests
{
private Mock<IServerRegistrationService> _mockServerRegistrationService;
private const string ApplicationUrl = "https://mysite.com/";
private readonly TimeSpan _staleServerTimeout = TimeSpan.FromMinutes(2);
[TestCase(RuntimeLevel.Boot)]
[TestCase(RuntimeLevel.Install)]
[TestCase(RuntimeLevel.Unknown)]
[TestCase(RuntimeLevel.Upgrade)]
[TestCase(RuntimeLevel.BootFailed)]
public async Task Does_Not_Execute_When_Runtime_State_Is_Not_Run(RuntimeLevel runtimeLevel)
{
var sut = CreateTouchServerTask(runtimeLevel);
await sut.PerformExecuteAsync(null);
VerifyServerNotTouched();
}
[Test]
public async Task Does_Not_Execute_When_Application_Url_Is_Not_Available()
{
var sut = CreateTouchServerTask(applicationUrl: string.Empty);
await sut.PerformExecuteAsync(null);
VerifyServerNotTouched();
}
[Test]
public async Task Executes_And_Touches_Server()
{
var sut = CreateTouchServerTask();
await sut.PerformExecuteAsync(null);
VerifyServerTouched();
}
[Test]
public async Task Does_Not_Execute_When_Role_Accessor_Is_Not_Elected()
{
var sut = CreateTouchServerTask(useElection: false);
await sut.PerformExecuteAsync(null);
VerifyServerNotTouched();
}
private TouchServerTask CreateTouchServerTask(
RuntimeLevel runtimeLevel = RuntimeLevel.Run,
string applicationUrl = ApplicationUrl,
bool useElection = true)
{
var mockRequestAccessor = new Mock<IHostingEnvironment>();
mockRequestAccessor.SetupGet(x => x.ApplicationMainUrl)
.Returns(!string.IsNullOrEmpty(applicationUrl) ? new Uri(ApplicationUrl) : null);
var mockRunTimeState = new Mock<IRuntimeState>();
mockRunTimeState.SetupGet(x => x.Level).Returns(runtimeLevel);
var mockLogger = new Mock<ILogger<TouchServerTask>>();
_mockServerRegistrationService = new Mock<IServerRegistrationService>();
var settings = new GlobalSettings
{
DatabaseServerRegistrar = new DatabaseServerRegistrarSettings { StaleServerTimeout = _staleServerTimeout },
};
IServerRoleAccessor roleAccessor = useElection
? new ElectedServerRoleAccessor(_mockServerRegistrationService.Object)
: new SingleServerRoleAccessor();
return new TouchServerTask(
mockRunTimeState.Object,
_mockServerRegistrationService.Object,
mockRequestAccessor.Object,
mockLogger.Object,
new TestOptionsMonitor<GlobalSettings>(settings),
roleAccessor);
}
private void VerifyServerNotTouched() => VerifyServerTouchedTimes(Times.Never());
private void VerifyServerTouched() => VerifyServerTouchedTimes(Times.Once());
private void VerifyServerTouchedTimes(Times times) => _mockServerRegistrationService
.Verify(
x => x.TouchServer(
It.Is<string>(y => y == ApplicationUrl),
It.Is<TimeSpan>(y => y == _staleServerTimeout)),
times);
}