Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/PublicAccessRepositoryTest.cs
Mole d9f8faf509 Load balancing: Load balance isolated caches to allow the backoffice to be load balanced (#20417)
* V16:  Cache Version Mechanism (#19747)

* Add RepositoryCacheVersion table

* Add repository

* Add Cache version lock

* Add GetAll method to repository

* Add RepositoryCacheVersionService

* Remember to add lock in data creator

* Work my way out of constructor hell

This is why we use DI folks. 🤦

* Add checks to specific cache policies

* Fix migration

* Add to schema creator

* Fix database access

* Initialize the cache version on in memory miss

* Make cache version service internal

* Add tests

* Apply suggestions from code review

Co-authored-by: Andy Butland <abutland73@gmail.com>

* Add missing obsoletions

* Prefer full name

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>

* fixed merge

* V16/feature/move last synced id to db (#19884)

* Foundation work for moving last synced id

* register manager and repo in dependency injection

* Fixing to make tests work

* Replacing the use of the old LastSyncedFileManager.cs with the new LastSyncedManager.cs

* Testing to delete out of sync id and old entries

* changing some stuff to please the reviewer.

* Inverted saving methods id check and fixed documentation mishaps

* Loadbalancing: Add Cache Sync service to allow us to roll forward isolated caches when backoffice is load balanced. (#20398)

* Split cache refreshers into internal and external caches

* Add obsolete constructor for CacheInstructionsPruningJob

* Add xml docs

* Move lastID management into CacheInstructionService

* Cache last synced ids in memory

* Lock when processing instructions

* Sync caches when out of sync

* Fix constructors for ICacheSyncService

* Cache version on request

* Register caches as synced when instructions are processed

* Rename CacheVersionAccessor to IRepositoryCacheVersionAccessor

* Set caches as synced before actually syncing the caches

* Set caches as synced before syncing, within scope, this should also lock the cache version from being written to whilst updating caches

* Only check version for backoffice requests

* Clear request cache when caches are syned

* Default to using NOOP cache version service

* Don't generate local identity in database server messenger anymore

* Fix ambiguous constructor

* Add helper method to switch to load balanced isolated caches

* Fix LastSyncedManagerTests

* Fix RepositoryCacheVersionServiceTests

* Fix DefaultCachePolicyTests

* Use correct constructor in FullDataSetRepositoryCachePolicy

* Minor cleanup

* Add XML docs

* Add more xml docs

* Apply suggestions from code review

Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

---------

Co-authored-by: Zeegaan <skrivdetud@gmail.com>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>

* Fix migration plan

* fix tests

* Fix integration tests

* Fix changes from github review

* Move premigrations to v17

* Make lock constantws sequential

* Fix comment

* Make IRepositoryCacheVersionService and ICacheSyncService protected on EntityRepositoryBase

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
Co-authored-by: Nicklas Kramer <nik@umbraco.dk>
Co-authored-by: NillasKA <kramernicklas@gmail.com>
Co-authored-by: Zeegaan <skrivdetud@gmail.com>
Co-authored-by: Nikolaj Geisle <70372949+Zeegaan@users.noreply.github.com>
2025-10-08 14:55:50 +02:00

275 lines
11 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
using Umbraco.Cms.Infrastructure.Scoping;
using Umbraco.Cms.Tests.Common.Attributes;
using Umbraco.Cms.Tests.Common.Builders;
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)]
internal sealed class PublicAccessRepositoryTest : UmbracoIntegrationTest
{
private IContentTypeRepository ContentTypeRepository => GetRequiredService<IContentTypeRepository>();
private DocumentRepository DocumentRepository => (DocumentRepository)GetRequiredService<IDocumentRepository>();
[Test]
public void Can_Delete()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry = new PublicAccessEntry(content[0], content[1], content[2], rules);
repo.Save(entry);
repo.Delete(entry);
entry = repo.Get(entry.Key);
Assert.IsNull(entry);
}
}
[Test]
public void Can_Add()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry = new PublicAccessEntry(content[0], content[1], content[2], rules);
repo.Save(entry);
var found = repo.GetMany().ToArray();
Assert.AreEqual(1, found.Length);
Assert.AreEqual(content[0].Id, found[0].ProtectedNodeId);
Assert.AreEqual(content[1].Id, found[0].LoginNodeId);
Assert.AreEqual(content[2].Id, found[0].NoAccessNodeId);
Assert.IsTrue(found[0].HasIdentity);
Assert.AreNotEqual(default(DateTime), found[0].CreateDate);
Assert.AreNotEqual(default(DateTime), found[0].UpdateDate);
Assert.AreEqual(1, found[0].Rules.Count());
Assert.AreEqual("test", found[0].Rules.First().RuleValue);
Assert.AreEqual("RoleName", found[0].Rules.First().RuleType);
Assert.AreNotEqual(default(DateTime), found[0].Rules.First().CreateDate);
Assert.AreNotEqual(default(DateTime), found[0].Rules.First().UpdateDate);
Assert.IsTrue(found[0].Rules.First().HasIdentity);
}
}
[Test]
[LongRunning]
public void Can_Add2()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
ScopeAccessor.AmbientScope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules =
{
new PublicAccessRule {RuleValue = "test", RuleType = "RoleName"},
new PublicAccessRule {RuleValue = "test2", RuleType = "RoleName2"}
};
var entry = new PublicAccessEntry(content[0], content[1], content[2], rules);
repo.Save(entry);
var found = repo.GetMany().ToArray();
Assert.AreEqual(1, found.Length);
Assert.AreEqual(content[0].Id, found[0].ProtectedNodeId);
Assert.AreEqual(content[1].Id, found[0].LoginNodeId);
Assert.AreEqual(content[2].Id, found[0].NoAccessNodeId);
Assert.IsTrue(found[0].HasIdentity);
Assert.AreNotEqual(default(DateTime), found[0].CreateDate);
Assert.AreNotEqual(default(DateTime), found[0].UpdateDate);
CollectionAssert.AreEquivalent(found[0].Rules, entry.Rules);
Assert.AreNotEqual(default(DateTime), found[0].Rules.First().CreateDate);
Assert.AreNotEqual(default(DateTime), found[0].Rules.First().UpdateDate);
Assert.IsTrue(found[0].Rules.First().HasIdentity);
}
}
[Test]
public void Can_Update()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry = new PublicAccessEntry(content[0], content[1], content[2], rules);
repo.Save(entry);
// re-get
entry = repo.Get(entry.Key);
entry.Rules.First().RuleValue = "blah";
entry.Rules.First().RuleType = "asdf";
repo.Save(entry);
// re-get
entry = repo.Get(entry.Key);
Assert.AreEqual("blah", entry.Rules.First().RuleValue);
Assert.AreEqual("asdf", entry.Rules.First().RuleType);
}
}
[Test]
public void Get_By_Id()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(),Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry = new PublicAccessEntry(content[0], content[1], content[2], rules);
repo.Save(entry);
// re-get
entry = repo.Get(entry.Key);
Assert.IsNotNull(entry);
}
}
[Test]
public void Get_All()
{
var content = CreateTestData(30).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
var allEntries = new List<PublicAccessEntry>();
for (var i = 0; i < 10; i++)
{
var rules = new List<PublicAccessRule>();
for (var j = 0; j < 50; j++)
{
rules.Add(new PublicAccessRule { RuleValue = "test" + j, RuleType = "RoleName" + j });
}
var entry1 = new PublicAccessEntry(content[i], content[i + 1], content[i + 2], rules);
repo.Save(entry1);
allEntries.Add(entry1);
}
// now remove a few rules from a few of the items and then add some more, this will put things 'out of order' which
// we need to verify our sort order is working for the relator
// TODO: no "relator" in v8?!
for (var i = 0; i < allEntries.Count; i++)
{
// all the even ones
if (i % 2 == 0)
{
var rules = allEntries[i].Rules.ToArray();
for (var j = 0; j < rules.Length; j++)
{
// all the even ones
if (j % 2 == 0)
{
allEntries[i].RemoveRule(rules[j]);
}
}
allEntries[i].AddRule("newrule" + i, "newrule" + i);
repo.Save(allEntries[i]);
}
}
var found = repo.GetMany().ToArray();
Assert.AreEqual(10, found.Length);
foreach (var publicAccessEntry in found)
{
var matched = allEntries.First(x => x.Key == publicAccessEntry.Key);
Assert.AreEqual(matched.Rules.Count(), publicAccessEntry.Rules.Count());
}
}
}
[Test]
public void Get_All_With_Id()
{
var content = CreateTestData(3).ToArray();
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repo = new PublicAccessRepository((IScopeAccessor)provider, AppCaches, LoggerFactory.CreateLogger<PublicAccessRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
PublicAccessRule[] rules1 = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry1 = new PublicAccessEntry(content[0], content[1], content[2], rules1);
repo.Save(entry1);
PublicAccessRule[] rules2 = { new PublicAccessRule { RuleValue = "test", RuleType = "RoleName" } };
var entry2 = new PublicAccessEntry(content[1], content[0], content[2], rules2);
repo.Save(entry2);
var found = repo.GetMany(entry1.Key).ToArray();
Assert.AreEqual(1, found.Count());
}
}
private IEnumerable<IContent> CreateTestData(int count)
{
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var ct = ContentTypeBuilder.CreateBasicContentType("testing");
ContentTypeRepository.Save(ct);
var result = new List<IContent>();
for (var i = 0; i < count; i++)
{
var c = new Content("test" + i, -1, ct);
DocumentRepository.Save(c);
result.Add(c);
}
scope.Complete();
return result;
}
}
}