public async Task CachesSettings() { SetCacheTime(ODataCachedEndpoint.GetSpecificPackage, 300); var target = new ODataCacheOutputAttribute(ODataCachedEndpoint.GetSpecificPackage, 100) { ReloadDuration = TimeSpan.FromHours(1), }; target.OnActionExecuting(ActionContext); await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); SetCacheTime(ODataCachedEndpoint.GetSpecificPackage, 600); Cache.ResetCalls(); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(300) <= e && e <= after.AddSeconds(300)), null), Times.Once); }
public async Task RevertsToDefaultValueWhenFeatureFlagIsTurnedOff() { SetCacheTime(ODataCachedEndpoint.GetSpecificPackage, 600); var target = new ODataCacheOutputAttribute(ODataCachedEndpoint.GetSpecificPackage, 100) { ReloadDuration = TimeSpan.Zero, }; target.OnActionExecuting(ActionContext); await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); FeatureFlagService.Setup(x => x.AreDynamicODataCacheDurationsEnabled()).Returns(false); Cache.ResetCalls(); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(100) <= e && e <= after.AddSeconds(100)), null), Times.Once); }
public async Task HasCorrectDefaultConfiguredCacheTime(ODataCachedEndpoint endpoint) { var cacheTime = DefaultCacheTime[endpoint]; var target = new ODataCacheOutputAttribute(endpoint, 0); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; if (cacheTime <= 0) { Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never); } else { Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(cacheTime) <= e && e <= after.AddSeconds(cacheTime)), null), Times.Once); } }
public async Task CanUseZeroFromConfiguration() { SetCacheTime(ODataCachedEndpoint.GetSpecificPackage, 0); var target = new ODataCacheOutputAttribute(ODataCachedEndpoint.GetSpecificPackage, 100); target.OnActionExecuting(ActionContext); await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never); }
public async Task CanUseZeroWhenConfigurationIsTurnedOff() { SetCacheTime(ODataCachedEndpoint.GetSpecificPackage, 600); var target = new ODataCacheOutputAttribute(ODataCachedEndpoint.GetSpecificPackage, 0); FeatureFlagService.Setup(x => x.AreDynamicODataCacheDurationsEnabled()).Returns(false); target.OnActionExecuting(ActionContext); await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), It.IsAny <string>()), Times.Never); }
public async Task DoesNotCacheHijackTrueAndGetSpecificPackage() { ActionContext.ActionArguments["hijack"] = false; var target = new ODataCacheOutputAttribute(ODataCachedEndpoint.GetSpecificPackage, 100); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), null), Times.Never); }
public async Task CacheHijackFalseOrNonGetSpecificPackage(ODataCachedEndpoint endpoint, bool hijack) { ActionContext.ActionArguments["hijack"] = hijack; var target = new ODataCacheOutputAttribute(endpoint, 100); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTimeOffset>(), null), Times.Once); }
public async Task ObservesConfiguredValue(ODataCachedEndpoint endpoint) { var cacheTime = 600; SetCacheTime(endpoint, cacheTime); var target = new ODataCacheOutputAttribute(endpoint, 100); target.OnActionExecuting(ActionContext); var before = DateTimeOffset.Now; await target.OnActionExecutedAsync(ActionExecutedContext, CancellationToken.None); var after = DateTimeOffset.Now; Cache.Verify( x => x.Add( It.IsAny <string>(), It.IsAny <object>(), It.Is <DateTimeOffset>(e => before.AddSeconds(cacheTime) <= e && e <= after.AddSeconds(cacheTime)), null), Times.Once); }