added some tests for cache refresher

This commit is contained in:
Shannon
2014-09-16 11:19:33 +10:00
parent 7437240d09
commit 311639332d
3 changed files with 54 additions and 21 deletions

View File

@@ -1,9 +1,26 @@
using NUnit.Framework;
using Umbraco.Core.Cache;
using umbraco.presentation.webservices;
using Umbraco.Tests.TestHelpers;
namespace Umbraco.Tests.Cache
{
[TestFixture]
public class CacheRefresherTests
{
[TestCase("", "123456", "testmachine", true)] //empty hash will continue
[TestCase("2c8aabac795d189d444a9cdc6e2a1819", "123456", "testmachine", false)] //match, don't continue
[TestCase("2c8aabac795d189d444a9cdc6e2a1819", "12345", "testmachine", true)]
[TestCase("2c8aabac795d189d444a9cdc6e2a1819", "123456", "testmachin", true)]
[TestCase("2c8aabac795d189d444a9cdc6e2a181", "123456", "testmachine", true)]
public void Continue_Refreshing_For_Request(string hash, string appDomainAppId, string machineName, bool expected)
{
var refresher = new CacheRefresher();
Assert.AreEqual(expected, refresher.ContinueRefreshingForRequest(hash, appDomainAppId, machineName));
}
}
[TestFixture]
public class HttpRequestCacheProviderTests : CacheProviderTests
{

View File

@@ -139,6 +139,7 @@
<Reference Include="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Services" />
<Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll</HintPath>

View File

@@ -24,6 +24,40 @@ namespace umbraco.presentation.webservices
public class CacheRefresher : WebService
{
/// <summary>
/// This checks the passed in hash and verifies if it does not match the hash of the combination of appDomainAppId and machineName
/// passed in. If the hashes don't match, then cache refreshing continues.
/// </summary>
/// <param name="hash"></param>
/// <param name="appDomainAppId"></param>
/// <param name="machineName"></param>
/// <returns></returns>
internal bool ContinueRefreshingForRequest(string hash, string appDomainAppId, string machineName)
{
//check if this is the same app id as the one passed in, if it is, then we will ignore
// the request - we will have to assume that the cache refreshing has already been applied to the server
// that executed the request.
if (hash.IsNullOrWhiteSpace() == false && SystemUtilities.GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted)
{
var hashedAppId = (machineName + appDomainAppId).ToMd5();
//we can only check this in full trust. if it's in medium trust we'll just end up with
// the server refreshing it's cache twice.
if (hashedAppId == hash)
{
LogHelper.Debug<CacheRefresher>(
"The passed in hashed appId equals the current server's hashed appId, cache refreshing will be ignored for this request as it will have already executed for this server (server: {0} , appId: {1} , hash: {2})",
() => machineName,
() => appDomainAppId,
() => hashedAppId);
return false;
}
}
return true;
}
[WebMethod]
public void BulkRefresh(RefreshInstruction[] instructions, string appId, string login, string password)
{
@@ -32,26 +66,7 @@ namespace umbraco.presentation.webservices
return;
}
//check if this is the same app id as the one passed in, if it is, then we will ignore
// the request - we will have to assume that the cache refeshing has already been applied to the server
// that executed the request.
if (appId.IsNullOrWhiteSpace() == false && SystemUtilities.GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted)
{
var hashedAppId = (NetworkHelper.MachineName + HttpRuntime.AppDomainAppId).ToMd5();
//we can only check this in full trust. if it's in medium trust we'll just end up with
// the server refreshing it's cache twice.
if (hashedAppId == appId)
{
LogHelper.Debug<CacheRefresher>(
"The passed in hashed appId equals the current server's hashed appId, cache refreshing will be ignored for this request as it will have already executed for this server (server: {0} , appId: {1} , hash: {2})",
() => NetworkHelper.MachineName,
() => HttpRuntime.AppDomainAppId,
() => hashedAppId);
return;
}
}
if (ContinueRefreshingForRequest(appId, HttpRuntime.AppDomainAppId, NetworkHelper.MachineName) == false) return;
//only execute distinct instructions - no sense in running the same one.
foreach (var instruction in instructions.Distinct())