Files
Umbraco-CMS/src/Umbraco.Tests/Cache/DeepCloneAppCacheTests.cs

115 lines
3.4 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
using System.Diagnostics;
using System.Reflection;
using System.Web;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Collections;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Entities;
using Umbraco.Tests.Collections;
using Umbraco.Web.Cache;
namespace Umbraco.Tests.Cache
{
[TestFixture]
2019-01-18 08:14:08 +01:00
public class DeepCloneAppCacheTests : RuntimeAppCacheTests
{
2019-01-17 11:01:23 +01:00
private DeepCloneAppCache _provider;
protected override int GetTotalItemCount => HttpRuntime.Cache.Count;
public override void Setup()
{
base.Setup();
2019-01-17 11:01:23 +01:00
_provider = new DeepCloneAppCache(new WebCachingAppCache(HttpRuntime.Cache));
}
internal override IAppCache AppCache => _provider;
internal override IAppPolicyCache AppPolicyCache => _provider;
[Test]
public void Clones_List()
{
var original = new DeepCloneableList<DeepCloneableListTests.TestClone>(ListCloneBehavior.Always);
original.Add(new DeepCloneableListTests.TestClone());
original.Add(new DeepCloneableListTests.TestClone());
original.Add(new DeepCloneableListTests.TestClone());
var val = _provider.GetCacheItem<DeepCloneableList<DeepCloneableListTests.TestClone>>("test", () => original);
Assert.AreEqual(original.Count, val.Count);
foreach (var item in val)
{
Assert.IsTrue(item.IsClone);
}
}
[Test]
public void Ensures_Cloned_And_Reset()
{
var original = new TestClass()
{
Name = "hello"
};
Assert.IsTrue(original.IsDirty());
var val = _provider.GetCacheItem<TestClass>("test", () => original);
2017-07-20 11:21:28 +02:00
Assert.AreNotEqual(original.CloneId, val.CloneId);
Assert.IsFalse(val.IsDirty());
}
[Test]
public void DoesNotCacheExceptions()
{
string value;
2019-01-17 11:01:23 +01:00
Assert.Throws<Exception>(() => { value = (string)_provider.Get("key", () => GetValue(1)); });
Assert.Throws<Exception>(() => { value = (string)_provider.Get("key", () => GetValue(2)); });
// does not throw
2019-01-17 11:01:23 +01:00
value = (string)_provider.Get("key", () => GetValue(3));
Assert.AreEqual("succ3", value);
// cache
2019-01-17 11:01:23 +01:00
value = (string)_provider.Get("key", () => GetValue(4));
Assert.AreEqual("succ3", value);
}
private static string GetValue(int i)
{
Debug.Print("get" + i);
if (i < 3)
throw new Exception("fail");
return "succ" + i;
}
private class TestClass : BeingDirtyBase, IDeepCloneable
{
public TestClass()
{
CloneId = Guid.NewGuid();
}
private string _name;
public string Name
{
get => _name;
set => SetPropertyValueAndDetectChanges(value, ref _name, nameof(Name));
}
public Guid CloneId { get; set; }
public object DeepClone()
{
var shallowClone = (TestClass)MemberwiseClone();
DeepCloneHelper.DeepCloneRefProperties(this, shallowClone);
shallowClone.CloneId = Guid.NewGuid();
return shallowClone;
}
}
}
2017-07-20 11:21:28 +02:00
}