2021-03-08 17:13:37 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NPoco;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Scoping;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Core.Services.Implement;
|
|
|
|
|
using Umbraco.Cms.Core.Sync;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
|
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
|
|
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
|
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
|
|
|
|
using Umbraco.Extensions;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
|
|
|
|
public class CacheInstructionServiceTests : UmbracoIntegrationTest
|
|
|
|
|
{
|
|
|
|
|
private const string LocalIdentity = "localIdentity";
|
|
|
|
|
private const string AlternateIdentity = "alternateIdentity";
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-03-09 18:00:31 +01:00
|
|
|
public void Confirms_Cold_Boot_Required_When_Instructions_Exist_And_None_Have_Been_Synced()
|
2021-03-08 17:13:37 +01:00
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
|
|
|
|
sut.DeliverInstructions(instructions, LocalIdentity);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
var result = sut.IsColdBootRequired(0);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(result);
|
2021-03-08 17:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 17:39:41 +01:00
|
|
|
[Test]
|
2021-03-09 18:00:31 +01:00
|
|
|
public void Confirms_Cold_Boot_Required_When_Last_Synced_Instruction_Not_Found()
|
2021-03-08 17:39:41 +01:00
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
|
|
|
|
sut.DeliverInstructions(instructions, LocalIdentity); // will create with Id = 1
|
|
|
|
|
|
|
|
|
|
var result = sut.IsColdBootRequired(2);
|
2021-03-08 17:39:41 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
Assert.IsTrue(result);
|
2021-03-08 17:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 17:13:37 +01:00
|
|
|
[Test]
|
2021-03-09 18:00:31 +01:00
|
|
|
public void Confirms_Cold_Boot_Not_Required_When_Last_Synced_Instruction_Found()
|
2021-03-08 17:13:37 +01:00
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
|
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
2021-03-09 18:00:31 +01:00
|
|
|
sut.DeliverInstructions(instructions, LocalIdentity); // will create with Id = 1
|
2021-03-08 17:13:37 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
var result = sut.IsColdBootRequired(1);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
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 int count);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(expectedResult, result);
|
|
|
|
|
Assert.AreEqual(expectedCount, count);
|
2021-03-08 17:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2021-03-09 18:00:31 +01:00
|
|
|
public void Can_Get_Max_Instruction_Id()
|
2021-03-08 17:13:37 +01:00
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
CreateAndDeliveryMultipleInstructions(sut);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
var result = sut.GetMaxInstructionId();
|
2021-03-08 17:13:37 +01:00
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
Assert.AreEqual(3, result);
|
2021-03-08 17:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Deliver_Instructions()
|
|
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
|
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
|
|
|
|
|
|
|
|
|
sut.DeliverInstructions(instructions, LocalIdentity);
|
|
|
|
|
|
|
|
|
|
AssertDeliveredInstructions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Can_Deliver_Instructions_In_Batches()
|
|
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
|
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
|
|
|
|
|
|
|
|
|
sut.DeliverInstructionsInBatches(instructions, LocalIdentity);
|
|
|
|
|
|
|
|
|
|
AssertDeliveredInstructions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<RefreshInstruction> CreateInstructions() => new List<RefreshInstruction>
|
|
|
|
|
{
|
|
|
|
|
new RefreshInstruction(UserCacheRefresher.UniqueId, RefreshMethodType.RefreshByIds, Guid.Empty, 0, "[-1]", null),
|
|
|
|
|
new RefreshInstruction(UserCacheRefresher.UniqueId, RefreshMethodType.RefreshByIds, Guid.Empty, 0, "[-1]", null),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private void AssertDeliveredInstructions()
|
|
|
|
|
{
|
|
|
|
|
List<CacheInstructionDto> cacheInstructions;
|
|
|
|
|
ISqlContext sqlContext = GetRequiredService<ISqlContext>();
|
|
|
|
|
Sql<ISqlContext> sql = sqlContext.Sql()
|
|
|
|
|
.Select<CacheInstructionDto>()
|
|
|
|
|
.From<CacheInstructionDto>();
|
|
|
|
|
using (IScope scope = ScopeProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
cacheInstructions = scope.Database.Fetch<CacheInstructionDto>(sql);
|
|
|
|
|
scope.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(1, cacheInstructions.Count);
|
|
|
|
|
|
|
|
|
|
CacheInstructionDto 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.
|
2021-03-09 18:00:31 +01:00
|
|
|
CreateAndDeliveryMultipleInstructions(sut);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
|
|
|
|
CacheInstructionServiceProcessInstructionsResult result = sut.ProcessInstructions(false, LocalIdentity, DateTime.UtcNow.AddSeconds(-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>();
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
CreateAndDeliveryMultipleInstructions(sut);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
|
|
|
|
CacheInstructionServiceProcessInstructionsResult result = sut.ProcessInstructions(false, LocalIdentity, DateTime.UtcNow.AddHours(-1));
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(result.InstructionsWerePruned);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Processes_No_Instructions_When_Released()
|
|
|
|
|
{
|
|
|
|
|
var sut = (CacheInstructionService)GetRequiredService<ICacheInstructionService>();
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
CreateAndDeliveryMultipleInstructions(sut);
|
2021-03-08 17:13:37 +01:00
|
|
|
|
|
|
|
|
CacheInstructionServiceProcessInstructionsResult result = sut.ProcessInstructions(true, LocalIdentity, DateTime.UtcNow.AddSeconds(-1));
|
|
|
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(0, result.LastId);
|
|
|
|
|
Assert.AreEqual(0, result.NumberOfInstructionsProcessed);
|
|
|
|
|
Assert.IsFalse(result.InstructionsWerePruned);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-09 18:00:31 +01:00
|
|
|
private void CreateAndDeliveryMultipleInstructions(CacheInstructionService sut)
|
2021-03-08 17:13:37 +01:00
|
|
|
{
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
|
{
|
|
|
|
|
List<RefreshInstruction> instructions = CreateInstructions();
|
|
|
|
|
sut.DeliverInstructions(instructions, i == 2 ? LocalIdentity : AlternateIdentity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureServerRegistered()
|
|
|
|
|
{
|
|
|
|
|
IServerRegistrationService serverRegistrationService = GetRequiredService<IServerRegistrationService>();
|
|
|
|
|
serverRegistrationService.TouchServer("http://localhost", TimeSpan.FromMinutes(10));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|