From 9adf5f43f11d5af66081a4c595a917152edcdbbf Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Sat, 25 Nov 2017 13:44:29 +0100 Subject: [PATCH 1/3] Update IApiOutputCache to Async signatures Removes obsolete Get signature. Updates Core library to Framework 4.5 to better facilitate Task methods. IApiOutputCache usages and implementations are adjusted to use the updated interface. --- .../TeamsController.cs | 5 +- .../Cache/CacheExtensions.cs | 7 +- .../Cache/IApiOutputCache.cs | 16 +- .../Cache/MemoryCacheDefault.cs | 48 ++++-- .../WebApi.OutputCache.Core.csproj | 6 +- .../AutoInvalidateCacheOutputAttribute.cs | 8 +- .../CacheOutputAttribute.cs | 28 ++-- .../InvalidateCacheOutputAttribute.cs | 8 +- .../WebApi.OutputCache.V2.csproj | 2 +- .../MemoryCacheDefaultTests.cs | 38 ++--- .../CacheKeyGeneratorRegistrationTests.cs | 7 +- .../CacheKeyGeneratorTests.cs | 19 ++- .../ConfigurationTests.cs | 6 +- .../ConnegTests.cs | 11 +- .../InlineInvalidateTests.cs | 8 +- .../InvalidateTests.cs | 86 ++++++----- .../ServerSideTests.cs | 145 +++++++++--------- .../InlineInvalidateController.cs | 19 +-- 18 files changed, 256 insertions(+), 211 deletions(-) diff --git a/sample/WebApi.OutputCache.V2.Demo/TeamsController.cs b/sample/WebApi.OutputCache.V2.Demo/TeamsController.cs index bd71655..3882cd0 100644 --- a/sample/WebApi.OutputCache.V2.Demo/TeamsController.cs +++ b/sample/WebApi.OutputCache.V2.Demo/TeamsController.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Threading.Tasks; using System.Web.Http; using WebApi.OutputCache.V2.TimeAttributes; @@ -37,7 +38,7 @@ public void Post(Team value) Teams.Add(value); } - public void Put(int id, Team value) + public async Task Put(int id, Team value) { if (!ModelState.IsValid) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); @@ -48,7 +49,7 @@ public void Put(int id, Team value) team.Name = value.Name; var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request); - cache.RemoveStartsWith(Configuration.CacheOutputConfiguration().MakeBaseCachekey((TeamsController t) => t.GetById(0))); + await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey((TeamsController t) => t.GetById(0))); } public void Delete(int id) diff --git a/src/WebApi.OutputCache.Core/Cache/CacheExtensions.cs b/src/WebApi.OutputCache.Core/Cache/CacheExtensions.cs index 6d3af19..b0ca07b 100644 --- a/src/WebApi.OutputCache.Core/Cache/CacheExtensions.cs +++ b/src/WebApi.OutputCache.Core/Cache/CacheExtensions.cs @@ -1,17 +1,18 @@ using System; +using System.Threading.Tasks; namespace WebApi.OutputCache.Core.Cache { public static class CacheExtensions { - public static T GetCachedResult(this IApiOutputCache cache, string key, DateTimeOffset expiry, Func resultGetter, bool bypassCache = true) where T : class + public static async Task GetCachedResultAsync(this IApiOutputCache cache, string key, DateTimeOffset expiry, Func resultGetter, bool bypassCache = true) where T : class { - var result = cache.Get(key); + var result = await cache.GetAsync(key); if (result == null || bypassCache) { result = resultGetter(); - if (result != null) cache.Add(key, result, expiry); + if (result != null) await cache.AddAsync(key, result, expiry); } return result; diff --git a/src/WebApi.OutputCache.Core/Cache/IApiOutputCache.cs b/src/WebApi.OutputCache.Core/Cache/IApiOutputCache.cs index 9365d98..fbeea7d 100644 --- a/src/WebApi.OutputCache.Core/Cache/IApiOutputCache.cs +++ b/src/WebApi.OutputCache.Core/Cache/IApiOutputCache.cs @@ -1,23 +1,21 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace WebApi.OutputCache.Core.Cache { public interface IApiOutputCache { - void RemoveStartsWith(string key); + Task RemoveStartsWithAsync(string key); - T Get(string key) where T : class; + Task GetAsync(string key) where T : class; - [Obsolete("Use Get instead")] - object Get(string key); + Task RemoveAsync(string key); - void Remove(string key); + Task ContainsAsync(string key); - bool Contains(string key); + Task AddAsync(string key, object value, DateTimeOffset expiration, string dependsOnKey = null); - void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null); - - IEnumerable AllKeys { get; } + Task> AllKeysAsync { get; } } } \ No newline at end of file diff --git a/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs b/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs index 157eaa0..9e81559 100644 --- a/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs +++ b/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; +using System.Threading.Tasks; namespace WebApi.OutputCache.Core.Cache { @@ -9,7 +10,7 @@ public class MemoryCacheDefault : IApiOutputCache { private static readonly MemoryCache Cache = MemoryCache.Default; - public virtual void RemoveStartsWith(string key) + private static void RemoveStartsWith(string key) { lock (Cache) { @@ -17,19 +18,13 @@ public virtual void RemoveStartsWith(string key) } } - public virtual T Get(string key) where T : class + private static T Get(string key) where T : class { var o = Cache.Get(key) as T; return o; } - [Obsolete("Use Get instead")] - public virtual object Get(string key) - { - return Cache.Get(key); - } - - public virtual void Remove(string key) + private static void Remove(string key) { lock (Cache) { @@ -37,12 +32,12 @@ public virtual void Remove(string key) } } - public virtual bool Contains(string key) + private static bool Contains(string key) { return Cache.Contains(key); } - public virtual void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null) + private static void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null) { var cachePolicy = new CacheItemPolicy { @@ -59,14 +54,39 @@ public virtual void Add(string key, object o, DateTimeOffset expiration, string { Cache.Add(key, o, cachePolicy); } - } + } - public virtual IEnumerable AllKeys + public virtual Task> AllKeysAsync { get { - return Cache.Select(x => x.Key); + return Task.FromResult(Cache.Select(x => x.Key)); } } + + public virtual Task RemoveStartsWithAsync(string key) + { + return Task.Run(() => RemoveStartsWith(key)); + } + + public virtual Task GetAsync(string key) where T : class + { + return Task.FromResult(Get(key)); + } + + public virtual Task RemoveAsync(string key) + { + return Task.Run(() => Remove(key)); + } + + public virtual Task ContainsAsync(string key) + { + return Task.FromResult(Contains(key)); + } + + public virtual Task AddAsync(string key, object value, DateTimeOffset expiration, string dependsOnKey = null) + { + return Task.Run(() => Add(key, value, expiration, dependsOnKey)); + } } } diff --git a/src/WebApi.OutputCache.Core/WebApi.OutputCache.Core.csproj b/src/WebApi.OutputCache.Core/WebApi.OutputCache.Core.csproj index 9d0d5d3..5b1cf40 100644 --- a/src/WebApi.OutputCache.Core/WebApi.OutputCache.Core.csproj +++ b/src/WebApi.OutputCache.Core/WebApi.OutputCache.Core.csproj @@ -1,5 +1,5 @@  - + Debug @@ -9,7 +9,7 @@ Properties WebApi.OutputCache.Core WebApi.OutputCache.Core - v4.0 + v4.5 512 @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + false diff --git a/src/WebApi.OutputCache.V2/AutoInvalidateCacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/AutoInvalidateCacheOutputAttribute.cs index 24e9f64..ea6a6cc 100644 --- a/src/WebApi.OutputCache.V2/AutoInvalidateCacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/AutoInvalidateCacheOutputAttribute.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Net.Http; using System.Reflection; +using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Filters; @@ -15,7 +17,7 @@ public sealed class AutoInvalidateCacheOutputAttribute : BaseCacheAttribute { public bool TryMatchType { get; set; } - public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) + public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { if (actionExecutedContext.Response != null && !actionExecutedContext.Response.IsSuccessStatusCode) return; if (actionExecutedContext.ActionContext.Request.Method != HttpMethod.Post && @@ -33,9 +35,9 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo foreach (var action in actions) { var key = config.CacheOutputConfiguration().MakeBaseCachekey(controller.ControllerType.FullName, action); - if (WebApiCache.Contains(key)) + if (await WebApiCache.ContainsAsync(key)) { - WebApiCache.RemoveStartsWith(key); + await WebApiCache.RemoveStartsWithAsync(key); } } } diff --git a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs index f995dc9..825c66c 100644 --- a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs @@ -166,7 +166,7 @@ protected virtual MediaTypeHeaderValue GetExpectedMediaType(HttpConfiguration co return responseMediaType; } - public override void OnActionExecuting(HttpActionContext actionContext) + public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken) { if (actionContext == null) throw new ArgumentNullException("actionContext"); @@ -183,11 +183,11 @@ public override void OnActionExecuting(HttpActionContext actionContext) actionContext.Request.Properties[CurrentRequestMediaType] = responseMediaType; var cachekey = cacheKeyGenerator.MakeCacheKey(actionContext, responseMediaType, ExcludeQueryStringFromCacheKey); - if (!_webApiCache.Contains(cachekey)) return; + if (await _webApiCache.ContainsAsync(cachekey) == false) return; if (actionContext.Request.Headers.IfNoneMatch != null) { - var etag = _webApiCache.Get(cachekey + Constants.EtagKey); + var etag = await _webApiCache.GetAsync(cachekey + Constants.EtagKey); if (etag != null) { if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag == etag)) @@ -203,11 +203,11 @@ public override void OnActionExecuting(HttpActionContext actionContext) } } - var val = _webApiCache.Get(cachekey); + var val = await _webApiCache.GetAsync(cachekey); if (val == null) return; - var contenttype = _webApiCache.Get(cachekey + Constants.ContentTypeKey) ?? responseMediaType; - var contentGeneration = _webApiCache.Get(cachekey + Constants.GenerationTimestampKey); + var contenttype = await _webApiCache.GetAsync(cachekey + Constants.ContentTypeKey) ?? responseMediaType; + var contentGeneration = await _webApiCache.GetAsync(cachekey + Constants.GenerationTimestampKey); DateTimeOffset? contentGenerationTimestamp = null; if (contentGeneration != null) @@ -222,7 +222,7 @@ public override void OnActionExecuting(HttpActionContext actionContext) actionContext.Response.Content = new ByteArrayContent(val); actionContext.Response.Content.Headers.ContentType = contenttype; - var responseEtag = _webApiCache.Get(cachekey + Constants.EtagKey); + var responseEtag = await _webApiCache.GetAsync(cachekey + Constants.EtagKey); if (responseEtag != null) SetEtag(actionContext.Response, responseEtag); var cacheTime = CacheTimeQuery.Execute(DateTime.Now); @@ -246,7 +246,7 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio var responseMediaType = actionExecutedContext.Request.Properties[CurrentRequestMediaType] as MediaTypeHeaderValue ?? GetExpectedMediaType(httpConfig, actionExecutedContext.ActionContext); var cachekey = cacheKeyGenerator.MakeCacheKey(actionExecutedContext.ActionContext, responseMediaType, ExcludeQueryStringFromCacheKey); - if (!string.IsNullOrWhiteSpace(cachekey) && !(_webApiCache.Contains(cachekey))) + if (!string.IsNullOrWhiteSpace(cachekey) && !(await _webApiCache.ContainsAsync(cachekey))) { SetEtag(actionExecutedContext.Response, CreateEtag(actionExecutedContext, cachekey, cacheTime)); @@ -262,16 +262,14 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio responseContent.Headers.Remove("Content-Length"); - _webApiCache.Add(baseKey, string.Empty, cacheTime.AbsoluteExpiration); - _webApiCache.Add(cachekey, content, cacheTime.AbsoluteExpiration, baseKey); - + await _webApiCache.AddAsync(baseKey, string.Empty, cacheTime.AbsoluteExpiration); + await _webApiCache.AddAsync(cachekey, content, cacheTime.AbsoluteExpiration, baseKey); - _webApiCache.Add(cachekey + Constants.ContentTypeKey, + await _webApiCache.AddAsync(cachekey + Constants.ContentTypeKey, contentType, cacheTime.AbsoluteExpiration, baseKey); - - - _webApiCache.Add(cachekey + Constants.EtagKey, + + await _webApiCache.AddAsync(cachekey + Constants.EtagKey, etag, cacheTime.AbsoluteExpiration, baseKey); diff --git a/src/WebApi.OutputCache.V2/InvalidateCacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/InvalidateCacheOutputAttribute.cs index e30b540..fc38aeb 100644 --- a/src/WebApi.OutputCache.V2/InvalidateCacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/InvalidateCacheOutputAttribute.cs @@ -1,5 +1,7 @@ using System; using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; using System.Web.Http.Filters; namespace WebApi.OutputCache.V2 @@ -21,7 +23,7 @@ public InvalidateCacheOutputAttribute(string methodName, Type type = null) _methodName = methodName; } - public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) + public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { if (actionExecutedContext.Response != null && !actionExecutedContext.Response.IsSuccessStatusCode) return; _controller = _controller ?? actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName; @@ -30,9 +32,9 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo EnsureCache(config, actionExecutedContext.Request); var key = actionExecutedContext.Request.GetConfiguration().CacheOutputConfiguration().MakeBaseCachekey(_controller, _methodName); - if (WebApiCache.Contains(key)) + if (await WebApiCache.ContainsAsync(key)) { - WebApiCache.RemoveStartsWith(key); + await WebApiCache.RemoveStartsWithAsync(key); } } } diff --git a/src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj b/src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj index 8b04ea1..b0bf854 100644 --- a/src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj +++ b/src/WebApi.OutputCache.V2/WebApi.OutputCache.V2.csproj @@ -1,5 +1,5 @@  - + Debug diff --git a/test/WebApi.OutputCache.Core.Tests/MemoryCacheDefaultTests.cs b/test/WebApi.OutputCache.Core.Tests/MemoryCacheDefaultTests.cs index cf0fd55..7e3e1eb 100644 --- a/test/WebApi.OutputCache.Core.Tests/MemoryCacheDefaultTests.cs +++ b/test/WebApi.OutputCache.Core.Tests/MemoryCacheDefaultTests.cs @@ -8,37 +8,37 @@ namespace WebApi.OutputCache.Core.Tests public class MemoryCacheDefaultTests { [Test] - public void returns_all_keys_in_cache() + public async void returns_all_keys_in_cache() { IApiOutputCache cache = new MemoryCacheDefault(); - cache.Add("base", "abc", DateTime.Now.AddSeconds(60)); - cache.Add("key1", "abc", DateTime.Now.AddSeconds(60), "base"); - cache.Add("key2", "abc", DateTime.Now.AddSeconds(60), "base"); - cache.Add("key3", "abc", DateTime.Now.AddSeconds(60), "base"); + await cache.AddAsync("base", "abc" , DateTime.Now.AddSeconds(60)); + await cache.AddAsync("key1", "abc", DateTime.Now.AddSeconds(60), "base"); + await cache.AddAsync("key2", "abc", DateTime.Now.AddSeconds(60), "base"); + await cache.AddAsync("key3", "abc", DateTime.Now.AddSeconds(60), "base"); - var result = cache.AllKeys; + var result = await cache.AllKeysAsync; CollectionAssert.AreEquivalent(new[] { "base", "key1", "key2", "key3" }, result); } [Test] - public void remove_startswith_cascades_to_all_dependencies() + public async void remove_startswith_cascades_to_all_dependencies() { IApiOutputCache cache = new MemoryCacheDefault(); - cache.Add("base", "abc", DateTime.Now.AddSeconds(60)); - cache.Add("key1","abc", DateTime.Now.AddSeconds(60), "base"); - cache.Add("key2", "abc", DateTime.Now.AddSeconds(60), "base"); - cache.Add("key3", "abc", DateTime.Now.AddSeconds(60), "base"); - Assert.IsNotNull(cache.Get("key1")); - Assert.IsNotNull(cache.Get("key2")); - Assert.IsNotNull(cache.Get("key3")); + await cache.AddAsync("base", "abc", DateTime.Now.AddSeconds(60)); + await cache.AddAsync("key1","abc", DateTime.Now.AddSeconds(60), "base"); + await cache.AddAsync("key2", "abc", DateTime.Now.AddSeconds(60), "base"); + await cache.AddAsync("key3", "abc", DateTime.Now.AddSeconds(60), "base"); + Assert.IsNotNull(cache.GetAsync("key1")); + Assert.IsNotNull(cache.GetAsync("key2")); + Assert.IsNotNull(cache.GetAsync("key3")); - cache.RemoveStartsWith("base"); + await cache.RemoveStartsWithAsync("base"); - Assert.IsNull(cache.Get("base")); - Assert.IsNull(cache.Get("key1")); - Assert.IsNull(cache.Get("key2")); - Assert.IsNull(cache.Get("key3")); + Assert.IsNull(cache.GetAsync("base").Result); + Assert.IsNull(cache.GetAsync("key1").Result); + Assert.IsNull(cache.GetAsync("key2").Result); + Assert.IsNull(cache.GetAsync("key3").Result); } } } diff --git a/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorRegistrationTests.cs b/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorRegistrationTests.cs index 46d2ae9..5e88157 100644 --- a/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorRegistrationTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorRegistrationTests.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Controllers; using Autofac; @@ -26,6 +27,8 @@ public void init() Thread.CurrentPrincipal = null; _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(default(object))); _keyGenerator = new Mock(); var conf = new HttpConfiguration(); @@ -89,7 +92,7 @@ public void selected_generator_with_internal_registration_is_used() var client = new HttpClient(_server); var result = client.GetAsync(_url + "cachekey/get_internalregistered").Result; - _cache.Verify(s => s.Add(It.Is(x => x == "internal"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_internalregistered")), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "internal"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_internalregistered")), Times.Once()); } [Test] @@ -98,7 +101,7 @@ public void custom_unregistered_cache_key_generator_called() var client = new HttpClient(_server); var result = client.GetAsync(_url + "cachekey/get_unregistered").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "unregistered")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "unregistered")), Times.Once()); } #region Helper classes diff --git a/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorTests.cs b/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorTests.cs index ffdfcab..74beecd 100644 --- a/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/CacheKeyGeneratorTests.cs @@ -2,6 +2,7 @@ using System.Net.Http; using System.Net.Http.Headers; using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Controllers; using Autofac; @@ -35,6 +36,8 @@ public void init() Thread.CurrentPrincipal = null; _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(default(object))); _keyGeneratorA = new Mock(); _keyGeneratorB = new CustomCacheKeyGenerator(); @@ -56,18 +59,18 @@ public void init() } [Test] - public void custom_default_cache_key_generator_called_and_key_used() + public async Task custom_default_cache_key_generator_called_and_key_used() { var client = new HttpClient(_server); _keyGeneratorA.Setup(k => k.MakeCacheKey(It.IsAny(), It.IsAny(), It.IsAny())) .Returns("keykeykey") .Verifiable("Key generator was never called"); // use the samplecontroller to show that no changes are required to existing code - var result = client.GetAsync(_url + "sample/Get_c100_s100").Result; + var result = await client.GetAsync(_url + "sample/Get_c100_s100"); - _cache.Verify(s => s.Contains(It.Is(x => x == "keykeykey")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "keykeykey"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "keykeykey:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "keykeykey")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "keykeykey"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "keykeykey:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); _keyGeneratorA.VerifyAll(); } @@ -78,9 +81,9 @@ public void custom_cache_key_generator_called() var client = new HttpClient(_server); var result = client.GetAsync(_url + "cachekey/get_custom_key").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "custom_key")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "custom_key"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_custom_key")), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "custom_key:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_custom_key")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "custom_key")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "custom_key"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_custom_key")), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "custom_key:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.cachekeycontroller-get_custom_key")), Times.Once()); } } } diff --git a/test/WebApi.OutputCache.V2.Tests/ConfigurationTests.cs b/test/WebApi.OutputCache.V2.Tests/ConfigurationTests.cs index b93040e..8468725 100644 --- a/test/WebApi.OutputCache.V2.Tests/ConfigurationTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/ConfigurationTests.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http; +using System.Threading.Tasks; using System.Web.Http; using Moq; using NUnit.Framework; @@ -18,6 +19,7 @@ public class ConfigurationTests public void cache_singleton_in_pipeline() { _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); var conf = new HttpConfiguration(); conf.CacheOutputConfiguration().RegisterCacheOutputProvider(() => _cache.Object); @@ -33,10 +35,10 @@ public void cache_singleton_in_pipeline() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_c100_s100").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); var result2 = client.GetAsync(_url + "Get_c100_s100").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(4)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(4)); _server.Dispose(); } diff --git a/test/WebApi.OutputCache.V2.Tests/ConnegTests.cs b/test/WebApi.OutputCache.V2.Tests/ConnegTests.cs index 2457dfe..cade54b 100644 --- a/test/WebApi.OutputCache.V2.Tests/ConnegTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/ConnegTests.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Net.Http.Headers; +using System.Threading.Tasks; using System.Web.Http; using Autofac; using Autofac.Integration.WebApi; @@ -21,6 +22,8 @@ public class ConnegTests public void init() { _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(default(object))); var conf = new HttpConfiguration(); var builder = new ContainerBuilder(); @@ -42,15 +45,15 @@ public void subsequent_xml_request_is_not_cached() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_c100_s100").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x < DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x < DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); var req = new HttpRequestMessage(HttpMethod.Get, _url + "Get_c100_s100"); req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); var result2 = client.SendAsync(req).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x < DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x < DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); } diff --git a/test/WebApi.OutputCache.V2.Tests/InlineInvalidateTests.cs b/test/WebApi.OutputCache.V2.Tests/InlineInvalidateTests.cs index e3ad623..e19edac 100644 --- a/test/WebApi.OutputCache.V2.Tests/InlineInvalidateTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/InlineInvalidateTests.cs @@ -44,7 +44,7 @@ public void inline_call_to_invalidate_is_correct() var result = client.PostAsync(_url + "Post", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); } [Test] @@ -53,7 +53,7 @@ public void inline_call_to_invalidate_using_expression_tree_is_correct() var client = new HttpClient(_server); var result = client.PutAsync(_url + "Put", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); } [Test] @@ -62,7 +62,7 @@ public void inline_call_to_invalidate_using_expression_tree_with_param_is_correc var client = new HttpClient(_server); var result = client.DeleteAsync(_url + "Delete_parameterized").Result; - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100_with_param")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-get_c100_s100_with_param")), Times.Exactly(1)); } [Test] @@ -71,7 +71,7 @@ public void inline_call_to_invalidate_using_expression_tree_with_custom_action_n var client = new HttpClient(_server); var result = client.DeleteAsync(_url + "Delete_non_standard_name").Result; - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-getbyid")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.inlineinvalidatecontroller-getbyid")), Times.Exactly(1)); } [TearDown] diff --git a/test/WebApi.OutputCache.V2.Tests/InvalidateTests.cs b/test/WebApi.OutputCache.V2.Tests/InvalidateTests.cs index 9bdd60a..f2c1846 100644 --- a/test/WebApi.OutputCache.V2.Tests/InvalidateTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/InvalidateTests.cs @@ -1,6 +1,8 @@ -using System.Net.Http; +using System; +using System.Net.Http; using System.Net.Http.Formatting; using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using Autofac; using Autofac.Integration.WebApi; @@ -23,6 +25,8 @@ public void init() Thread.CurrentPrincipal = null; _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.RemoveStartsWithAsync(It.IsAny())).Returns(Task.FromResult(default(object))); var conf = new HttpConfiguration(); var builder = new ContainerBuilder(); @@ -46,8 +50,8 @@ public void regular_invalidate_works_on_post() var result2 = client.PostAsync(_url + "Post", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); } [Test] @@ -58,10 +62,10 @@ public void regular_invalidate_on_two_methods_works_on_post() var result2 = client.PostAsync(_url + "Post_2_invalidates", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); } [Test] @@ -72,12 +76,12 @@ public void controller_level_invalidate_on_three_methods_works_on_post() var result2 = client.PostAsync("http://www.strathweb.com/api/autoinvalidate/Post", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); } [Test] @@ -88,12 +92,12 @@ public void controller_level_invalidate_on_three_methods_works_on_put() var result2 = client.PutAsync("http://www.strathweb.com/api/autoinvalidate/Put", new StringContent(string.Empty)).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); } [Test] @@ -104,12 +108,12 @@ public void controller_level_invalidate_on_three_methods_works_on_delete() var result2 = client.DeleteAsync("http://www.strathweb.com/api/autoinvalidate/Delete").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304")), Times.Exactly(1)); } [Test] @@ -121,8 +125,8 @@ public void controller_level_invalidate_with_type_check_does_not_invalidate_on_n var result2 = client.PostAsync("http://www.strathweb.com/api/autoinvalidatewithtype/Post", new StringContent(string.Empty)).Result; Assert.True(result2.IsSuccessStatusCode); - _cache.Verify(s => s.Contains(It.IsAny()), Times.Never()); - _cache.Verify(s => s.RemoveStartsWith(It.IsAny()), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.IsAny()), Times.Never()); + _cache.Verify(s => s.RemoveStartsWithAsync(It.IsAny()), Times.Never()); } [Test] @@ -133,24 +137,24 @@ public void controller_level_invalidate_with_type_check_invalidates_only_methods var result2 = client.PostAsync("http://www.strathweb.com/api/autoinvalidatewithtype/PostString", "hi", new JsonMediaTypeFormatter()).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array")), Times.Exactly(1)); - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback")), Times.Never()); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array")), Times.Exactly(1)); - _cache.Verify(s => s.RemoveStartsWith(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback")), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback")), Times.Never()); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array")), Times.Exactly(1)); + _cache.Verify(s => s.RemoveStartsWithAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback")), Times.Never()); } private void SetupCacheForAutoInvalidate() { - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback"))).Returns(true); - _cache.Setup(x => x.Contains(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array"))).Returns(true); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_c100_s100"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-get_s50_exclude_fakecallback"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatecontroller-etag_match_304"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_s50_exclude_fakecallback"))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.ContainsAsync(It.Is(s => s == "webapi.outputcache.v2.tests.testcontrollers.autoinvalidatewithtypecontroller-get_c100_s100_array"))).Returns(Task.FromResult(true)); } [TearDown] diff --git a/test/WebApi.OutputCache.V2.Tests/ServerSideTests.cs b/test/WebApi.OutputCache.V2.Tests/ServerSideTests.cs index 27912e7..00cc1ef 100644 --- a/test/WebApi.OutputCache.V2.Tests/ServerSideTests.cs +++ b/test/WebApi.OutputCache.V2.Tests/ServerSideTests.cs @@ -4,6 +4,7 @@ using System.Net.Http.Headers; using System.Security.Principal; using System.Threading; +using System.Threading.Tasks; using System.Web.Http; using Autofac; using Autofac.Integration.WebApi; @@ -27,6 +28,10 @@ public void init() Thread.CurrentPrincipal = null; _cache = new Mock(); + _cache.Setup(cache => cache.ContainsAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(default(object))); + _cache.Setup(cache => cache.GetAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); + _cache.Setup(cache => cache.GetAsync(It.IsAny())).Returns(Task.FromResult(It.IsAny())); var conf = new HttpConfiguration(); var builder = new ContainerBuilder(); @@ -48,9 +53,9 @@ public void set_cache_to_predefined_value() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_c100_s100").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); } [Test] @@ -60,10 +65,10 @@ public void set_cache_to_predefined_value_c100_s0() var result = client.GetAsync(_url + "Get_c100_s0").Result; // NOTE: Should we expect the _cache to not be called at all if the ServerTimeSpan is 0? - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0:application/json; charset=utf-8")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0:application/json; charset=utf-8")), Times.Once()); // NOTE: Server timespan is 0, so there should not have been any Add at all. - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Never()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(1)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0")), Times.Never()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Never()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(1)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s0")), Times.Never()); } [Test] @@ -72,8 +77,8 @@ public void not_cache_when_request_not_succes() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_request_httpResponseException_noCache").Result; - _cache.Verify(s => s.Contains(It.IsAny()), Times.Once()); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.IsAny()), Times.Once()); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -82,8 +87,8 @@ public void not_cache_when_request_exception() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_request_exception_noCache").Result; - _cache.Verify(s => s.Contains(It.IsAny()), Times.Once()); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.IsAny()), Times.Once()); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -92,8 +97,8 @@ public void not_cache_add_when_no_content() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_request_noContent").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_request_nocontent:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_request_nocontent:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } [Test] @@ -104,9 +109,9 @@ public void set_cache_to_predefined_value_respect_formatter_through_accept_heade req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); var result = client.SendAsync(req).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Once()); } [Test] @@ -118,9 +123,9 @@ public void set_cache_to_predefined_value_respect_formatter_through_content_type req.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml"); var result = client.SendAsync(req).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c100_s100")), Times.Exactly(1)); } [Test] @@ -129,9 +134,9 @@ public void set_cache_dont_exclude_querystring() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_false/1?xxx=2").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1&xxx=2:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1&xxx=2:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1&xxx=2:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1&xxx=2:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false")), Times.Once()); } [Test] @@ -140,9 +145,9 @@ public void set_cache_dont_exclude_querystring_duplicate_action_arg_in_querystri var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_false/1?id=1").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_false")), Times.Once()); } [Test] @@ -152,13 +157,13 @@ public void set_cache_do_exclude_querystring() var result = client.GetAsync(_url + "Get_s50_exclude_true/1?xxx=1").Result; //check - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8")), Times.Exactly(2)); //base - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); //actual - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true")), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true")), Times.Once()); } [Test] @@ -168,13 +173,13 @@ public void set_cache_do_exclude_querystring_do_not_exclude_action_arg_even_if_p var result = client.GetAsync(_url + "Get_s50_exclude_true?id=1").Result; //check - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8")), Times.Exactly(2)); //base - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); //actual - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true")), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_true")), Times.Once()); } [Test] @@ -183,9 +188,9 @@ public void callback_at_the_end_is_excluded_querystring() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_fakecallback?id=1&callback=abc").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); } [Test] @@ -194,9 +199,9 @@ public void callback_at_the_beginning_is_excluded_querystring() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_fakecallback?callback=abc&id=1").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); } [Test] @@ -205,9 +210,9 @@ public void callback_in_the_middle_is_excluded_querystring() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_fakecallback?de=xxx&callback=abc&id=1").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1&de=xxx:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1&de=xxx:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1&de=xxx:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback-id=1&de=xxx:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); } [Test] @@ -216,9 +221,9 @@ public void callback_alone_is_excluded_querystring() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_s50_exclude_fakecallback?callback=abc").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_s50_exclude_fakecallback")), Times.Once()); } [Test] @@ -230,15 +235,15 @@ public void no_caching_if_user_authenticated_and_flag_set_to_off() Assert.True(result.IsSuccessStatusCode); Assert.IsNull(result.Headers.CacheControl); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } [Test] public void etag_match_304_if_none_match() { - _cache.Setup(x => x.Contains(It.Is(i => i.Contains("etag_match_304")))).Returns(true); - _cache.Setup(x => x.Get(It.Is(i => i.Contains("etag_match_304") && i.Contains(Constants.EtagKey)))) - .Returns(@"""abc"""); + _cache.Setup(x => x.ContainsAsync(It.Is(i => i.Contains("etag_match_304")))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.GetAsync(It.Is(i => i.Contains("etag_match_304") && i.Contains(Constants.EtagKey)))) + .Returns(Task.FromResult(@"""abc""")); var client = new HttpClient(_server); var req = new HttpRequestMessage(HttpMethod.Get, _url + "etag_match_304"); @@ -253,10 +258,10 @@ public void etag_match_304_if_none_match() [Test] public void etag_not_match_304_if_none_match() { - _cache.Setup(x => x.Contains(It.Is(i => i.Contains("etag_match_304")))).Returns(true); - _cache.Setup(x => x.Get(It.IsAny())).Returns((byte[])null); - _cache.Setup(x => x.Get(It.Is(i => i.Contains("etag_match_304") && i.Contains(Constants.EtagKey)))) - .Returns(@"""abcdef"""); + _cache.Setup(x => x.ContainsAsync(It.Is(i => i.Contains("etag_match_304")))).Returns(Task.FromResult(true)); + _cache.Setup(x => x.GetAsync(It.IsAny())).Returns(Task.FromResult((byte[])null)); + _cache.Setup(x => x.GetAsync(It.Is(i => i.Contains("etag_match_304") && i.Contains(Constants.EtagKey)))) + .Returns(Task.FromResult(@"""abcdef""")); var client = new HttpClient(_server); var req = new HttpRequestMessage(HttpMethod.Get, _url + "etag_match_304"); @@ -274,9 +279,9 @@ public void can_handle_ihttpactionresult_with_default_media_type() var client = new HttpClient(_server); var result = client.GetAsync(_url + "Get_ihttpactionresult").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult")), Times.Once()); } [Test] @@ -287,9 +292,9 @@ public void can_handle_ihttpactionresult_with_non_default_media_type() req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); var result = client.SendAsync(req).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:text/xml; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:text/xml; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult:text/xml; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_ihttpactionresult")), Times.Once()); } @@ -299,8 +304,8 @@ public void can_handle_media_type_when_cache_has_expired_during_request() var client = new HttpClient(_server); var req = new HttpRequestMessage(HttpMethod.Get, _url + "Get_ihttpactionresult"); req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml")); - _cache.Setup(o => o.Contains(It.IsAny())).Returns(true); - _cache.Setup(o => o.Get(It.Is((string key) => key.Contains(Constants.ContentTypeKey)))).Returns((MediaTypeHeaderValue)null); + _cache.Setup(o => o.ContainsAsync(It.IsAny())).Returns(Task.FromResult(true)); + _cache.Setup(o => o.GetAsync(It.Is((string key) => key.Contains(Constants.ContentTypeKey)))).Returns(Task.FromResult((MediaTypeHeaderValue)null)); var result = client.SendAsync(req).Result; Assert.That(result.IsSuccessStatusCode, Is.True); } @@ -311,9 +316,9 @@ public void will_cache_if_cacheouput_present_on_controller() var client = new HttpClient(_server); var result = client.GetAsync("http://www.strathweb.com/api/ignore/cached").Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached:application/json; charset=utf-8")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached:application/json; charset=utf-8")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached:application/json; charset=utf-8"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.ignorecontroller-cached")), Times.Once()); } [Test] @@ -322,9 +327,9 @@ public void will_not_cache_if_cacheouput_present_on_controller_but_action_has_ig var client = new HttpClient(_server); var result = client.GetAsync("http://www.strathweb.com/api/ignore/uncached").Result; - _cache.Verify(s => s.Contains(It.IsAny()), Times.Never()); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); - _cache.Verify(s => s.Add(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.ContainsAsync(It.IsAny()), Times.Never()); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); + _cache.Verify(s => s.AddAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Never()); } //[Test] @@ -333,9 +338,9 @@ public void will_not_cache_if_cacheouput_present_on_controller_but_action_has_ig // var client = new HttpClient(_server); // var result = client.GetAsync(_url + "cachekey/get_custom_key").Result; - // _cache.Verify(s => s.Contains(It.Is(x => x == "custom_key")), Times.Exactly(2)); - // _cache.Verify(s => s.Add(It.Is(x => x == "custom_key"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "cachekey-get_custom_key")), Times.Once()); - // _cache.Verify(s => s.Add(It.Is(x => x == "custom_key:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "cachekey-get_custom_key")), Times.Once()); + // _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "custom_key")), Times.Exactly(2)); + // _cache.Verify(s => s.AddAsync(It.Is(x => x == "custom_key"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "cachekey-get_custom_key")), Times.Once()); + // _cache.Verify(s => s.AddAsync(It.Is(x => x == "custom_key:response-ct"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(100)), It.Is(x => x == "cachekey-get_custom_key")), Times.Once()); //} [Test] @@ -345,9 +350,9 @@ public void override_mediatype() var req = new HttpRequestMessage(HttpMethod.Get, _url + "Get_c50_s50_image"); var result = client.SendAsync(req).Result; - _cache.Verify(s => s.Contains(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image:image/jpeg")), Times.Exactly(2)); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); - _cache.Verify(s => s.Add(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image:image/jpeg"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image")), Times.Once()); + _cache.Verify(s => s.ContainsAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image:image/jpeg")), Times.Exactly(2)); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), null), Times.Once()); + _cache.Verify(s => s.AddAsync(It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image:image/jpeg"), It.IsAny(), It.Is(x => x <= DateTime.Now.AddSeconds(50)), It.Is(x => x == "webapi.outputcache.v2.tests.testcontrollers.samplecontroller-get_c50_s50_image")), Times.Once()); Assert.That(result.Content.Headers.ContentType, Is.EqualTo(new MediaTypeHeaderValue("image/jpeg"))); } diff --git a/test/WebApi.OutputCache.V2.Tests/TestControllers/InlineInvalidateController.cs b/test/WebApi.OutputCache.V2.Tests/TestControllers/InlineInvalidateController.cs index d3f58cf..536952a 100644 --- a/test/WebApi.OutputCache.V2.Tests/TestControllers/InlineInvalidateController.cs +++ b/test/WebApi.OutputCache.V2.Tests/TestControllers/InlineInvalidateController.cs @@ -1,4 +1,5 @@ -using System.Web.Http; +using System.Threading.Tasks; +using System.Web.Http; namespace WebApi.OutputCache.V2.Tests.TestControllers { @@ -36,32 +37,32 @@ public string etag_match_304() return "value"; } - public void Post() + public async Task Post() { var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request); - cache.RemoveStartsWith(Configuration.CacheOutputConfiguration().MakeBaseCachekey(this.GetType().FullName, "Get_c100_s100")); + await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey(this.GetType().FullName, "Get_c100_s100")); //do nothing } - public void Put() + public async Task Put() { var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request); - cache.RemoveStartsWith(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100())); + await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100())); //do nothing } - public void Delete_non_standard_name() + public async Task Delete_non_standard_name() { var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request); - cache.RemoveStartsWith(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100(7))); + await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100(7))); } - public void Delete_parameterized() + public async Task Delete_parameterized() { var cache = Configuration.CacheOutputConfiguration().GetCacheOutputProvider(Request); - cache.RemoveStartsWith(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100_with_param(7))); + await cache.RemoveStartsWithAsync(Configuration.CacheOutputConfiguration().MakeBaseCachekey((InlineInvalidateController x) => x.Get_c100_s100_with_param(7))); //do nothing } From a2b7597d08edd030f19db5864e4497e1433e4124 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 21 Jan 2019 20:54:09 +0100 Subject: [PATCH 2/3] Remove Task.Run and return Task.FromResult(0) --- .../Cache/MemoryCacheDefault.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs b/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs index 9e81559..90397b2 100644 --- a/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs +++ b/src/WebApi.OutputCache.Core/Cache/MemoryCacheDefault.cs @@ -54,7 +54,7 @@ private static void Add(string key, object o, DateTimeOffset expiration, string { Cache.Add(key, o, cachePolicy); } - } + } public virtual Task> AllKeysAsync { @@ -66,7 +66,8 @@ public virtual Task> AllKeysAsync public virtual Task RemoveStartsWithAsync(string key) { - return Task.Run(() => RemoveStartsWith(key)); + RemoveStartsWith(key); + return Task.FromResult(0); } public virtual Task GetAsync(string key) where T : class @@ -76,7 +77,8 @@ public virtual Task GetAsync(string key) where T : class public virtual Task RemoveAsync(string key) { - return Task.Run(() => Remove(key)); + Remove(key); + return Task.FromResult(0); } public virtual Task ContainsAsync(string key) @@ -86,7 +88,8 @@ public virtual Task ContainsAsync(string key) public virtual Task AddAsync(string key, object value, DateTimeOffset expiration, string dependsOnKey = null) { - return Task.Run(() => Add(key, value, expiration, dependsOnKey)); + Add(key, value, expiration, dependsOnKey); + return Task.FromResult(0); } } } From 138af2f3250bb8715b731f0769ee8604d5695f29 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Tue, 22 Jan 2019 23:11:22 +0100 Subject: [PATCH 3/3] Await added method due to rebase --- src/WebApi.OutputCache.V2/CacheOutputAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs index 825c66c..8eac9d0 100644 --- a/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs +++ b/src/WebApi.OutputCache.V2/CacheOutputAttribute.cs @@ -274,7 +274,7 @@ await _webApiCache.AddAsync(cachekey + Constants.EtagKey, cacheTime.AbsoluteExpiration, baseKey); - _webApiCache.Add(cachekey + Constants.GenerationTimestampKey, + await _webApiCache.AddAsync(cachekey + Constants.GenerationTimestampKey, actionExecutionTimestamp.ToString(), cacheTime.AbsoluteExpiration, baseKey); }