From b804ff61076fe22434c99d13175df2c3037ff7a7 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 21 May 2015 16:01:05 +0200 Subject: [PATCH] Explicit exception on cache reentry --- .../Cache/DictionaryCacheProviderBase.cs | 11 +++++++-- src/Umbraco.Tests/Cache/CacheProviderTests.cs | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Cache/DictionaryCacheProviderBase.cs b/src/Umbraco.Core/Cache/DictionaryCacheProviderBase.cs index 64eb8a3d35..a2bcaee9c9 100644 --- a/src/Umbraco.Core/Cache/DictionaryCacheProviderBase.cs +++ b/src/Umbraco.Core/Cache/DictionaryCacheProviderBase.cs @@ -61,8 +61,15 @@ namespace Umbraco.Core.Cache if (lazy.Value is ExceptionHolder) return null; // we have a value and execution has not thrown so returning - // here does not throw - return lazy.Value; + // here does not throw - unless we're re-entering, take care of it + try + { + return lazy.Value; + } + catch (InvalidOperationException e) + { + throw new InvalidOperationException("The method that computes a value for the cache has tried to read that value from the cache.", e); + } } internal class ExceptionHolder diff --git a/src/Umbraco.Tests/Cache/CacheProviderTests.cs b/src/Umbraco.Tests/Cache/CacheProviderTests.cs index 8dd76102cf..384fbf2bb8 100644 --- a/src/Umbraco.Tests/Cache/CacheProviderTests.cs +++ b/src/Umbraco.Tests/Cache/CacheProviderTests.cs @@ -24,6 +24,30 @@ namespace Umbraco.Tests.Cache Provider.ClearAllCache(); } + [Test] + public void Throws_On_Reentry() + { + // don't run for StaticCacheProvider - not making sense + if (GetType() == typeof (StaticCacheProviderTests)) + Assert.Ignore("Do not run for StaticCacheProvider."); + + Exception exception = null; + var result = Provider.GetCacheItem("blah", () => + { + try + { + var result2 = Provider.GetCacheItem("blah"); + } + catch (Exception e) + { + exception = e; + } + return "value"; + }); + Assert.IsNotNull(exception); + Assert.IsAssignableFrom(exception); + } + [Test] public void Does_Not_Cache_Exceptions() {