Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/CacheInstructionServiceTests.cs
Andy Butland 825f791d01 Remove the non-controversial, straightforward obsoleted constructs for Umbraco 16 (#18661)
* Removed obsoletes from IConfigManipulator.

* Removed obsolete models builder extensions.

* Removed the obsolete ContentDashboardSettings.

* Removed the obsolete InstallMissingDatabase setting on GlobalSettings.

* Removed obsolete NuCache settings.

* Removed obsolete RuntimeMinificationSettings.

* Removed obsolete health check constant.

* Removed obsolete icon constant.

* Removed obsolete telemetry constant.

* Removed obsolete property and constructor on UmbracoBuilder.

* Removed obsolete constructor on AuditNotificationsHandler.

* Removed obsolete constructor on HTTP header health checks.

* Removed obsolete constructor on MediaFileManager.

* Removed obsolete GetDefaultFileContent on ViewHelper.

* Remove obsoleted methods on embed providers.

* Fix tests.

* Removed obsolete constructors on BlockEditorDataConverter.

* Removed obsolete SeedCacheDuration property on CacheSettings.

* Removed obsolete PublishCulture on ContentRepositoryExtensions.

* Removed obsolete MonitorLock.

* Removed obsolete synchronous HasSavedValues from IDataTypeUsageService and IDataTypeUsageRepository.

* Removed obsolete HasSavedPropertyValues from IPropertyTypeUsageService and IPropertyTypeUsageRepository.

* Removed obsolete methods in ITrackedReferencesService and ITrackedReferencesRepository.

* Removed obsolete DateValueEditor constructors.

* Removed obsolete GetAutomaticRelationTypesAliases.

* Removed obsolete constructor on TextOnlyValueEditor.

* Removed obsolete constructors on RegexValidator and RequiredValidator.

* Removed obsolete constructs on SliderValueConverter and TagsValueConverter.

* Removed obsolete GetContentType methods from IPublishedCache.

* Removed ContentFinderByIdPath.

* Removed obsolete constructor on DefaultMediaUrlProvider.

* Removed obsolete constructor on Domain.

* Removed obsolete constructor on PublishedRequest.

* Removed obsolete methods on CheckPermissions.

* Removed obsolete GetUserId from IBackOfficeSecurity.

* Removed obsolete methods on LegacyPasswordSecurity.

* Removed obsolete constructors on AuditService.

* Removed obsolete methods on IContentEditingService.

* Remove obsolete constructors and methods on ContentService/IContentService.

* Removed obsolete constructor in ContentTypeEditingService.

* Removed obsolete constructor in MediaTypeEditingService.

* Removed obsolete constructor in MemberTypeEditingService.

* Removed obsolete constructor in ContentTypeService.

* Removed obsolete constructors in ContentTypeServiceBase.

* Removed obsolete constructors and methods in ContentVersionService.

* Removed obsolete constructor in DataTypeUsageService.

* Removed obsolete constructor in DomainService.

* Removed obsolete constructor in FileService.

* Removes obsolete AttemptMove from IContentService.

* Removes obsolete SetPreventCleanup from IContentVersionService.

* Removes obsolete GetReferences from IDataTypeService.

* Removed obsolete SetConsentLevel from IMetricsConsentService.

* Removed obsolete methods from IPackageDataInstallation.

* Removed obsolete methods from IPackagingService.

* Removed obsolete methods on ITwoFactorLoginService.
Removed obsolete ITemporaryMediaService.

* Removed obsolete constructor from MediaService, MemberTypeService and MediaTypeService.

* More obsolete constructors.

* Removed obsoleted overloads on IPropertyValidationService.

* Fixed build for tests.

* Removed obsolete constructor for PublicAccessService, UserService and RelationService.

* Removed GetDefaultMemberType.

* Removed obsolete user group functionality from IUserService.

* Removed obsolete extension methods on IUserService.

* Removed obsolete method from ITelemetryService.

* Removed obsolete UdiParserServiceConnectors.

* Removed obsolete method on ICookieManager.

* Removed obsolete DynamicContext.

* Removed obsolete XmlHelper.

* Fixed failing integration tests.

* Removed obsoletes in Umbraco.Cms.Api.Common

* Removed obsoletes in Umbraco.Cms.Api.Delivery

* Removed obsoletes in Umbraco.Cms.Api.Management

* Removed obsoletes in Umbraco.Examine.Lucene

* Removed obsoletes in Umbraco.Infrastructure

* Fix failing delivery API contract integration test.

* Made integration tests internal.

* Removed obsoletes from web projects.

* Fix build.

* Removed Twitter OEmbed provider

* Removed obsolete constructor on PublishedDataType.

* Removed obsolete constructors on PublishedCacheBase.

* Removed the obsolete PropertyEditorTagsExtensions.

* Removed obsoletion properties on configuration response  models (#18697)

* Removed obsolete methods from server-side models.

* Update client-side types and sdk.

* Update client-side files.

* Removed obsoletion of Utf8ToAsciiConverter.ToAsciiString overload. (#18694)

* Removed obsolete method in UserService. (#18710)

* Removed obsoleted group alias keys from being publicly available. (#18682)

* Removed unneceessary ApiVersion attribute.

* Clean-up obsoletions on MemberService (#18703)

* Removed obsoleted method on MemberService, added future obsoletion to interface and updated all callers.

* Removed obsoletion on member service method that's not obsolete on the interface.
2025-03-21 17:02:31 +00:00

316 lines
12 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Sync;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Infrastructure.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
internal sealed class CacheInstructionServiceTests : UmbracoIntegrationTest
{
private const string LocalIdentity = "localIdentity";
private const string AlternateIdentity = "alternateIdentity";
private CancellationToken CancellationToken => new();
private CacheRefresherCollection CacheRefreshers => GetRequiredService<CacheRefresherCollection>();
private IServerRoleAccessor ServerRoleAccessor => GetRequiredService<IServerRoleAccessor>();
[Test]
public void Confirms_Cold_Boot_Required_When_Instructions_Exist_And_None_Have_Been_Synced()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, LocalIdentity);
var result = sut.IsColdBootRequired(0);
Assert.IsTrue(result);
}
[Test]
public void Confirms_Cold_Boot_Required_When_Last_Synced_Instruction_Not_Found()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, LocalIdentity); // will create with Id = 1
var result = sut.IsColdBootRequired(2);
Assert.IsTrue(result);
}
[Test]
public void Confirms_Cold_Boot_Not_Required_When_Last_Synced_Instruction_Found()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, LocalIdentity); // will create with Id = 1
var result = sut.IsColdBootRequired(1);
Assert.IsFalse(result);
}
[TestCase(1, 10, false, 4)]
[TestCase(1, 3, true, 4)]
public void Confirms_Instruction_Count_Over_Limit(int lastId, int limit, bool expectedResult, int expectedCount)
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut); // 3 records, each with 2 instructions = 6.
var result = sut.IsInstructionCountOverLimit(lastId, limit, out var count);
Assert.AreEqual(expectedResult, result);
Assert.AreEqual(expectedCount, count);
}
[Test]
public void Can_Get_Max_Instruction_Id()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var result = sut.GetMaxInstructionId();
Assert.AreEqual(3, result);
}
[Test]
public void Can_Deliver_Instructions()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, LocalIdentity);
AssertDeliveredInstructions();
}
[Test]
public void Can_Deliver_Instructions_In_Batches()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
var instructions = CreateInstructions();
sut.DeliverInstructionsInBatches(instructions, LocalIdentity);
AssertDeliveredInstructions();
}
private List<RefreshInstruction> CreateInstructions() => new()
{
new(UserCacheRefresher.UniqueId, RefreshMethodType.RefreshByIds, Guid.Empty, 0, "[-1]", null),
new(UserCacheRefresher.UniqueId, RefreshMethodType.RefreshByIds, Guid.Empty, 0, "[-1]", null)
};
private void AssertDeliveredInstructions()
{
List<CacheInstructionDto> cacheInstructions;
var sqlContext = GetRequiredService<ISqlContext>();
var sql = sqlContext.Sql()
.Select<CacheInstructionDto>()
.From<CacheInstructionDto>();
using (var scope = ScopeProvider.CreateScope())
{
cacheInstructions = ScopeAccessor.AmbientScope.Database.Fetch<CacheInstructionDto>(sql);
scope.Complete();
}
Assert.Multiple(() =>
{
Assert.AreEqual(1, cacheInstructions.Count);
var firstInstruction = cacheInstructions.First();
Assert.AreEqual(2, firstInstruction.InstructionCount);
Assert.AreEqual(LocalIdentity, firstInstruction.OriginIdentity);
});
}
[Test]
public void Can_Process_Instructions()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
// Create three instruction records, each with two instructions. First two records are for a different identity.
CreateAndDeliveryMultipleInstructions(sut);
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole, CancellationToken,
LocalIdentity, DateTime.UtcNow.AddSeconds(-1), -1);
Assert.Multiple(() =>
{
Assert.AreEqual(3, result.LastId); // 3 records found.
Assert.AreEqual(2,
result.NumberOfInstructionsProcessed); // 2 records processed (as one is for the same identity).
Assert.IsFalse(result.InstructionsWerePruned);
});
}
[Test]
public void Can_Process_And_Purge_Instructions()
{
// Purging of instructions only occurs on single or master servers, so we need to ensure this is set before running the test.
EnsureServerRegistered();
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole, CancellationToken,
LocalIdentity, DateTime.UtcNow.AddHours(-1), -1);
Assert.IsTrue(result.InstructionsWerePruned);
}
[Test]
public void Processes_No_Instructions_When_CancellationToken_is_Cancelled()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole,
cancellationTokenSource.Token, LocalIdentity, DateTime.UtcNow.AddSeconds(-1), -1);
Assert.Multiple(() =>
{
Assert.AreEqual(0, result.LastId);
Assert.AreEqual(0, result.NumberOfInstructionsProcessed);
Assert.IsFalse(result.InstructionsWerePruned);
});
}
[Test]
public void Processes_Instructions_Only_Once()
{
// This test shows what's happening in issue #10112
// The DatabaseServerMessenger will run its sync operation every five seconds which calls CacheInstructionService.ProcessInstructions,
// which is why the CacheRefresherNotification keeps dispatching, because the cache instructions gets constantly processed.
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var lastId = -1;
// Run once
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole, CancellationToken,
LocalIdentity, DateTime.UtcNow.AddSeconds(-1), lastId);
Assert.Multiple(() =>
{
Assert.AreEqual(3, result.LastId); // 3 records found.
Assert.AreEqual(2, result.NumberOfInstructionsProcessed); // 2 records processed (as one is for the same identity).
Assert.IsFalse(result.InstructionsWerePruned);
});
// DatabaseServerMessenger stores the LastID after ProcessInstructions has been run.
lastId = result.LastId;
// The instructions has now been processed and shouldn't be processed on the next call...
// Run again.
var secondResult = sut.ProcessInstructions(
CacheRefreshers,
ServerRoleAccessor.CurrentServerRole,
CancellationToken,
LocalIdentity,
DateTime.UtcNow.AddSeconds(-1),
lastId);
Assert.Multiple(() =>
{
Assert.AreEqual(
0,
secondResult
.LastId); // No instructions was processed so LastId is 0, this is consistent with behavior from V8
Assert.AreEqual(0, secondResult.NumberOfInstructionsProcessed); // Nothing was processed.
Assert.IsFalse(secondResult.InstructionsWerePruned);
});
}
[Test]
public void Processes_New_Instructions()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var lastId = -1;
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole, CancellationToken,
LocalIdentity, DateTime.UtcNow.AddSeconds(-1), lastId);
Assert.AreEqual(3, result.LastId); // Make sure LastId is 3, the rest is tested in other test.
lastId = result.LastId;
// Add new instruction
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, AlternateIdentity);
var secondResult = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole,
CancellationToken, LocalIdentity, DateTime.UtcNow.AddSeconds(-1), lastId);
Assert.Multiple(() =>
{
Assert.AreEqual(4, secondResult.LastId);
Assert.AreEqual(1, secondResult.NumberOfInstructionsProcessed);
Assert.IsFalse(secondResult.InstructionsWerePruned);
});
}
[Test]
public void Correct_ID_For_Instruction_With_Same_Identity()
{
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
CreateAndDeliveryMultipleInstructions(sut);
var lastId = -1;
var result = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole, CancellationToken,
LocalIdentity, DateTime.UtcNow.AddSeconds(-1), lastId);
Assert.AreEqual(3, result.LastId); // Make sure LastId is 3, the rest is tested in other test.
lastId = result.LastId;
// Add new instruction
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, LocalIdentity);
var secondResult = sut.ProcessInstructions(CacheRefreshers, ServerRoleAccessor.CurrentServerRole,
CancellationToken, LocalIdentity, DateTime.UtcNow.AddSeconds(-1), lastId);
Assert.Multiple(() =>
{
Assert.AreEqual(4, secondResult.LastId);
Assert.AreEqual(0, secondResult.NumberOfInstructionsProcessed);
Assert.IsFalse(secondResult.InstructionsWerePruned);
});
}
private void CreateAndDeliveryMultipleInstructions(CacheInstructionService sut)
{
for (var i = 0; i < 3; i++)
{
var instructions = CreateInstructions();
sut.DeliverInstructions(instructions, i == 2 ? LocalIdentity : AlternateIdentity);
}
}
private void EnsureServerRegistered()
{
var serverRegistrationService = GetRequiredService<IServerRegistrationService>();
serverRegistrationService.TouchServer("http://localhost", TimeSpan.FromMinutes(10));
}
}