2014-11-13 13:05:27 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2018-11-28 11:05:41 +01:00
|
|
|
|
using Moq;
|
2014-11-13 13:05:27 +11:00
|
|
|
|
using NUnit.Framework;
|
2018-07-20 15:45:01 +02:00
|
|
|
|
using Umbraco.Core;
|
2016-03-17 15:59:35 +01:00
|
|
|
|
using Umbraco.Core.Cache;
|
2017-05-30 15:46:25 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2018-11-28 11:05:41 +01:00
|
|
|
|
using Umbraco.Core.Logging;
|
2014-11-13 13:05:27 +11:00
|
|
|
|
using Umbraco.Core.Sync;
|
2018-12-12 14:28:57 +01:00
|
|
|
|
using Umbraco.Tests.Components;
|
2014-11-13 13:05:27 +11:00
|
|
|
|
|
2015-04-10 10:36:21 +10:00
|
|
|
|
namespace Umbraco.Tests.Cache.DistributedCache
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Ensures that calls to DistributedCache methods carry through to the IServerMessenger correctly
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
|
public class DistributedCacheTests
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
private Umbraco.Web.Cache.DistributedCache _distributedCache;
|
|
|
|
|
|
|
2014-11-13 13:05:27 +11:00
|
|
|
|
[SetUp]
|
|
|
|
|
|
public void Setup()
|
|
|
|
|
|
{
|
2018-11-28 12:59:40 +01:00
|
|
|
|
var register = RegisterFactory.Create();
|
2018-11-28 16:57:01 +01:00
|
|
|
|
|
2018-12-12 14:28:57 +01:00
|
|
|
|
var composition = new Composition(register, new TypeLoader(), Mock.Of<IProfilingLogger>(), ComponentTests.MockRuntimeState(RuntimeLevel.Run));
|
2015-05-05 16:53:52 +10:00
|
|
|
|
|
2018-11-29 10:35:16 +01:00
|
|
|
|
composition.RegisterUnique<IServerRegistrar>(_ => new TestServerRegistrar());
|
|
|
|
|
|
composition.RegisterUnique<IServerMessenger>(_ => new TestServerMessenger());
|
2016-08-23 11:17:08 +02:00
|
|
|
|
|
2018-11-28 17:35:12 +01:00
|
|
|
|
composition.WithCollectionBuilder<CacheRefresherCollectionBuilder>()
|
2016-08-13 16:02:35 +02:00
|
|
|
|
.Add<TestCacheRefresher>();
|
2017-07-11 19:13:45 +02:00
|
|
|
|
|
2018-11-29 10:35:16 +01:00
|
|
|
|
Current.Factory = composition.CreateFactory();
|
2018-11-28 16:57:01 +01:00
|
|
|
|
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache = new Umbraco.Web.Cache.DistributedCache();
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
|
public void Teardown()
|
|
|
|
|
|
{
|
2016-08-07 16:45:41 +02:00
|
|
|
|
Current.Reset();
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void RefreshIntId()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var i = 1; i < 11; i++)
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
2016-08-23 11:17:08 +02:00
|
|
|
|
Assert.AreEqual(10, ((TestServerMessenger)Current.ServerMessenger).IntIdsRefreshed.Count);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void RefreshIntIdFromObject()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache.Refresh(
|
2014-11-13 13:05:27 +11:00
|
|
|
|
Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"),
|
|
|
|
|
|
x => x.Id,
|
|
|
|
|
|
new TestObjectWithId{Id = i});
|
|
|
|
|
|
}
|
2016-08-23 11:17:08 +02:00
|
|
|
|
Assert.AreEqual(10, ((TestServerMessenger)Current.ServerMessenger).IntIdsRefreshed.Count);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void RefreshGuidId()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var i = 0; i < 11; i++)
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache.Refresh(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), Guid.NewGuid());
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
2016-08-23 11:17:08 +02:00
|
|
|
|
Assert.AreEqual(11, ((TestServerMessenger)Current.ServerMessenger).GuidIdsRefreshed.Count);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void RemoveIds()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var i = 1; i < 13; i++)
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache.Remove(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"), i);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
2016-08-23 11:17:08 +02:00
|
|
|
|
Assert.AreEqual(12, ((TestServerMessenger)Current.ServerMessenger).IntIdsRemoved.Count);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void FullRefreshes()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (var i = 0; i < 13; i++)
|
|
|
|
|
|
{
|
2017-07-11 19:13:45 +02:00
|
|
|
|
_distributedCache.RefreshAll(Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73"));
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
2016-08-23 11:17:08 +02:00
|
|
|
|
Assert.AreEqual(13, ((TestServerMessenger)Current.ServerMessenger).CountOfFullRefreshes);
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region internal test classes
|
|
|
|
|
|
|
|
|
|
|
|
internal class TestObjectWithId
|
|
|
|
|
|
{
|
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class TestCacheRefresher : ICacheRefresher
|
|
|
|
|
|
{
|
2016-05-26 17:12:04 +02:00
|
|
|
|
public static readonly Guid UniqueId = Guid.Parse("E0F452CB-DCB2-4E84-B5A5-4F01744C5C73");
|
|
|
|
|
|
|
|
|
|
|
|
public Guid RefresherUniqueId => UniqueId;
|
|
|
|
|
|
|
|
|
|
|
|
public string Name => "Test Cache Refresher";
|
|
|
|
|
|
|
2014-11-13 13:05:27 +11:00
|
|
|
|
public void RefreshAll()
|
2016-05-26 17:12:04 +02:00
|
|
|
|
{ }
|
2014-11-13 13:05:27 +11:00
|
|
|
|
|
|
|
|
|
|
public void Refresh(int id)
|
2016-05-26 17:12:04 +02:00
|
|
|
|
{ }
|
2014-11-13 13:05:27 +11:00
|
|
|
|
|
|
|
|
|
|
public void Remove(int id)
|
2016-05-26 17:12:04 +02:00
|
|
|
|
{ }
|
2014-11-13 13:05:27 +11:00
|
|
|
|
|
|
|
|
|
|
public void Refresh(Guid id)
|
2016-05-26 17:12:04 +02:00
|
|
|
|
{ }
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class TestServerMessenger : IServerMessenger
|
|
|
|
|
|
{
|
|
|
|
|
|
//used for tests
|
2016-07-29 10:58:57 +02:00
|
|
|
|
public List<int> IntIdsRefreshed = new List<int>();
|
2014-11-13 13:05:27 +11:00
|
|
|
|
public List<Guid> GuidIdsRefreshed = new List<Guid>();
|
|
|
|
|
|
public List<int> IntIdsRemoved = new List<int>();
|
|
|
|
|
|
public List<string> PayloadsRemoved = new List<string>();
|
2016-07-29 10:58:57 +02:00
|
|
|
|
public List<string> PayloadsRefreshed = new List<string>();
|
2014-11-13 13:05:27 +11:00
|
|
|
|
public int CountOfFullRefreshes = 0;
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh<TPayload>(ICacheRefresher refresher, TPayload[] payload)
|
2015-04-08 14:21:58 +02:00
|
|
|
|
{
|
|
|
|
|
|
// doing nothing
|
|
|
|
|
|
}
|
2014-11-13 13:05:27 +11:00
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh(ICacheRefresher refresher, string jsonPayload)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
PayloadsRefreshed.Add(jsonPayload);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh<T>(ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
IntIdsRefreshed.AddRange(instances.Select(getNumericId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh<T>(ICacheRefresher refresher, Func<T, Guid> getGuidId, params T[] instances)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
GuidIdsRefreshed.AddRange(instances.Select(getGuidId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRemove(ICacheRefresher refresher, string jsonPayload)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
PayloadsRemoved.Add(jsonPayload);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRemove<T>(ICacheRefresher refresher, Func<T, int> getNumericId, params T[] instances)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
IntIdsRemoved.AddRange(instances.Select(getNumericId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRemove(ICacheRefresher refresher, params int[] numericIds)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
IntIdsRemoved.AddRange(numericIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh(ICacheRefresher refresher, params int[] numericIds)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
IntIdsRefreshed.AddRange(numericIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefresh(ICacheRefresher refresher, params Guid[] guidIds)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
GuidIdsRefreshed.AddRange(guidIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-01 10:39:04 +10:00
|
|
|
|
public void PerformRefreshAll(ICacheRefresher refresher)
|
2014-11-13 13:05:27 +11:00
|
|
|
|
{
|
|
|
|
|
|
CountOfFullRefreshes++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class TestServerRegistrar : IServerRegistrar
|
|
|
|
|
|
{
|
2016-06-20 17:30:49 +02:00
|
|
|
|
public IEnumerable<IServerAddress> Registrations => new List<IServerAddress>
|
|
|
|
|
|
{
|
|
|
|
|
|
new TestServerAddress("localhost")
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public ServerRole GetCurrentServerRole()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetCurrentServerUmbracoApplicationUrl()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
2014-11-13 13:05:27 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class TestServerAddress : IServerAddress
|
|
|
|
|
|
{
|
|
|
|
|
|
public TestServerAddress(string address)
|
|
|
|
|
|
{
|
|
|
|
|
|
ServerAddress = address;
|
|
|
|
|
|
}
|
|
|
|
|
|
public string ServerAddress { get; private set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|