Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/ServerRegistrationRepositoryTest.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

248 lines
7.2 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Data.Common;
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.Infrastructure.Persistence.Repositories.Implement;
using Umbraco.Cms.Infrastructure.Scoping;
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 ServerRegistrationRepositoryTest : UmbracoIntegrationTest
{
[SetUp]
public void SetUp()
{
_appCaches = AppCaches.Disabled;
CreateTestData();
}
private AppCaches _appCaches;
private ServerRegistrationRepository CreateRepository(IScopeProvider provider) =>
new((IScopeAccessor)provider, LoggerFactory.CreateLogger<ServerRegistrationRepository>(), Mock.Of<IRepositoryCacheVersionService>(), Mock.Of<ICacheSyncService>());
[Test]
public void Cannot_Add_Duplicate_Server_Identities()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER1", DateTime.UtcNow);
Assert.That(() => repository.Save(server), Throws.InstanceOf<DbException>());
}
}
[Test]
public void Cannot_Update_To_Duplicate_Server_Identities()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
var server = repository.Get(1);
server.ServerIdentity = "COMPUTER2";
Assert.That(() => repository.Save(server), Throws.InstanceOf<DbException>());
}
}
[Test]
public void Can_Instantiate_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Assert
Assert.That(repository, Is.Not.Null);
}
}
[Test]
public void Can_Perform_Get_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var server = repository.Get(1);
// Assert
Assert.That(server, Is.Not.Null);
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.ServerAddress, Is.EqualTo("http://localhost"));
}
}
[Test]
public void Can_Perform_GetAll_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var servers = repository.GetMany();
// Assert
Assert.That(servers.Count(), Is.EqualTo(3));
}
}
// queries are not supported due to in-memory caching
//// [Test]
//// public void Can_Perform_GetByQuery_On_Repository()
//// {
//// // Arrange
//// var provider = ScopeProvider;
//// using (var unitOfWork = provider.GetUnitOfWork())
//// using (var repository = CreateRepository(provider))
//// {
//// // Act
//// var query = Query<IServerRegistration>.Builder.Where(x => x.ServerIdentity.ToUpper() == "COMPUTER3");
//// var result = repository.GetByQuery(query);
//// // Assert
//// Assert.AreEqual(1, result.Count());
//// }
//// }
//// [Test]
//// public void Can_Perform_Count_On_Repository()
//// {
//// // Arrange
//// var provider = ScopeProvider;
//// using (var unitOfWork = provider.GetUnitOfWork())
//// using (var repository = CreateRepository(provider))
//// {
//// // Act
//// var query = Query<IServerRegistration>.Builder.Where(x => x.ServerAddress.StartsWith("http://"));
//// int count = repository.Count(query);
//// // Assert
//// Assert.That(count, Is.EqualTo(2));
//// }
//// }
[Test]
public void Can_Perform_Add_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var server = new ServerRegistration("http://shazwazza.com", "COMPUTER4", DateTime.UtcNow);
repository.Save(server);
// Assert
Assert.That(server.HasIdentity, Is.True);
Assert.That(server.Id, Is.EqualTo(4)); // With 3 existing entries the Id should be 4
}
}
[Test]
public void Can_Perform_Update_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var server = repository.Get(2);
server.ServerAddress = "https://umbraco.com";
server.IsActive = true;
repository.Save(server);
var serverUpdated = repository.Get(2);
// Assert
Assert.That(serverUpdated, Is.Not.Null);
Assert.That(serverUpdated.ServerAddress, Is.EqualTo("https://umbraco.com"));
Assert.That(serverUpdated.IsActive, Is.EqualTo(true));
}
}
[Test]
public void Can_Perform_Delete_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var server = repository.Get(3);
Assert.IsNotNull(server);
repository.Delete(server);
var exists = repository.Exists(3);
// Assert
Assert.That(exists, Is.False);
}
}
[Test]
public void Can_Perform_Exists_On_Repository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository(provider);
// Act
var exists = repository.Exists(3);
var doesntExist = repository.Exists(10);
// Assert
Assert.That(exists, Is.True);
Assert.That(doesntExist, Is.False);
}
}
public void CreateTestData()
{
var provider = ScopeProvider;
using (var scope = provider.CreateScope())
{
var repository = CreateRepository(provider);
repository.Save(new ServerRegistration("http://localhost", "COMPUTER1", DateTime.UtcNow) { IsActive = true });
repository.Save(new ServerRegistration("http://www.mydomain.com", "COMPUTER2", DateTime.UtcNow));
repository.Save(new ServerRegistration("https://www.another.domain.com", "Computer3", DateTime.UtcNow));
scope.Complete();
}
}
}