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

262 lines
7.8 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Linq;
using System.Web.UI;
using NUnit.Framework;
using Umbraco.Core.Cache;
namespace Umbraco.Tests.Cache
{
2019-01-18 08:14:08 +01:00
public abstract class AppCacheTests
2018-06-29 19:52:40 +02:00
{
2019-01-18 08:14:08 +01:00
internal abstract IAppCache AppCache { get; }
2018-06-29 19:52:40 +02:00
protected abstract int GetTotalItemCount { get; }
[SetUp]
public virtual void Setup()
{
}
[TearDown]
public virtual void TearDown()
{
2019-01-18 08:14:08 +01:00
AppCache.Clear();
2018-06-29 19:52:40 +02:00
}
[Test]
public void Throws_On_Reentry()
{
2019-01-18 08:29:16 +01:00
// don't run for DictionaryAppCache - not making sense
if (GetType() == typeof (DictionaryAppCacheTests))
Assert.Ignore("Do not run for DictionaryAppCache.");
2018-06-29 19:52:40 +02:00
Exception exception = null;
2019-01-18 08:14:08 +01:00
var result = AppCache.Get("blah", () =>
2018-06-29 19:52:40 +02:00
{
try
{
2019-01-18 08:14:08 +01:00
var result2 = AppCache.Get("blah");
2018-06-29 19:52:40 +02:00
}
catch (Exception e)
{
exception = e;
}
return "value";
});
Assert.IsNotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
}
[Test]
public void Does_Not_Cache_Exceptions()
{
var counter = 0;
object result;
try
{
2019-01-18 08:14:08 +01:00
result = AppCache.Get("Blah", () =>
2018-06-29 19:52:40 +02:00
{
counter++;
throw new Exception("Do not cache this");
});
}
catch (Exception){}
try
{
2019-01-18 08:14:08 +01:00
result = AppCache.Get("Blah", () =>
2018-06-29 19:52:40 +02:00
{
counter++;
throw new Exception("Do not cache this");
});
}
catch (Exception){}
Assert.Greater(counter, 1);
}
[Test]
public void Ensures_Delegate_Result_Is_Cached_Once()
{
var counter = 0;
object result;
2019-01-18 08:14:08 +01:00
result = AppCache.Get("Blah", () =>
2018-06-29 19:52:40 +02:00
{
counter++;
return "";
});
2019-01-18 08:14:08 +01:00
result = AppCache.Get("Blah", () =>
2018-06-29 19:52:40 +02:00
{
counter++;
return "";
});
2020-03-20 23:25:32 +11:00
Assert.AreEqual(1, counter);
2018-06-29 19:52:40 +02:00
}
[Test]
public void Can_Get_By_Search()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
var result = AppCache.SearchByKey("Tes");
2018-06-29 19:52:40 +02:00
Assert.AreEqual(3, result.Count());
}
[Test]
public void Can_Clear_By_Expression()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("TTes1t", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
AppCache.ClearByRegex("^\\w+es\\d.*");
2018-06-29 19:52:40 +02:00
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_By_Search()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Tester2", () => cacheContent2);
AppCache.Get("Tes3", () => cacheContent3);
AppCache.Get("different4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
AppCache.ClearByKey("Test");
2018-06-29 19:52:40 +02:00
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_By_Key()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
AppCache.Clear("Test1");
AppCache.Clear("Test2");
2018-06-29 19:52:40 +02:00
Assert.AreEqual(2, GetTotalItemCount);
}
[Test]
public void Can_Clear_All_Items()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
AppCache.Clear();
2018-06-29 19:52:40 +02:00
Assert.AreEqual(0, GetTotalItemCount);
}
[Test]
public void Can_Add_When_Not_Available()
{
var cacheContent1 = new MacroCacheContent();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(1, GetTotalItemCount);
}
[Test]
public void Can_Get_When_Available()
{
var cacheContent1 = new MacroCacheContent();
2019-01-18 08:14:08 +01:00
var result = AppCache.Get("Test1", () => cacheContent1);
var result2 = AppCache.Get("Test1", () => cacheContent1);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(1, GetTotalItemCount);
Assert.AreEqual(result, result2);
}
[Test]
public void Can_Remove_By_Type_Name()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
//Provider.ClearCacheObjectTypes("umbraco.MacroCacheContent");
AppCache.ClearOfType<MacroCacheContent>();
2018-06-29 19:52:40 +02:00
Assert.AreEqual(1, GetTotalItemCount);
}
[Test]
public void Can_Remove_By_Strong_Type()
{
var cacheContent1 = new MacroCacheContent();
var cacheContent2 = new MacroCacheContent();
var cacheContent3 = new MacroCacheContent();
2018-06-29 19:52:40 +02:00
var cacheContent4 = new LiteralControl();
2019-01-18 08:14:08 +01:00
AppCache.Get("Test1", () => cacheContent1);
AppCache.Get("Test2", () => cacheContent2);
AppCache.Get("Test3", () => cacheContent3);
AppCache.Get("Test4", () => cacheContent4);
2018-06-29 19:52:40 +02:00
Assert.AreEqual(4, GetTotalItemCount);
2019-01-18 08:14:08 +01:00
AppCache.ClearOfType<MacroCacheContent>();
2018-06-29 19:52:40 +02:00
Assert.AreEqual(1, GetTotalItemCount);
}
//just used for these tests
private class MacroCacheContent
{
}
2018-06-29 19:52:40 +02:00
}
}