updates especially when deleting one since we require all sorts of Ids from the object but the object will be deleted by the time the request reaches other servers so instead we create a json payload to send to other servers which contains all information necessary to refresh/clear the cache on the other servers. This will probably be the preferred way going forward to handle cache refreshing. With this in place, when removing a conent type cache is removed based on events. This also adds a 'class' generic argument constraint to the repository base classes to guarantee we can do null checks and then we also fix a null check on RepositoryBase. Updates the Entity class to properly support tracking properties - this now allows us to determine if an entity was new, which is now used to ensure we don't re-update all of the content cache when a new content type is created. Have also changed the deletion and creation of document types to use the new API, this allows for a lot less processing and streamlining how all cache is invalidated. Fixes the construction of a new Template and Content Type in the v6 api and ensures that default values are set - #U4-1972, #U4-1971
211 lines
7.1 KiB
C#
211 lines
7.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using NUnit.Framework;
|
|
using Umbraco.Core;
|
|
using Umbraco.Core.ObjectResolution;
|
|
using Umbraco.Core.Sync;
|
|
using Umbraco.Web.Cache;
|
|
using umbraco.interfaces;
|
|
|
|
namespace Umbraco.Tests.Sync
|
|
{
|
|
/// <summary>
|
|
/// Ensures that calls to DistributedCache methods carry through to the IServerMessenger correctly
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class DistributedCacheTests
|
|
{
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
ServerRegistrarResolver.Current = new ServerRegistrarResolver(
|
|
new TestServerRegistrar());
|
|
ServerMessengerResolver.Current = new ServerMessengerResolver(
|
|
new TestServerMessenger());
|
|
CacheRefreshersResolver.Current = new CacheRefreshersResolver(() => new[] { typeof(TestCacheRefresher) });
|
|
Resolution.Freeze();
|
|
}
|
|
|
|
[TearDown]
|
|
public void Teardown()
|
|
{
|
|
ServerRegistrarResolver.Reset();
|
|
ServerMessengerResolver.Reset();
|
|
CacheRefreshersResolver.Reset();
|
|
}
|
|
|
|
[Test]
|
|
public void RefreshIntId()
|
|
{
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
DistributedCache.Instance.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
|
|
}
|
|
Assert.AreEqual(10, ((TestServerMessenger)ServerMessengerResolver.Current.Messenger).IntIdsRefreshed.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void RefreshIntIdFromObject()
|
|
{
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
DistributedCache.Instance.Refresh(
|
|
Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"),
|
|
x => x.Id,
|
|
new TestObjectWithId{Id = i});
|
|
}
|
|
Assert.AreEqual(10, ((TestServerMessenger)ServerMessengerResolver.Current.Messenger).IntIdsRefreshed.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void RefreshGuidId()
|
|
{
|
|
for (var i = 0; i < 11; i++)
|
|
{
|
|
DistributedCache.Instance.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), Guid.NewGuid());
|
|
}
|
|
Assert.AreEqual(11, ((TestServerMessenger)ServerMessengerResolver.Current.Messenger).GuidIdsRefreshed.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void RemoveIds()
|
|
{
|
|
for (var i = 0; i < 12; i++)
|
|
{
|
|
DistributedCache.Instance.Remove(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
|
|
}
|
|
Assert.AreEqual(12, ((TestServerMessenger)ServerMessengerResolver.Current.Messenger).IntIdsRemoved.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void FullRefreshes()
|
|
{
|
|
for (var i = 0; i < 13; i++)
|
|
{
|
|
DistributedCache.Instance.RefreshAll(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"));
|
|
}
|
|
Assert.AreEqual(13, ((TestServerMessenger)ServerMessengerResolver.Current.Messenger).CountOfFullRefreshes);
|
|
}
|
|
|
|
#region internal test classes
|
|
|
|
internal class TestObjectWithId
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
internal class TestCacheRefresher : ICacheRefresher
|
|
{
|
|
public Guid UniqueIdentifier
|
|
{
|
|
get { return Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"); }
|
|
}
|
|
public string Name
|
|
{
|
|
get { return "Test"; }
|
|
}
|
|
public void RefreshAll()
|
|
{
|
|
|
|
}
|
|
|
|
public void Refresh(int id)
|
|
{
|
|
|
|
}
|
|
|
|
public void Remove(int id)
|
|
{
|
|
|
|
}
|
|
|
|
public void Refresh(Guid id)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
internal class TestServerMessenger : IServerMessenger
|
|
{
|
|
//used for tests
|
|
public List<int> IntIdsRefreshed = new List<int>();
|
|
public List<Guid> GuidIdsRefreshed = new List<Guid>();
|
|
public List<int> IntIdsRemoved = new List<int>();
|
|
public List<string> PayloadsRemoved = new List<string>();
|
|
public List<string> PayloadsRefreshed = new List<string>();
|
|
public int CountOfFullRefreshes = 0;
|
|
|
|
|
|
public void PerformRefresh(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, string jsonPayload)
|
|
{
|
|
PayloadsRefreshed.Add(jsonPayload);
|
|
}
|
|
|
|
public void PerformRefresh<T>(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
|
|
{
|
|
IntIdsRefreshed.AddRange(instances.Select(getNumericId));
|
|
}
|
|
|
|
public void PerformRefresh<T>(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, Func<T, Guid> getGuidId, params T[] instances)
|
|
{
|
|
GuidIdsRefreshed.AddRange(instances.Select(getGuidId));
|
|
}
|
|
|
|
public void PerformRemove(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, string jsonPayload)
|
|
{
|
|
PayloadsRemoved.Add(jsonPayload);
|
|
}
|
|
|
|
public void PerformRemove<T>(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
|
|
{
|
|
IntIdsRemoved.AddRange(instances.Select(getNumericId));
|
|
}
|
|
|
|
public void PerformRemove(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, params int[] numericIds)
|
|
{
|
|
IntIdsRemoved.AddRange(numericIds);
|
|
}
|
|
|
|
public void PerformRefresh(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, params int[] numericIds)
|
|
{
|
|
IntIdsRefreshed.AddRange(numericIds);
|
|
}
|
|
|
|
public void PerformRefresh(IEnumerable<IServerAddress> servers, ICacheRefresher refresher, params Guid[] guidIds)
|
|
{
|
|
GuidIdsRefreshed.AddRange(guidIds);
|
|
}
|
|
|
|
public void PerformRefreshAll(IEnumerable<IServerAddress> servers, ICacheRefresher refresher)
|
|
{
|
|
CountOfFullRefreshes++;
|
|
}
|
|
}
|
|
|
|
internal class TestServerRegistrar : IServerRegistrar
|
|
{
|
|
public IEnumerable<IServerAddress> Registrations
|
|
{
|
|
get
|
|
{
|
|
return new List<IServerAddress>()
|
|
{
|
|
new TestServerAddress("localhost")
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
public class TestServerAddress : IServerAddress
|
|
{
|
|
public TestServerAddress(string address)
|
|
{
|
|
ServerAddress = address;
|
|
}
|
|
public string ServerAddress { get; private set; }
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |