From dc21e9ee8a9ea00a74bc6e3ee4365ede52a6284c Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Sun, 7 Mar 2021 19:35:36 +0100 Subject: [PATCH] Added integration tests for CacheInstructionRepository. --- .../ICacheInstructionRepository.cs | 2 +- .../Implement/CacheInstructionRepository.cs | 2 +- .../Implement/CacheInstructionService.cs | 2 +- .../CacheInstructionRepositoryTest.cs | 153 ++++++++++++++++++ 4 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs diff --git a/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs index 0381c74a03..e93f5829a1 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ICacheInstructionRepository.cs @@ -40,7 +40,7 @@ namespace Umbraco.Cms.Core.Persistence.Repositories /// /// Last id processed. /// The maximum number of instructions to retrieve. - IEnumerable GetInstructions(int lastId, int maxNumberToRetrieve); + IEnumerable GetPendingInstructions(int lastId, int maxNumberToRetrieve); /// /// Deletes cache instructions older than the provided date. diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs index f5452b53c0..d0e025e06e 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/CacheInstructionRepository.cs @@ -51,7 +51,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement } /// - public IEnumerable GetInstructions(int lastId, int maxNumberToRetrieve) + public IEnumerable GetPendingInstructions(int lastId, int maxNumberToRetrieve) { Sql sql = AmbientScope.SqlContext.Sql().SelectAll() .From() diff --git a/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs b/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs index 6ca01ccb50..a2b400293a 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/CacheInstructionService.cs @@ -248,7 +248,7 @@ namespace Umbraco.Cms.Core.Services.Implement // some memory however we cannot do that because inside of this loop the cache refreshers are also // performing some lookups which cannot be done with an active reader open. lastId = 0; - foreach (CacheInstruction instruction in _cacheInstructionRepository.GetInstructions(lastId, MaxInstructionsToRetrieve)) + foreach (CacheInstruction instruction in _cacheInstructionRepository.GetPendingInstructions(lastId, MaxInstructionsToRetrieve)) { // If this flag gets set it means we're shutting down! In this case, we need to exit asap and cannot // continue processing anything otherwise we'll hold up the app domain shutdown. diff --git a/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs new file mode 100644 index 0000000000..faa5fd9d6e --- /dev/null +++ b/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/CacheInstructionRepositoryTest.cs @@ -0,0 +1,153 @@ +// Copyright (c) Umbraco. +// See LICENSE for more details. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Extensions.Logging; +using NUnit.Framework; +using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Persistence.Querying; +using Umbraco.Cms.Core.Scoping; +using Umbraco.Cms.Infrastructure.Persistence; +using Umbraco.Cms.Infrastructure.Persistence.Dtos; +using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; +using Umbraco.Cms.Tests.Common.Testing; +using Umbraco.Cms.Tests.Integration.Testing; + +namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories +{ + [TestFixture] + [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)] + public class CacheInstructionRepositoryTest : UmbracoIntegrationTest + { + private const string OriginIdentiy = "Test1"; + private const string Instructions = "{}"; + private readonly DateTime _date = new DateTime(2021, 7, 3, 10, 30, 0); + private const int InstructionCount = 1; + + [Test] + public void Can_Count_All() + { + const int Count = 5; + + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + for (var i = 0; i < Count; i++) + { + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + } + + var count = repo.CountAll(); + + Assert.That(count, Is.EqualTo(Count)); + } + } + + [Test] + public void Can_Count_Pending_Instructions() + { + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + for (var i = 0; i < 5; i++) + { + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + } + + var count = repo.CountPendingInstructions(2); + + Assert.That(count, Is.EqualTo(3)); + } + } + + [Test] + public void Can_Check_Exists() + { + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + + var existsBefore = repo.Exists(1); + + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + + var existsAfter = repo.Exists(1); + + Assert.That(existsBefore, Is.False); + Assert.That(existsAfter, Is.True); + } + } + + [Test] + public void Can_Add_Cache_Instruction() + { + const string OriginIdentiy = "Test1"; + const string Instructions = "{}"; + var date = new DateTime(2021, 7, 3, 10, 30, 0); + const int InstructionCount = 1; + + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); + + List dtos = scope.Database.Fetch("WHERE id > -1"); + + Assert.That(dtos.Any(), Is.True); + + CacheInstructionDto dto = dtos.First(); + Assert.That(dto.UtcStamp, Is.EqualTo(date)); + Assert.That(dto.Instructions, Is.EqualTo(Instructions)); + Assert.That(dto.OriginIdentity, Is.EqualTo(OriginIdentiy)); + Assert.That(dto.InstructionCount, Is.EqualTo(InstructionCount)); + } + } + + [Test] + public void Can_Get_Pending_Instructions() + { + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + for (var i = 0; i < 5; i++) + { + repo.Add(new CacheInstruction(0, _date, Instructions, OriginIdentiy, InstructionCount)); + } + + IEnumerable instructions = repo.GetPendingInstructions(2, 2); + + Assert.That(instructions.Count(), Is.EqualTo(2)); + + Assert.That(string.Join(",", instructions.Select(x => x.Id)), Is.EqualTo("3,4")); + } + } + + [Test] + public void Can_Delete_Old_Instructions() + { + IScopeProvider sp = ScopeProvider; + using (IScope scope = ScopeProvider.CreateScope()) + { + var repo = new CacheInstructionRepository((IScopeAccessor)sp); + for (var i = 0; i < 5; i++) + { + DateTime date = i == 0 ? DateTime.UtcNow.AddDays(-2) : DateTime.UtcNow; + repo.Add(new CacheInstruction(0, date, Instructions, OriginIdentiy, InstructionCount)); + } + + repo.DeleteInstructionsOlderThan(DateTime.UtcNow.AddDays(-1)); + + var count = repo.CountAll(); + Assert.That(count, Is.EqualTo(4)); // 5 have been added, 1 is older and deleted. + } + } + } +}