diff --git a/build/releasenotes.props b/build/releasenotes.props index 5cba50e4..1fe50f01 100644 --- a/build/releasenotes.props +++ b/build/releasenotes.props @@ -1,13 +1,13 @@ + 1. Support IServiceProvider in setupActions 1. Upgrading dependencies. - 1. Fixed redis distributed lock for different process. - 2. Support code configuration for StackExchange.Redis. + 1. Upgrading dependencies. 1. Upgrading dependencies. @@ -40,25 +40,22 @@ 1. Upgrading dependencies. - 1. Support async version of subscribe. + 1. Upgrading dependencies. - 1. Support code configuration for StackExchange.Redis. - 2. Support async version of subscribe. + 1. Upgrading dependencies. - 1. Support async version of subscribe. + 1. Upgrading dependencies. - 1. Support async version of subscribe. + 1. Upgrading dependencies. - 1. Support async version of subscribe. - 2. Rename WithConfluentKafkaBus to WithZookeeeperBus. + 1. Upgrading dependencies. - 1. Support custom serializer. - 2. Fixed DiskCachingProvider async warning. + 1. Upgrading dependencies. 1. Upgrading dependencies. diff --git a/build/version.props b/build/version.props index 8cadff5e..5dc47ec6 100644 --- a/build/version.props +++ b/build/version.props @@ -1,28 +1,28 @@ - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 - 1.9.0 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 + 1.9.1 diff --git a/bus/EasyCaching.Bus.Redis/Configurations/EasyCachingOptionsExtensions.cs b/bus/EasyCaching.Bus.Redis/Configurations/EasyCachingOptionsExtensions.cs index 46f35f99..99b753e9 100644 --- a/bus/EasyCaching.Bus.Redis/Configurations/EasyCachingOptionsExtensions.cs +++ b/bus/EasyCaching.Bus.Redis/Configurations/EasyCachingOptionsExtensions.cs @@ -58,6 +58,7 @@ void configure(RedisBusOptions x) x.Username = redisOptions.Username; x.Password = redisOptions.Password; x.SslHost = redisOptions.SslHost; + x.SerializerName = redisOptions.SerializerName; foreach (var item in redisOptions.Endpoints) x.Endpoints.Add(item); } diff --git a/docs/In-Memory.md b/docs/In-Memory.md index 76e44463..9174afba 100644 --- a/docs/In-Memory.md +++ b/docs/In-Memory.md @@ -46,7 +46,7 @@ public class Startup // below two settings are added in v0.8.0 // enable deep clone when reading object from cache or not, default value is true. EnableReadDeepClone = true, - // enable deep clone when writing object to cache or not, default valuee is false. + // enable deep clone when writing object to cache or not, default value is false. EnableWriteDeepClone = false, }; // the max random second will be added to cache's expiration, default value is 120 diff --git a/interceptor/EasyCaching.Interceptor.AspectCore/EasyCaching.Interceptor.AspectCore.csproj b/interceptor/EasyCaching.Interceptor.AspectCore/EasyCaching.Interceptor.AspectCore.csproj index 9170f032..5cd7f675 100644 --- a/interceptor/EasyCaching.Interceptor.AspectCore/EasyCaching.Interceptor.AspectCore.csproj +++ b/interceptor/EasyCaching.Interceptor.AspectCore/EasyCaching.Interceptor.AspectCore.csproj @@ -35,7 +35,7 @@ - - + + diff --git a/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs b/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs index a3f105bc..2873dad9 100644 --- a/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs +++ b/src/EasyCaching.Core/Configurations/EasyCachingServiceCollectionExtensions.cs @@ -1,8 +1,8 @@ namespace Microsoft.Extensions.DependencyInjection { using EasyCaching.Core; - using EasyCaching.Core.Configurations; - using System; + using EasyCaching.Core.Configurations; + using System; /// /// EasyCaching service collection extensions. @@ -10,7 +10,7 @@ public static class EasyCachingServiceCollectionExtensions { /// - /// Adds the easycaching. + /// Adds the EasyCaching. /// /// The easy caching. /// Services. @@ -30,5 +30,38 @@ public static IServiceCollection AddEasyCaching(this IServiceCollection services return services; } + + /// + /// Adds the EasyCaching. + /// + /// The easy caching. + /// Services. + /// Setup action. + public static IServiceCollection AddEasyCaching(this IServiceCollection services, Action setupAction) + { + ArgumentCheck.NotNull(setupAction, nameof(setupAction)); + + // Options + services.AddSingleton(sp => + { + var options = new EasyCachingOptions(); + setupAction(sp, options); + return options; + }); + + // Extension services + services.AddSingleton(sp => + { + var options = sp.GetRequiredService(); + foreach (var serviceExtension in options.Extensions) + { + serviceExtension.AddServices(services); + } + + return options; + }); + + return services; + } } } diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs index 3c2d696d..96b7050e 100644 --- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs +++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs @@ -159,11 +159,12 @@ public override async Task BaseGetAsync(string cacheKey, Type type, Canc /// The async. /// Cache key. /// CancellationToken - public override async Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) + public override Task BaseRemoveAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - await Task.Run(() => { _cache.Remove(cacheKey); }, cancellationToken); + _cache.Remove(cacheKey); + return Task.CompletedTask; } /// @@ -175,7 +176,7 @@ public override async Task BaseRemoveAsync(string cacheKey, CancellationToken ca /// Expiration. /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) + public override Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNull(cacheValue, nameof(cacheValue), _options.CacheNulls); @@ -187,12 +188,10 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp expiration = expiration.Add(TimeSpan.FromSeconds(addSec)); } - await Task.Run(() => - { - //var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2); - //var val = new CacheValue(cacheValue, true, valExpiration); - _cache.Set(cacheKey, cacheValue, expiration); - }, cancellationToken); + //var valExpiration = expiration.Seconds <= 1 ? expiration : TimeSpan.FromSeconds(expiration.Seconds / 2); + //var val = new CacheValue(cacheValue, true, valExpiration); + _cache.Set(cacheKey, cacheValue, expiration); + return Task.CompletedTask; } /// @@ -201,11 +200,11 @@ await Task.Run(() => /// The async. /// Cache key. /// CancellationToken - public override async Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) + public override Task BaseExistsAsync(string cacheKey, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); - return await Task.FromResult(_cache.Exists(cacheKey)); + return Task.FromResult(_cache.Exists(cacheKey)); } /// @@ -214,14 +213,15 @@ public override async Task BaseExistsAsync(string cacheKey, CancellationTo /// The by prefix async. /// Prefix. /// CancellationToken - public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) + public override Task BaseRemoveByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); - var count = await Task.Run(() => _cache.RemoveByPrefix(prefix), cancellationToken); + var count = _cache.RemoveByPrefix(prefix); if (_options.EnableLogging) _logger?.LogInformation($"RemoveByPrefixAsync : prefix = {prefix} , count = {count}"); + return Task.CompletedTask; } /// @@ -230,17 +230,18 @@ public override async Task BaseRemoveByPrefixAsync(string prefix, CancellationTo /// The by prefix async. /// Pattern. /// CancellationToken - public override async Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default) + public override Task BaseRemoveByPatternAsync(string pattern, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(pattern, nameof(pattern)); var searchPattern = this.ProcessSearchKeyPattern(pattern); var searchKey = this.HandleSearchKeyPattern(pattern); - - var count = await Task.Run(() => _cache.RemoveByPattern(searchKey, searchPattern), cancellationToken); + + var count = _cache.RemoveByPattern(searchKey, searchPattern); if (_options.EnableLogging) _logger?.LogInformation($"BaseRemoveByPatternAsync : pattern = {pattern} , count = {count}"); + return Task.CompletedTask; } /// @@ -251,12 +252,13 @@ public override async Task BaseRemoveByPatternAsync(string pattern, Cancellation /// Expiration. /// CancellationToken /// The 1st type parameter. - public override async Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) + public override Task BaseSetAllAsync(IDictionary values, TimeSpan expiration, CancellationToken cancellationToken = default) { ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); ArgumentCheck.NotNullAndCountGTZero(values, nameof(values)); - await Task.Run(() => _cache.SetAll(values, expiration), cancellationToken); + _cache.SetAll(values, expiration); + return Task.CompletedTask; } /// @@ -266,16 +268,16 @@ public override async Task BaseSetAllAsync(IDictionary values, Tim /// Cache keys. /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) + public override Task>> BaseGetAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); if (_options.EnableLogging) _logger?.LogInformation($"GetAllAsync : cacheKeys = {string.Join(",", cacheKeys)}"); - return await Task.FromResult(_cache.GetAll(cacheKeys)); + return Task.FromResult(_cache.GetAll(cacheKeys)); } - + /// /// Get all cacheKey by prefix async. @@ -298,7 +300,7 @@ public override Task> BaseGetAllKeysByPrefixAsync(string pre /// Prefix. /// CancellationToken /// The 1st type parameter. - public override async Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) + public override Task>> BaseGetByPrefixAsync(string prefix, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullOrWhiteSpace(prefix, nameof(prefix)); var map = new Dictionary>(); @@ -306,7 +308,7 @@ public override async Task>> BaseGetByPrefixAs if (_options.EnableLogging) _logger?.LogInformation($"GetByPrefixAsync : prefix = {prefix}"); - return await Task.FromResult(_cache.GetByPrefix(prefix)); + return Task.FromResult(_cache.GetByPrefix(prefix)); } /// @@ -315,14 +317,15 @@ public override async Task>> BaseGetByPrefixAs /// The all async. /// Cache keys. /// CancellationToken - public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) + public override Task BaseRemoveAllAsync(IEnumerable cacheKeys, CancellationToken cancellationToken = default) { ArgumentCheck.NotNullAndCountGTZero(cacheKeys, nameof(cacheKeys)); if (_options.EnableLogging) _logger?.LogInformation($"RemoveAllAsync : cacheKeys = {string.Join(",", cacheKeys)}"); - await Task.Run(() => _cache.RemoveAll(cacheKeys), cancellationToken); + _cache.RemoveAll(cacheKeys); + return Task.CompletedTask; } /// @@ -330,13 +333,13 @@ public override async Task BaseRemoveAllAsync(IEnumerable cacheKeys, Can /// /// The async. /// CancellationToken - public override async Task BaseFlushAsync(CancellationToken cancellationToken = default) + public override Task BaseFlushAsync(CancellationToken cancellationToken = default) { if (_options.EnableLogging) _logger?.LogInformation("FlushAsync"); _cache.Clear(); - await Task.CompletedTask; + return Task.CompletedTask; } /// diff --git a/src/EasyCaching.LiteDB/EasyCaching.LiteDB.csproj b/src/EasyCaching.LiteDB/EasyCaching.LiteDB.csproj index 90a65259..2f6f6724 100644 --- a/src/EasyCaching.LiteDB/EasyCaching.LiteDB.csproj +++ b/src/EasyCaching.LiteDB/EasyCaching.LiteDB.csproj @@ -34,7 +34,7 @@ - + diff --git a/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderTest.cs deleted file mode 100644 index 32f3e5dc..00000000 --- a/test/EasyCaching.UnitTests/CachingTests/BaseCachingProviderTest.cs +++ /dev/null @@ -1,1555 +0,0 @@ -namespace EasyCaching.UnitTests -{ - using EasyCaching.Core; - using EasyCaching.Core.Configurations; - using FakeItEasy; - using System; - using System.Collections.Generic; - using System.Linq; - using System.Threading.Tasks; - using Xunit; - - public abstract class BaseCachingProviderTest - { - protected IEasyCachingProvider _provider; - protected IEasyCachingProvider _providerWithNullsCached; - - protected TimeSpan _defaultTs; - protected string _nameSpace = string.Empty; - - protected BaseCachingProviderTest() - { - _provider = CreateCachingProvider(options => options.SleepMs = 3000); - _providerWithNullsCached = CreateCachingProvider(options => options.CacheNulls = true); - } - - protected abstract IEasyCachingProvider CreateCachingProvider(Action additionalSetup); - - #region Parameter Check Test - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Set_Value_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - var cacheVlaue = "value"; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, _defaultTs)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Set_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - var cacheVlaue = "value"; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public void Set_Value_Should_Throw_ArgumentNullException_When_CacheValue_IsNull() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = null; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public async Task Set_Value_Async_Should_Throw_ArgumentNullException_When_CacheValue_IsNull() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = null; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public void Set_Value_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Zero() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = "123"; - var expiration = TimeSpan.Zero; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public async Task Set_Value_Async_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Zero() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = "123"; - var expiration = TimeSpan.Zero; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public void Set_Value_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Negative() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = "123"; - var expiration = new TimeSpan(0, 0, -1); - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public async Task Set_Value_Async_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Negative() - { - var cacheKey = Guid.NewGuid().ToString(); - string cacheVlaue = "123"; - var expiration = new TimeSpan(0, 0, -1); - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, expiration)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Get_Cached_Value_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cachekey) - { - Assert.Throws(() => _provider.Get(cachekey, null, _defaultTs)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Get_Cached_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cachekey) - { - await Assert.ThrowsAsync(async () => await _provider.GetAsync(cachekey, null, _defaultTs)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Get_Cached_Value_Without_Retriever_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cachekey) - { - Assert.Throws(() => _provider.Get(cachekey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Get_Cached_Value_Without_Retriever_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cachekey) - { - await Assert.ThrowsAsync(async () => await _provider.GetAsync(cachekey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Remove_Cached_Value_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - Assert.Throws(() => _provider.Remove(cacheKey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Remove_Cached_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - await Assert.ThrowsAsync(async () => await _provider.RemoveAsync(cacheKey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Exists_Cached_Value_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - Assert.Throws(() => _provider.Exists(cacheKey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Exists_Cached_Value_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - await Assert.ThrowsAsync(async () => await _provider.ExistsAsync(cacheKey)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public void Refresh_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - var cacheVlaue = "value"; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, _defaultTs)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - public async Task Refresh_Async_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string cacheKey) - { - var cacheVlaue = "value"; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public void Refresh_Should_Throw_ArgumentNullException_When_CacheValue_IsNull() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = null; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public async Task Refresh_Async_Should_Throw_ArgumentNullException_When_CacheValue_IsNull() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = null; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, _defaultTs)); - } - - [Fact] - public void Refresh_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Zero() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = "123"; - var expiration = TimeSpan.Zero; - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public async Task Refresh_Async_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Zero() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = "123"; - var expiration = TimeSpan.Zero; - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public void Refresh_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Negative() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = "123"; - var expiration = new TimeSpan(0, 0, -1); - Assert.Throws(() => _provider.Set(cacheKey, cacheVlaue, expiration)); - } - - [Fact] - public async Task Refresh_Async_Should_Throw_ArgumentOutOfRangeException_When_Expiration_Is_Negative() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - string cacheVlaue = "123"; - var expiration = new TimeSpan(0, 0, -1); - await Assert.ThrowsAsync(async () => await _provider.SetAsync(cacheKey, cacheVlaue, expiration)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - protected virtual void RemoveByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string prefix) - { - Assert.Throws(() => _provider.RemoveByPrefix(prefix)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - protected async virtual Task RemoveByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace(string preifx) - { - await Assert.ThrowsAsync(async () => await _provider.RemoveByPrefixAsync(preifx)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - protected virtual void GetAllByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace(string prefix) - { - Assert.Throws(() => _provider.RemoveByPrefix(prefix)); - } - - [Theory] - [InlineData("")] - [InlineData(" ")] - [InlineData(null)] - protected virtual async Task GetAllByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace(string preifx) - { - await Assert.ThrowsAsync(async () => await _provider.RemoveByPrefixAsync(preifx)); - } - #endregion - - #region Set/SetAsync - [Fact] - public void Set_Value_And_Get_Cached_Value_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-sg-{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue, val.Value); - - _provider.Remove(cacheKey); - } - - [Fact] - public void Set_Value_And_Get_Cached_Value_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sg-{Guid.NewGuid().ToString()}"; - string cacheValue = null; - - _providerWithNullsCached.Set(cacheKey, cacheValue, _defaultTs); - - var val = _providerWithNullsCached.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - _providerWithNullsCached.Remove(cacheKey); - } - - [Fact] - public async Task Set_Value_And_Get_Cached_Value_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-sgasync{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _provider.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue, val.Value); - - await _provider.RemoveAsync(cacheKey); - } - - [Fact] - public async Task Set_Value_And_Get_Cached_Value_Async_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sg-{Guid.NewGuid().ToString()}"; - string cacheValue = null; - - await _providerWithNullsCached.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _providerWithNullsCached.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - await _providerWithNullsCached.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void Set_Object_Value_And_Get_Cached_Value_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-sog-{Guid.NewGuid().ToString()}"; - var cacheValue = new Product { Id = 999, Name = "product999" }; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue.Id, val.Value.Id); - Assert.Equal(cacheValue.Name, val.Value.Name); - - _provider.Remove(cacheKey); - } - - [Fact] - protected virtual void Set_Object_Value_And_Get_Cached_Value_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sog-{Guid.NewGuid().ToString()}"; - Product cacheValue = null; - - _providerWithNullsCached.Set(cacheKey, cacheValue, _defaultTs); - - var val = _providerWithNullsCached.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - _providerWithNullsCached.Remove(cacheKey); - } - - [Fact] - protected virtual async Task Set_Object_Value_And_Get_Cached_Value_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-sogasync-{Guid.NewGuid().ToString()}"; - var cacheValue = new Product { Id = 999, Name = "product999" }; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _provider.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue.Id, val.Value.Id); - Assert.Equal(cacheValue.Name, val.Value.Name); - - await _provider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task Set_Object_Value_And_Get_Cached_Value_Async_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sogasync-{Guid.NewGuid().ToString()}"; - Product cacheValue = null; - - await _providerWithNullsCached.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _providerWithNullsCached.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - await _providerWithNullsCached.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void Set_And_Get_Value_Type_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-svg-{Guid.NewGuid().ToString()}"; - var cacheValue = 100; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue, val.Value); - - _provider.Remove(cacheKey); - } - - [Fact] - protected virtual void Set_And_Get_Value_Type_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-svg-{Guid.NewGuid().ToString()}"; - int? cacheValue = null; - - _providerWithNullsCached.Set(cacheKey, cacheValue, _defaultTs); - - var val = _providerWithNullsCached.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - _providerWithNullsCached.Remove(cacheKey); - } - - [Fact] - protected virtual async Task Set_And_Get_Value_Type_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-svgasync-{Guid.NewGuid().ToString()}"; - var cacheValue = 100; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _provider.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue, val.Value); - - await _provider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task Set_And_Get_Value_Type_Async_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-svgasync-{Guid.NewGuid().ToString()}"; - int? cacheValue = null; - - await _providerWithNullsCached.SetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _providerWithNullsCached.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - await _providerWithNullsCached.RemoveAsync(cacheKey); - } - #endregion - - #region Get/GetAsync - [Fact] - public void Get_Not_Cached_Value_Should_Call_Retriever() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var func = Create_Fake_Retriever_Return_String(); - - var res = _provider.Get(cacheKey, func, _defaultTs); - - A.CallTo(() => func.Invoke()).MustHaveHappened(); - } - - [Fact] - public async Task Get_Not_Cached_Value_Async_Should_Call_Retriever() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var func = Create_Fake_Retriever_Return_String_Async(); - - var res = await _provider.GetAsync(cacheKey, func, _defaultTs); - - A.CallTo(() => func.Invoke()).MustHaveHappened(); - } - - [Fact] - public void Get_Not_Cached_Value_Should_Call_Retriever_And_Return_Null_Without_Caching() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_NULL(); - - var res = _provider.Get(cacheKey, func, _defaultTs); - - Assert.Equal(default(string), res.Value); - var cachedValue = _provider.Get(cacheKey); - Assert.False(cachedValue.HasValue); - Assert.Null(cachedValue.Value); - } - - [Fact] - public async Task Get_Not_Cached_Value_Async_Should_Call_Retriever_And_Return_Null() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_NULL_Async(); - - var res = await _provider.GetAsync(cacheKey, func, _defaultTs); - - Assert.Equal(default(string), res.Value); - } - - [Fact] - public void Get_Not_Cached_Value_Should_Call_Retriever_And_Return_Null_With_Caching_When_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_NULL(); - - var res = _providerWithNullsCached.Get(cacheKey, func, _defaultTs); - - Assert.Equal(default(string), res.Value); - var cachedValue = _providerWithNullsCached.Get(cacheKey); - Assert.True(cachedValue.HasValue); - Assert.Null(cachedValue.Value); - } - - [Fact] - public async Task Get_Cached_Value_Async_Should_Call_Retriever_And_Return_String_With_Caching_When_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_String_Async(); - - var res = await _providerWithNullsCached.GetAsync(cacheKey, func, _defaultTs); - - Assert.Equal("123", res.Value); - var cachedValue = await _providerWithNullsCached.GetAsync(cacheKey); - Assert.True(cachedValue.HasValue); - Assert.NotNull(cachedValue.Value); - Assert.Equal("123", cachedValue.Value); - } - - [Fact] - public void Get_Cached_Value_Should_Not_Call_Retriever() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_String(); - var cacheVlaue = "Memory"; - - _provider.Set(cacheKey, cacheVlaue, _defaultTs); - var res = _provider.Get(cacheKey, func, _defaultTs); - - A.CallTo(() => func.Invoke()).MustNotHaveHappened(); - } - - [Fact] - public async Task Get_Cached_Value_Async_Should_Not_Call_Retriever() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_String_Async(); - var cacheVlaue = "Memory"; - - await _provider.SetAsync(cacheKey, cacheVlaue, _defaultTs); - var res = await _provider.GetAsync(cacheKey, func, _defaultTs); - - A.CallTo(() => func.Invoke()).MustNotHaveHappened(); - } - - [Fact] - public void Get_Not_Cached_Value_Should_Call_Retriever_And_Return_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_String(); - - var res = _provider.Get(cacheKey, func, _defaultTs); - - Assert.Equal("123", res.Value); - } - - [Fact] - public async Task Get_Not_Cached_Value_Async_Should_Call_Retriever_And_Return_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var func = Create_Fake_Retriever_Return_String_Async(); - - var res = await _provider.GetAsync(cacheKey, func, _defaultTs); - - Assert.Equal("123", res.Value); - } - - [Fact] - public void Get_Not_Cached_Value_Without_Retriever_Should_Return_Default_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var res = _provider.Get(cacheKey); - - Assert.Equal(default(string), res.Value); - } - - [Fact] - public async Task Get_Not_Cached_Value_Without_Retriever_Async_Should_Return_Default_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var res = await _provider.GetAsync(cacheKey); - - Assert.Equal(default(string), res.Value); - } - - [Fact] - public void Get_Cached_Value_Without_Retriever_Should_Return_Default_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - _provider.Set(cacheKey, "123", _defaultTs); - - var res = _provider.Get(cacheKey); - - Assert.Equal("123", res.Value); - } - - [Fact] - public async Task Get_Cached_Value_Without_Retriever_Async_Should_Return_Default_Value() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - await _provider.SetAsync(cacheKey, "123", _defaultTs); - - var res = await _provider.GetAsync(cacheKey); - - Assert.Equal("123", res.Value); - } - - [Fact] - public void Get_Cached_Value_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - var val = _provider.Get(cacheKey, null, _defaultTs); - - Assert.Equal(cacheValue, val.Value); - } - - [Fact] - public async Task Get_Cached_Value_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - var val = await _provider.GetAsync(cacheKey, null, _defaultTs); - - Assert.Equal(cacheValue, val.Value); - } - - [Fact] - public void Get_Cached_Value_Without_Retriever_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - var val = _provider.Get(cacheKey); - - Assert.Equal(cacheValue, val.Value); - } - - [Fact] - public async Task Get_Cached_Value_Without_Retriever_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - var val = await _provider.GetAsync(cacheKey); - - Assert.Equal(cacheValue, val.Value); - } - - [Fact] - public void Get_Not_Cached_Value_Without_Retriever_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var val = _provider.Get(cacheKey); - - Assert.Equal(default(string), val.Value); - } - - [Fact] - public async Task Get_Not_Cached_Value_Without_Retriever_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var val = await _provider.GetAsync(cacheKey); - - Assert.Equal(default(string), val.Value); - } - - [Fact] - protected virtual void Get_Parallel_Should_Succeed() - { - var cacheKey = $"{_nameSpace}parallel{Guid.NewGuid().ToString()}"; - - int count = 0; - - Parallel.For(0, 20, x => - { - _provider.Get(cacheKey, () => - { - System.Threading.Interlocked.Increment(ref count); - return 1; - }, _defaultTs); - }); - - if (_provider.UseLock) Assert.Equal(1, count); - else Assert.InRange(count, 1, 5); - } - - [Fact] - protected virtual async Task GetAsync_Parallel_Should_Succeed() - { - var cacheKey = $"{_nameSpace}parallelasync{Guid.NewGuid().ToString()}"; - int count = 0; - - var tasks = Enumerable.Range(0, 20) - .Select(i => _provider.GetAsync(cacheKey, () => - { - System.Threading.Interlocked.Increment(ref count); - return Task.FromResult(1); - }, _defaultTs)); - - await Task.WhenAll(tasks); - - if (_provider.UseLock) Assert.Equal(1, count); - else Assert.InRange(count, 1, 5); - } - #endregion - - #region Remove/RemoveAsync - [Fact] - public void Remove_Cached_Value_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - _provider.Set(cacheKey, cacheValue, _defaultTs); - var valBeforeRemove = _provider.Get(cacheKey, null, _defaultTs); - Assert.NotNull(valBeforeRemove); - - _provider.Remove(cacheKey); - var valAfterRemove = _provider.Get(cacheKey, () => "123", _defaultTs); - Assert.Equal("123", valAfterRemove.Value); - } - - [Fact] - public async Task Remove_Cached_Value_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - var valBeforeRemove = await _provider.GetAsync(cacheKey, () => null, _defaultTs); - Assert.NotNull(valBeforeRemove); - - await _provider.RemoveAsync(cacheKey); - var valAfterRemove = await _provider.GetAsync(cacheKey, async () => await Task.FromResult("123"), _defaultTs); - Assert.Equal("123", valAfterRemove.Value); - } - #endregion - - #region Exists/ExistsAsync - [Fact] - public void Exists_Cached_Value_Should_Return_True() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - _provider.Set(cacheKey, cacheValue, _defaultTs); - - var flag = _provider.Exists(cacheKey); - - Assert.True(flag); - } - - [Fact] - public async Task Exists_Cached_Value_Async_Should_Return_True() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue = "value"; - await _provider.SetAsync(cacheKey, cacheValue, _defaultTs); - - var flag = await _provider.ExistsAsync(cacheKey); - - Assert.True(flag); - } - - [Fact] - public void Exists_Cached_Value_Should_Return_False() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var flag = _provider.Exists(cacheKey); - - Assert.False(flag); - } - - [Fact] - public async Task Exists_Cached_Value_Async_Should_Return_False() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var flag = await _provider.ExistsAsync(cacheKey); - - Assert.False(flag); - } - #endregion - - #region RemoveByPrefix/RemoveByPrefixAsync - [Fact] - protected virtual void RemoveByPrefix_Should_Succeed() - { - SetCacheItem($"{_nameSpace}demo:1", "1"); - SetCacheItem($"{_nameSpace}demo:2", "2"); - SetCacheItem($"{_nameSpace}demo:3", "3"); - SetCacheItem($"{_nameSpace}demo:4", "4"); - SetCacheItem($"{_nameSpace}xxx:1", "1"); - - _provider.RemoveByPrefix($"{_nameSpace}demo"); - - var demo1 = _provider.Get($"{_nameSpace}demo:1"); - var demo2 = _provider.Get($"{_nameSpace}demo:2"); - var demo3 = _provider.Get($"{_nameSpace}demo:3"); - var demo4 = _provider.Get($"{_nameSpace}demo:4"); - var xxx1 = _provider.Get($"{_nameSpace}xxx:1"); - - Assert.False(demo1.HasValue); - Assert.False(demo2.HasValue); - Assert.False(demo3.HasValue); - Assert.False(demo4.HasValue); - Assert.True(xxx1.HasValue); - } - - [Fact] - protected virtual async Task RemoveByPrefixAsync_Should_Succeed() - { - SetCacheItem($"{_nameSpace}demo:1#async", "1"); - SetCacheItem($"{_nameSpace}demo:2#async", "2"); - SetCacheItem($"{_nameSpace}demo:3#async", "3"); - SetCacheItem($"{_nameSpace}demo:4#async", "4"); - SetCacheItem($"{_nameSpace}xxx:1#async", "1"); - - await _provider.RemoveByPrefixAsync($"{_nameSpace}demo"); - - var demo1 = _provider.Get($"{_nameSpace}demo:1#async"); - var demo2 = _provider.Get($"{_nameSpace}demo:2#async"); - var demo3 = _provider.Get($"{_nameSpace}demo:3#async"); - var demo4 = _provider.Get($"{_nameSpace}demo:4#async"); - var xxx1 = _provider.Get($"{_nameSpace}xxx:1#async"); - - Assert.False(demo1.HasValue); - Assert.False(demo2.HasValue); - Assert.False(demo3.HasValue); - Assert.False(demo4.HasValue); - Assert.True(xxx1.HasValue); - } - - #endregion - - #region RemoveByPattern/RemoveByPatternAsync - - [Fact] - protected virtual void RemoveByPattern_Should_Succeed() - { - SetCacheItem("garden:pots:flowers", "ok"); - SetCacheItem("garden:pots:flowers:test", "ok"); - SetCacheItem("garden:flowerspots:test", "ok"); - SetCacheItem("boo:foo", "ok"); - SetCacheItem("boo:test:foo", "ok"); - SetCacheItem("sky:birds:bar", "ok"); - SetCacheItem("sky:birds:test:bar", "ok"); - SetCacheItem("akey", "ok"); - - var val1 = _provider.Get("garden:pots:flowers"); - var val2 = _provider.Get("garden:pots:flowers:test"); - var val3 = _provider.Get("garden:flowerspots:test"); - var val4 = _provider.Get("boo:foo"); - var val5 = _provider.Get("boo:test:foo"); - var val6 = _provider.Get("sky:birds:bar"); - var val7 = _provider.Get("sky:birds:test:bar"); - var val8 = _provider.Get("akey"); - - Assert.True(val1.HasValue); - Assert.True(val2.HasValue); - Assert.True(val3.HasValue); - Assert.True(val4.HasValue); - Assert.True(val5.HasValue); - Assert.True(val6.HasValue); - Assert.True(val7.HasValue); - Assert.True(val8.HasValue); - - // contains - _provider.RemoveByPattern("*:pots:*"); - - // postfix - _provider.RemoveByPattern("*foo"); - - // prefix - _provider.RemoveByPattern("sky*"); - - // exact - _provider.RemoveByPattern("akey"); - - var val9 = _provider.Get("garden:pots:flowers"); - var val10 = _provider.Get("garden:pots:flowers:test"); - var val11 = _provider.Get("garden:flowerspots:test"); - var val12 = _provider.Get("boo:foo"); - var val13 = _provider.Get("boo:test:foo"); - var val14 = _provider.Get("sky:birds:bar"); - var val15 = _provider.Get("sky:birds:test:bar"); - var val16 = _provider.Get("akey"); - - Assert.False(val9.HasValue); - Assert.False(val10.HasValue); - Assert.True(val11.HasValue); - Assert.False(val12.HasValue); - Assert.False(val13.HasValue); - Assert.False(val14.HasValue); - Assert.False(val15.HasValue); - Assert.False(val16.HasValue); - } - - [Fact] - protected virtual async Task RemoveByPatternAsync_Should_Succeed() - { - SetCacheItem("garden:pots:flowers", "ok"); - SetCacheItem("garden:pots:flowers:test", "ok"); - SetCacheItem("garden:flowerspots:test", "ok"); - SetCacheItem("boo:foo", "ok"); - SetCacheItem("boo:test:foo", "ok"); - SetCacheItem("sky:birds:bar", "ok"); - SetCacheItem("sky:birds:test:bar", "ok"); - SetCacheItem("akey", "ok"); - - var val1 = _provider.Get("garden:pots:flowers"); - var val2 = _provider.Get("garden:pots:flowers:test"); - var val3 = _provider.Get("garden:flowerspots:test"); - var val4 = _provider.Get("boo:foo"); - var val5 = _provider.Get("boo:test:foo"); - var val6 = _provider.Get("sky:birds:bar"); - var val7 = _provider.Get("sky:birds:test:bar"); - var val8 = _provider.Get("akey"); - - Assert.True(val1.HasValue); - Assert.True(val2.HasValue); - Assert.True(val3.HasValue); - Assert.True(val4.HasValue); - Assert.True(val5.HasValue); - Assert.True(val6.HasValue); - Assert.True(val7.HasValue); - Assert.True(val8.HasValue); - - // contains - await _provider.RemoveByPatternAsync("*:pots:*"); - - // postfix - await _provider.RemoveByPatternAsync("*foo"); - - // prefix - await _provider.RemoveByPatternAsync("sky*"); - - // exact - await _provider.RemoveByPatternAsync("akey"); - - var val9 = _provider.Get("garden:pots:flowers"); - var val10 = _provider.Get("garden:pots:flowers:test"); - var val11 = _provider.Get("garden:flowerspots:test"); - var val12 = _provider.Get("boo:foo"); - var val13 = _provider.Get("boo:test:foo"); - var val14 = _provider.Get("sky:birds:bar"); - var val15 = _provider.Get("sky:birds:test:bar"); - var val16 = _provider.Get("akey"); - - Assert.False(val9.HasValue); - Assert.False(val10.HasValue); - Assert.True(val11.HasValue); - Assert.False(val12.HasValue); - Assert.False(val13.HasValue); - Assert.False(val14.HasValue); - Assert.False(val15.HasValue); - Assert.False(val16.HasValue); - } - #endregion - - #region SetAll/SetAllAsync - [Fact] - protected virtual void SetAll_Should_Succeed() - { - var dict = GetMultiDict($"{_nameSpace}setall:"); - - _provider.SetAll(dict, _defaultTs); - - var res1 = _provider.Get($"{_nameSpace}setall:key:1"); - var res2 = _provider.Get($"{_nameSpace}setall:key:2"); - - Assert.Equal("value1", res1.Value); - Assert.Equal("value2", res2.Value); - } - - [Fact] - protected virtual void SetAll_Should_Succeed_When_One_Of_Values_Is_Null_And_Nulls_Are_Cached() - { - var dict = new Dictionary() - { - [$"{_nameSpace}setallwithnull:key:1"] = "value1", - [$"{_nameSpace}setallwithnull:key:2"] = null, - }; ; - - _providerWithNullsCached.SetAll(dict, _defaultTs); - - var res1 = _providerWithNullsCached.Get($"{_nameSpace}setallwithnull:key:1"); - var res2 = _providerWithNullsCached.Get($"{_nameSpace}setallwithnull:key:2"); - - Assert.Equal("value1", res1.Value); - Assert.True(res2.HasValue); - Assert.Null(res2.Value); - } - - [Fact] - protected virtual async Task SetAllAsync_Should_Succeed() - { - var dict = GetMultiDict($"{_nameSpace}setallasync:"); - - await _provider.SetAllAsync(dict, _defaultTs); - - var res1 = _provider.Get($"{_nameSpace}setallasync:key:1"); - var res2 = _provider.Get($"{_nameSpace}setallasync:key:2"); - - Assert.Equal("value1", res1.Value); - Assert.Equal("value2", res2.Value); - } - - [Fact] - protected virtual async Task SetAllAsync_Should_Succeed_When_One_Of_Values_Is_Null_And_Nulls_Are_Cached() - { - var dict = new Dictionary() - { - [$"{_nameSpace}setallasyncwithnull:key:1"] = "value1", - [$"{_nameSpace}setallasyncwithnull:key:2"] = null, - }; ; - - await _providerWithNullsCached.SetAllAsync(dict, _defaultTs); - - var res1 = _providerWithNullsCached.Get($"{_nameSpace}setallasyncwithnull:key:1"); - var res2 = _providerWithNullsCached.Get($"{_nameSpace}setallasyncwithnull:key:2"); - - Assert.Equal("value1", res1.Value); - Assert.True(res2.HasValue); - Assert.Null(res2.Value); - } - #endregion - - #region GetAll/GetAllAsync - [Fact] - protected virtual void GetAll_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getall:key:1", $"{_nameSpace}getall:key:2" }); - var dict = GetMultiDict($"{_nameSpace}getall:"); - - _provider.SetAll(dict, _defaultTs); - - var res = _provider.GetAll(new List { $"{_nameSpace}getall:key:1", $"{_nameSpace}getall:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getall:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getall:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getall:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal("value2", res.Where(x => x.Key == $"{_nameSpace}getall:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual void GetAll_Should_Succeed_When_One_Of_Values_Is_Null_And_Nulls_Are_Cached() - { - _providerWithNullsCached.RemoveAll(new List { $"{_nameSpace}getall:key:1", $"{_nameSpace}getall:key:2" }); - var dict = new Dictionary() - { - [$"{_nameSpace}getallwithnull:key:1"] = "value1", - [$"{_nameSpace}getallwithnull:key:2"] = null, - }; ; - - _providerWithNullsCached.SetAll(dict, _defaultTs); - - var res = _providerWithNullsCached.GetAll(new List { $"{_nameSpace}getallwithnull:key:1", $"{_nameSpace}getallwithnull:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getallwithnull:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getallwithnull:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getallwithnull:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Null(res.Where(x => x.Key == $"{_nameSpace}getallwithnull:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual async Task GetAllAsync_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getallasync:key:1", $"{_nameSpace}getallasync:key:2" }); - var dict = GetMultiDict($"{_nameSpace}getallasync:"); - - _provider.SetAll(dict, _defaultTs); - - var res = await _provider.GetAllAsync(new List { $"{_nameSpace}getallasync:key:1", $"{_nameSpace}getallasync:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getallasync:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getallasync:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getallasync:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal("value2", res.Where(x => x.Key == $"{_nameSpace}getallasync:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual async Task GetAllAsync_Should_Succeed_When_One_Of_Values_Is_Null_And_Nulls_Are_Cached() - { - _providerWithNullsCached.RemoveAll(new List { $"{_nameSpace}getall:key:1", $"{_nameSpace}getall:key:2" }); - var dict = new Dictionary() - { - [$"{_nameSpace}getallasyncwithnull:key:1"] = "value1", - [$"{_nameSpace}getallasyncwithnull:key:2"] = null, - }; ; - - _providerWithNullsCached.SetAll(dict, _defaultTs); - - var res = await _providerWithNullsCached.GetAllAsync(new List { $"{_nameSpace}getallasyncwithnull:key:1", $"{_nameSpace}getallasyncwithnull:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getallasyncwithnull:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getallasyncwithnull:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getallasyncwithnull:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Null(res.Where(x => x.Key == $"{_nameSpace}getallasyncwithnull:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual void GetAll_With_Value_Type_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getall:valuetype:key:1", $"{_nameSpace}getall:valuetype:key:2" }); - var dict = new Dictionary { { $"{_nameSpace}getall:valuetype:key:1", 10 }, { $"{_nameSpace}getall:valuetype:key:2", 100 } }; - - _provider.SetAll(dict, _defaultTs); - - var res = _provider.GetAll(new List { $"{_nameSpace}getall:valuetype:key:1", $"{_nameSpace}getall:valuetype:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getall:valuetype:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getall:valuetype:key:2", res.Select(x => x.Key)); - Assert.Equal(10, res.Where(x => x.Key == $"{_nameSpace}getall:valuetype:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal(100, res.Where(x => x.Key == $"{_nameSpace}getall:valuetype:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual async Task GetAll_Async_With_Value_Type_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getallasync:valuetype:key:1", $"{_nameSpace}getallasync:valuetype:key:2" }); - var dict = new Dictionary { { $"{_nameSpace}getallasync:valuetype:key:1", 10 }, { $"{_nameSpace}getallasync:valuetype:key:2", 100 } }; - - _provider.SetAll(dict, _defaultTs); - - var res = await _provider.GetAllAsync(new List { $"{_nameSpace}getallasync:valuetype:key:1", $"{_nameSpace}getallasync:valuetype:key:2" }); - - Assert.Equal(2, res.Count); - - Assert.Contains($"{_nameSpace}getallasync:valuetype:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getallasync:valuetype:key:2", res.Select(x => x.Key)); - Assert.Equal(10, res.Where(x => x.Key == $"{_nameSpace}getallasync:valuetype:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal(100, res.Where(x => x.Key == $"{_nameSpace}getallasync:valuetype:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - #endregion - - #region GetByPrefix/GetByPrefixAsync - [Fact] - protected virtual void GetByPrefix_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getbyprefix:key:1", $"{_nameSpace}getbyprefix:key:2" }); - var dict = GetMultiDict($"{_nameSpace}getbyprefix:"); - - _provider.SetAll(dict, _defaultTs); - - string prefix = $"{_nameSpace}getbyprefix:key:"; - - var res = _provider.GetByPrefix(prefix); - - Assert.Equal(2, res.Count); - Assert.Contains($"{_nameSpace}getbyprefix:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getbyprefix:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getbyprefix:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal("value2", res.Where(x => x.Key == $"{_nameSpace}getbyprefix:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual async Task GetByPrefixAsync_Should_Succeed() - { - _provider.RemoveAll(new List { $"{_nameSpace}getbyprefixasync:key:1", $"{_nameSpace}getbyprefixasync:key:2" }); - var dict = GetMultiDict($"{_nameSpace}getbyprefixasync:"); - - _provider.SetAll(dict, _defaultTs); - - string prefix = $"{_nameSpace}getbyprefixasync:key:"; - - var res = await _provider.GetByPrefixAsync(prefix); - - Assert.Equal(2, res.Count); - Assert.Contains($"{_nameSpace}getbyprefixasync:key:1", res.Select(x => x.Key)); - Assert.Contains($"{_nameSpace}getbyprefixasync:key:2", res.Select(x => x.Key)); - Assert.Equal("value1", res.Where(x => x.Key == $"{_nameSpace}getbyprefixasync:key:1").Select(x => x.Value).FirstOrDefault().Value); - Assert.Equal("value2", res.Where(x => x.Key == $"{_nameSpace}getbyprefixasync:key:2").Select(x => x.Value).FirstOrDefault().Value); - } - - [Fact] - protected virtual void GetByPrefix_With_Not_Existed_Prefix_Should_Return_Empty_Dict() - { - var prefix = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var res = _provider.GetByPrefix(prefix); - - Assert.Equal(0, res.Count); - } - - [Fact] - protected virtual async Task GetByPrefixAsync_With_Not_Existed_Prefix_Should_Return_Empty_Dict() - { - var prefix = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - - var res = await _provider.GetByPrefixAsync(prefix); - - Assert.Equal(0, res.Count); - } - - #endregion - - #region RemoveAll/RemoveAllAsync - [Fact] - protected virtual void RemoveAll_Should_Succeed() - { - var dict = GetMultiDict($"{_nameSpace}removeall:"); - - _provider.SetAll(dict, _defaultTs); - - _provider.RemoveAll(new List { $"{_nameSpace}removeall:key:1", $"{_nameSpace}removeall:key:2" }); - - var res1 = _provider.Get($"{_nameSpace}removeall:key:1"); - var res2 = _provider.Get($"{_nameSpace}removeall:key:2"); - - Assert.False(res1.HasValue); - Assert.False(res2.HasValue); - } - - [Fact] - protected virtual async Task RemoveAllAsync_Should_Succeed() - { - var dict = GetMultiDict($"{_nameSpace}removeallasync:"); - - _provider.SetAll(dict, _defaultTs); - - await _provider.RemoveAllAsync(new List { $"{_nameSpace}removeallasync:key:1", $"{_nameSpace}removeallasync:key:2" }); - - var res1 = _provider.Get($"{_nameSpace}removeallasync:key:1"); - var res2 = _provider.Get($"{_nameSpace}removeallasync:key:2"); - - Assert.False(res1.HasValue); - Assert.False(res2.HasValue); - } - #endregion - - #region Flush/FlushAsync - [Fact] - protected virtual void Flush_Should_Succeed() - { - for (var i = 0; i < 5; i++) - _provider.Set($"{_nameSpace}flush:{i}", $"value{i}", _defaultTs); - - for (var i = 0; i < 5; i++) - Assert.Equal($"value{i}", _provider.Get($"{_nameSpace}flush:{i}").Value); - - _provider.Flush(); - - for (var i = 0; i < 5; i++) - Assert.False(_provider.Get($"{_nameSpace}flush:{i}").HasValue); - } - - [Fact] - protected virtual async Task FlushAsync_Should_Succeed() - { - for (var i = 0; i < 5; i++) - _provider.Set($"{_nameSpace}flushasync:{i}", $"value{i}", _defaultTs); - - for (var i = 0; i < 5; i++) - Assert.Equal($"value{i}", _provider.Get($"{_nameSpace}flushasync:{i}").Value); - - await _provider.FlushAsync(); - - for (var i = 0; i < 5; i++) - Assert.False(_provider.Get($"{_nameSpace}flushasync:{i}").HasValue); - } - #endregion - - #region GetCount - [Fact] - protected virtual void Get_Count_Without_Prefix_Should_Succeed() - { - _provider.Flush(); - var rd = Guid.NewGuid().ToString(); - - for (var i = 0; i < 5; i++) - _provider.Set($"{rd}:getcount:{i}", $"value{i}", _defaultTs); - - Assert.Equal(5, _provider.GetCount()); - - _provider.Remove($"{rd}:getcount:4"); - - Assert.Equal(4, _provider.GetCount()); - } - - [Fact] - protected virtual void Get_Count_With_Prefix_Should_Succeed() - { - _provider.Flush(); - var rd = Guid.NewGuid().ToString(); - - for (var i = 0; i < 5; i++) - _provider.Set($"{rd}:getcount:withprefix:{i}", $"value{i}", _defaultTs); - - Assert.Equal(5, _provider.GetCount($"{rd}:getcount:withprefix:")); - - _provider.Remove($"{rd}:getcount:withprefix:1"); - - Assert.Equal(4, _provider.GetCount($"{rd}:getcount:withprefix:")); - } - - [Fact] - protected virtual async Task Get_Count_Async_Without_Prefix_Should_Succeed() - { - _provider.Flush(); - var rd = Guid.NewGuid().ToString(); - - for (var i = 0; i < 5; i++) - _provider.Set($"{rd}:getcountaync:{i}", $"value{i}", _defaultTs); - - Assert.Equal(5, await _provider.GetCountAsync()); - - _provider.Remove($"{rd}:getcountaync:4"); - - Assert.Equal(4, await _provider.GetCountAsync()); - } - - [Fact] - protected virtual async Task Get_Count_Async_With_Prefix_Should_Succeed() - { - _provider.Flush(); - var rd = Guid.NewGuid().ToString(); - - for (var i = 0; i < 5; i++) - _provider.Set($"{rd}:getcountaync:withprefix:{i}", $"value{i}", _defaultTs); - - Assert.Equal(5, await _provider.GetCountAsync($"{rd}:getcountaync:withprefix:")); - - _provider.Remove($"{rd}:getcountaync:withprefix:1"); - - Assert.Equal(4, await _provider.GetCountAsync($"{rd}:getcountaync:withprefix:")); - } - #endregion - - #region TrySet - [Fact] - protected virtual void TrySet_Value_And_Get_Cached_Value_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue1 = "value1"; - var cacheValue2 = "value2"; - - var first = _provider.TrySet(cacheKey, cacheValue1, _defaultTs); - var second = _provider.TrySet(cacheKey, cacheValue2, _defaultTs); - - Assert.True(first); - Assert.False(second); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue1, val.Value); - } - - [Fact] - public void TrySet_Value_And_Get_Cached_Value_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sg-{Guid.NewGuid().ToString()}"; - string cacheValue = null; - - _providerWithNullsCached.TrySet(cacheKey, cacheValue, _defaultTs); - - var val = _providerWithNullsCached.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - _providerWithNullsCached.Remove(cacheKey); - } - - [Fact] - protected virtual async Task TrySet_Value_And_Get_Cached_Value_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue1 = "value1"; - var cacheValue2 = "value2"; - - var first = await _provider.TrySetAsync(cacheKey, cacheValue1, _defaultTs); - var second = await _provider.TrySetAsync(cacheKey, cacheValue2, _defaultTs); - - Assert.True(first); - Assert.False(second); - - var val = _provider.Get(cacheKey); - Assert.True(val.HasValue); - Assert.Equal(cacheValue1, val.Value); - } - - [Fact] - public async Task TrySet_Value_And_Get_Cached_Value_Async_Should_Succeed_When_CacheValue_IsNull_And_Nulls_Are_Cached() - { - var cacheKey = $"{_nameSpace}-sg-{Guid.NewGuid().ToString()}"; - string cacheValue = null; - - await _providerWithNullsCached.TrySetAsync(cacheKey, cacheValue, _defaultTs); - - var val = await _providerWithNullsCached.GetAsync(cacheKey); - Assert.True(val.HasValue); - Assert.Null(val.Value); - - await _providerWithNullsCached.RemoveAsync(cacheKey); - } - #endregion - - #region GetExpiration - [Fact] - protected virtual void GetExpiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue1 = "value1"; - - _provider.Set(cacheKey, cacheValue1, _defaultTs); - - var ts = _provider.GetExpiration(cacheKey); - - Assert.InRange(ts, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30 + 120)); - } - - [Fact] - protected virtual async Task GetExpiration_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - var cacheValue1 = "value1"; - - await _provider.SetAsync(cacheKey, cacheValue1, _defaultTs); - - var ts = await _provider.GetExpirationAsync(cacheKey); - - Assert.InRange(ts, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30 + 120)); - } - #endregion - - #region common method - protected Dictionary GetMultiDict(string prefix = "") - { - return new Dictionary() - { - {string.Concat(prefix,"key:1"), "value1"}, - {string.Concat(prefix,"key:2"), "value2"} - }; - } - - protected void SetCacheItem(string cacheKey, string cacheValue) - { - _provider.Set(cacheKey, cacheValue, _defaultTs); - var val = _provider.Get(cacheKey); - Assert.Equal(cacheValue, val.Value); - } - - protected Func Create_Fake_Retriever_Return_String() - { - var func = A.Fake>(); - - A.CallTo(() => func.Invoke()).Returns("123"); - - return func; - } - - protected Func Create_Fake_Retriever_Return_NULL() - { - var func = A.Fake>(); - - A.CallTo(() => func.Invoke()).Returns(null); - - return func; - } - - protected Func> Create_Fake_Retriever_Return_String_Async() - { - var func = A.Fake>>(); - - A.CallTo(() => func.Invoke()).Returns(Task.FromResult("123")); - - return func; - } - - protected Func> Create_Fake_Retriever_Return_NULL_Async() - { - var func = A.Fake>>(); - string res = null; - A.CallTo(() => func.Invoke()).Returns(Task.FromResult(res)); - - return func; - } - #endregion - - [Fact] - protected virtual void OnHit_Should_Return_One_And_OnMiss_Should_Return_Zero() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - _provider.Set(cacheKey, "onhit", _defaultTs); - _provider.Get(cacheKey); - - var hitRes = _provider.CacheStats.GetStatistic(StatsType.Hit); - var missedRes = _provider.CacheStats.GetStatistic(StatsType.Missed); - - Assert.Equal(1, hitRes); - Assert.Equal(0, missedRes); - } - - [Fact] - protected virtual void OnHit_Should_Return_Zero_And_OnMiss_Should_Return_One() - { - var cacheKey = $"{_nameSpace}{Guid.NewGuid().ToString()}"; - _provider.Get(cacheKey); - - var hitRes = _provider.CacheStats.GetStatistic(StatsType.Hit); - var missedRes = _provider.CacheStats.GetStatistic(StatsType.Missed); - - Assert.Equal(0, hitRes); - Assert.Equal(1, missedRes); - } - } -} diff --git a/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs deleted file mode 100644 index 82bf69f4..00000000 --- a/test/EasyCaching.UnitTests/CachingTests/BaseRedisFeatureCachingProviderTest.cs +++ /dev/null @@ -1,1920 +0,0 @@ -namespace EasyCaching.UnitTests -{ - using EasyCaching.Core; - using System; - using System.Collections.Generic; - using System.Linq; - using System.Threading.Tasks; - using Xunit; - - - - public abstract class BaseRedisFeatureCachingProviderTest - { - protected IRedisCachingProvider _provider; - protected IEasyCachingProvider _baseProvider; - protected string _nameSpace = string.Empty; - - #region Keys - [Fact] - protected virtual void KeyDel_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = _provider.KeyDel(cacheKey); - - Assert.True(flag); - } - - [Fact] - protected virtual async Task KeyDelAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = await _provider.KeyDelAsync(cacheKey); - - Assert.True(flag); - } - - [Fact] - protected virtual void StringSet_And_KeyExpire_And_TTL_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.StringSet(cacheKey, "123"); - - Assert.True(res); - - var flag = _provider.KeyExpire(cacheKey, 10); - - Assert.True(flag); - - var ttl = _provider.TTL(cacheKey); - - Assert.InRange(ttl, 1, 10); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSet_And_KeyExpire_And_TTL_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.StringSetAsync(cacheKey, "123"); - - Assert.True(res); - - var flag = await _provider.KeyExpireAsync(cacheKey, 10); - - Assert.True(flag); - - var ttl = await _provider.TTLAsync(cacheKey); - - Assert.InRange(ttl, 1, 10); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual async Task StringSetWithExpiration_And_Persist_And_TTL_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.StringSetAsync(cacheKey, "123", TimeSpan.FromSeconds(10)); - - Assert.True(res); - - var initialTtl = await _provider.TTLAsync(cacheKey); - - Assert.InRange(initialTtl, 1, 10); - - var flag = await _provider.KeyPersistAsync(cacheKey); - - Assert.True(flag); - - var persistedTtl = await _provider.TTLAsync(cacheKey); - - Assert.Equal(persistedTtl, -1); - - await _provider.KeyDelAsync(cacheKey); - } - #endregion - - #region String - - [Fact] - protected virtual void StringSet_With_Nx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue, when: "nx"); - Assert.True(res); - - var res2 = _provider.StringSet(cacheKey, cacheValue, when: "nx"); - Assert.False(res2); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetAsync_With_Nx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); - Assert.True(res); - - var res2 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "nx"); - Assert.False(res2); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void StringSet_With_Xx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue, when: "xx"); - Assert.False(res); - - var res2 = _provider.StringSet(cacheKey, cacheValue); - Assert.True(res2); - var res3 = _provider.StringSet(cacheKey, cacheValue, when: "xx"); - Assert.True(res3); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetAsync_With_Xx_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); - Assert.False(res); - - var res2 = await _provider.StringSetAsync(cacheKey, cacheValue); - Assert.True(res2); - var res3 = await _provider.StringSetAsync(cacheKey, cacheValue, when: "xx"); - Assert.True(res3); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void StringSet_And_StringGet_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(res); - - var val = _provider.StringGet(cacheKey); - - Assert.Equal(cacheValue, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSet_And_StringGet_Async_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = Guid.NewGuid().ToString(); - - var res = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(res); - - var val = await _provider.StringGetAsync(cacheKey); - - Assert.Equal(cacheValue, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual void IncrBy_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.IncrBy(cacheKey); - - Assert.Equal(1, res); - - var val = _provider.IncrBy(cacheKey, 3); - - Assert.Equal(4, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task IncrByAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.IncrByAsync(cacheKey); - - Assert.Equal(1, res); - - var val = await _provider.IncrByAsync(cacheKey, 3); - - Assert.Equal(4, val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void IncrByFloat_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.IncrByFloat(cacheKey); - - Assert.Equal(1d, res); - - var val = _provider.IncrByFloat(cacheKey, 3d); - - Assert.Equal(4d, val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task IncrByFloatAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.IncrByFloatAsync(cacheKey); - - Assert.Equal(1d, res); - - var val = await _provider.IncrByFloatAsync(cacheKey, 3d); - - Assert.Equal(4d, val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "catcherwong"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var len = _provider.StringLen(cacheKey); - - Assert.Equal(11, len); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "catcherwong"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var len = await _provider.StringLenAsync(cacheKey); - - Assert.Equal(11, len); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringSetRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "Hello World"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var len = _provider.StringSetRange(cacheKey, 6, "Redis"); - - Assert.Equal(11, len); - - var val = _provider.StringGet(cacheKey); - - Assert.Equal("Hello Redis", val); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringSetRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "Hello World"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var len = await _provider.StringSetRangeAsync(cacheKey, 6, "Redis"); - - Assert.Equal(11, len); - - var val = await _provider.StringGetAsync(cacheKey); - - Assert.Equal("Hello Redis", val); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void StringGetRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "This is a string"; - - var flag = _provider.StringSet(cacheKey, cacheValue); - - Assert.True(flag); - - var res1 = _provider.StringGetRange(cacheKey, 0, 3); - - Assert.Equal("This", res1); - - var res2 = _provider.StringGetRange(cacheKey, -3, -1); - - Assert.Equal("ing", res2); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task StringGetRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - var cacheValue = "This is a string"; - - var flag = await _provider.StringSetAsync(cacheKey, cacheValue); - - Assert.True(flag); - - var res1 = await _provider.StringGetRangeAsync(cacheKey, 0, 3); - - Assert.Equal("This", res1); - - var res2 = await _provider.StringGetRangeAsync(cacheKey, -3, -1); - - Assert.Equal("ing", res2); - - await _provider.KeyDelAsync(cacheKey); - } - - - #endregion - - #region Hash - [Fact] - protected virtual void HMSet_And_HMGet_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var dict = _provider.HMGet(cacheKey, new List { "a1", "a2" }); - - Assert.Contains("v1", dict.Values); - Assert.Contains("v2", dict.Values); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HMSetAsync_And_HMGetAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var dict = await _provider.HMGetAsync(cacheKey, new List { "a1", "a2" }); - - Assert.Contains("v1", dict.Values); - Assert.Contains("v2", dict.Values); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HKeys_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var keys = _provider.HKeys(cacheKey); - - Assert.Contains("k1", keys); - Assert.Contains("k2", keys); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HKeysAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var keys = await _provider.HKeysAsync(cacheKey); - - Assert.Contains("k1", keys); - Assert.Contains("k2", keys); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HVals_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var vals = _provider.HVals(cacheKey); - - Assert.Contains("v1", vals); - Assert.Contains("v2", vals); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HValsAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"k1","v1"},{"k2","v2"} - }); - - var vals = await _provider.HValsAsync(cacheKey); - - Assert.Contains("v1", vals); - Assert.Contains("v2", vals); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGetAll_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - var all = _provider.HGetAll(cacheKey); - - Assert.Equal(2, all.Count); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAllAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-g-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - var all = await _provider.HGetAllAsync(cacheKey); - - Assert.Equal(2, all.Count); - - await _provider.HDelAsync(cacheKey); - } - - - [Fact] - protected virtual void HDel_With_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = _provider.HDel(cacheKey, new List { "a1", "a3" }); - - Assert.Equal(2, del); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(1, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HDelAsync_With_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = await _provider.HDelAsync(cacheKey, new List { "a1", "a3" }); - - Assert.Equal(2, del); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(1, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HDel_WithOut_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = _provider.HDel(cacheKey); - - Assert.Equal(1, del); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(0, len); - } - - [Fact] - protected virtual async Task HDelAsync_WithOut_Fields_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-d-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"},{"a3","v3"} - }); - - var del = await _provider.HDelAsync(cacheKey); - - Assert.Equal(1, del); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(0, len); - } - - [Fact] - protected virtual void HIncrBy_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = _provider.HIncrBy(cacheKey, "a1"); - - Assert.Equal(1, res); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HIncrByAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HIncrByAsync(cacheKey, "a1"); - - Assert.Equal(1, res); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HIncrBy_With_Val_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = _provider.HIncrBy(cacheKey, "a1", 3); - - Assert.Equal(3, res); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HIncrByAsync_With_Val_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-i-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HIncrByAsync(cacheKey, "a1", 3); - - Assert.Equal(3, res); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HExists_With_Other_CacheKey_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var flag = _provider.HExists(cacheKey, "field"); - - Assert.False(flag); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Other_CacheKey_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var flag = await _provider.HExistsAsync(cacheKey, "field"); - - Assert.False(flag); - } - - - [Fact] - protected virtual void HExists_With_Other_Field_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = _provider.HSet(cacheKey, "a1", "v1"); - - Assert.True(res); - - var flag = _provider.HExists(cacheKey, "field"); - - Assert.False(flag); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Other_Field_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); - - Assert.True(res); - - var flag = await _provider.HExistsAsync(cacheKey, "field"); - - Assert.False(flag); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HExists_With_Field_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = _provider.HSet(cacheKey, "a1", "v1"); - Assert.True(res); - - var flag = _provider.HExists(cacheKey, "a1"); - Assert.True(flag); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HExistsAsync_With_Field_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-e-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HSetAsync(cacheKey, "a1", "v1"); - Assert.True(res); - - var flag = await _provider.HExistsAsync(cacheKey, "a1"); - Assert.True(flag); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HSet_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var flag = _provider.HSet(cacheKey, "a3", "v3"); - - Assert.True(flag); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(3, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HSetAsync_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var flag = await _provider.HSetAsync(cacheKey, "a3", "v3"); - - Assert.True(flag); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(3, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGet_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = _provider.HGet(cacheKey, "a1"); - - Assert.NotNull(val); - Assert.Equal("v1", val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAsync_With_Field_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = await _provider.HGetAsync(cacheKey, "a1"); - - Assert.NotNull(val); - Assert.Equal("v1", val); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HGet_With_Other_Field_Should_Return_Null() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = _provider.HGet(cacheKey, "a3"); - - Assert.Null(val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HGetAsync_With_Other_Field_Should_Return_Null() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var val = await _provider.HGetAsync(cacheKey, "a3"); - - Assert.Null(val); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var len = _provider.HLen(cacheKey); - - Assert.Equal(2, len); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }); - - Assert.True(res); - - var len = await _provider.HLenAsync(cacheKey); - - Assert.Equal(2, len); - - await _provider.HDelAsync(cacheKey); - } - - [Fact] - protected virtual void HMSet_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.HMSet(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }, TimeSpan.FromSeconds(1)); - - Assert.True(res); - - System.Threading.Thread.Sleep(1050); - - var val = _provider.HGet(cacheKey, "a1"); - Assert.Null(val); - - _provider.HDel(cacheKey); - } - - [Fact] - protected virtual async Task HMSetAsync_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.HMSetAsync(cacheKey, new Dictionary - { - {"a1","v1"},{"a2","v2"} - }, TimeSpan.FromSeconds(1)); - - Assert.True(res); - - await Task.Delay(1050); - - var val = await _provider.HGetAsync(cacheKey, "a1"); - Assert.Null(val); - - await _provider.HDelAsync(cacheKey); - } - #endregion - - #region List - [Fact] - protected virtual void LPush_And_LPop_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LPop(cacheKey); - Assert.Equal("p2", val); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LPushAsync_And_LPopAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LPopAsync(cacheKey); - Assert.Equal("p2", val); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LPushX_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LPushX(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = _provider.LPop(cacheKey); - Assert.Equal("p4", pop); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task LPushXAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LPushXAsync(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = await _provider.LPopAsync(cacheKey); - Assert.Equal("p4", pop); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LPushX_Not_Exist_CacheKey_Should_Return_Zero() - { - var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; - - var val = _provider.LPushX(cacheKey, "p4"); - - Assert.Equal(0, val); - } - - [Fact] - protected virtual async Task LPushXAsync_Not_Exist_CacheKey_Should_Return_Zero() - { - var cacheKey = $"{_nameSpace}-nex-{Guid.NewGuid().ToString()}"; - - var val = await _provider.LPushXAsync(cacheKey, "p4"); - - Assert.Equal(0, val); - } - - [Fact] - protected virtual void LSet_And_LRange_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1" }); - Assert.Equal(1, res); - - var val = _provider.LSet(cacheKey, 0, "p3"); - Assert.True(val); - - var list = _provider.LRange(cacheKey, 0, -1); - - Assert.Single(list); - Assert.Equal("p3", list[0]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LSetAsync_And_LRangeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1" }); - Assert.Equal(1, res); - - var val = await _provider.LSetAsync(cacheKey, 0, "p3"); - Assert.True(val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - - Assert.Single(list); - Assert.Equal("p3", list[0]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LIndex_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - - Assert.Equal(2, res); - - var val0 = _provider.LIndex(cacheKey, 0); - var val1 = _provider.LIndex(cacheKey, 1); - - Assert.Equal("p2", val0); - Assert.Equal("p1", val1); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LIndexAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - - Assert.Equal(2, res); - - var val0 = await _provider.LIndexAsync(cacheKey, 0); - var val1 = await _provider.LIndexAsync(cacheKey, 1); - - Assert.Equal("p2", val0); - Assert.Equal("p1", val1); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LRem_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); - Assert.Equal(5, res); - - _provider.LRem(cacheKey, 0, "p1"); - - var len1 = _provider.LLen(cacheKey); - Assert.Equal(2, len1); - - _provider.LRem(cacheKey, 1, "p2"); - - var len2 = _provider.LLen(cacheKey); - Assert.Equal(1, len2); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task LRemAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p1", "p2", "p1" }); - Assert.Equal(5, res); - - await _provider.LRemAsync(cacheKey, 0, "p1"); - - var len1 = await _provider.LLenAsync(cacheKey); - Assert.Equal(2, len1); - - await _provider.LRemAsync(cacheKey, 1, "p2"); - - var len2 = await _provider.LLenAsync(cacheKey); - Assert.Equal(1, len2); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LTrim_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2", "p3" }); - Assert.Equal(3, res); - - var flag = _provider.LTrim(cacheKey, 2, -1); - Assert.True(flag); - - var vals = _provider.LRange(cacheKey, 0, -1); - Assert.Single(vals); - Assert.Equal("p1", vals[0]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LTrimAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2", "p3" }); - Assert.Equal(3, res); - - var flag = await _provider.LTrimAsync(cacheKey, 2, -1); - Assert.True(flag); - - var vals = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Single(vals); - Assert.Equal("p1", vals[0]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LLen_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var len = _provider.LLen(cacheKey); - Assert.Equal(2, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LLenAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var len = await _provider.LLenAsync(cacheKey); - Assert.Equal(2, len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void RPush_And_RPop_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.RPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.RPop(cacheKey); - Assert.Equal("p2", val); - - _baseProvider.Remove(cacheKey); - } - - - [Fact] - protected virtual async Task RPushAsync_And_RPopAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.RPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.RPopAsync(cacheKey); - Assert.Equal("p2", val); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void RPushX_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.RPushX(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = _provider.RPop(cacheKey); - Assert.Equal("p4", pop); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task RPushXAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.RPushXAsync(cacheKey, "p4"); - Assert.Equal(3, val); - - var pop = await _provider.RPopAsync(cacheKey); - Assert.Equal("p4", pop); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LInsertBefore_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LInsertBefore(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = _provider.LRange(cacheKey, 0, -1); - Assert.Equal("p4", list[1]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LInsertBeforeAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LInsertBeforeAsync(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Equal("p4", list[1]); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual void LInsertAfter_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.LPush(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = _provider.LInsertAfter(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = _provider.LRange(cacheKey, 0, -1); - Assert.Equal("p4", list[2]); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task LInsertAfterAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.LPushAsync(cacheKey, new List { "p1", "p2" }); - Assert.Equal(2, res); - - var val = await _provider.LInsertAfterAsync(cacheKey, "p1", "p4"); - Assert.Equal(3, val); - - var list = await _provider.LRangeAsync(cacheKey, 0, -1); - Assert.Equal("p4", list[2]); - - await _baseProvider.RemoveAsync(cacheKey); - } - #endregion - - #region Set - [Fact] - protected virtual void SAdd_And_SCard_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var len = _provider.SCard(cacheKey); - - Assert.Equal(2, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SAdd_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); - - Assert.Equal(2, res); - - System.Threading.Thread.Sleep(1050); - - var flag = _baseProvider.Exists(cacheKey); - - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SIsMember_With_Existed_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = _provider.SIsMember(cacheKey, "s1"); - var i2 = _provider.SIsMember(cacheKey, "s2"); - - Assert.True(i1); - Assert.True(i2); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SIsMember_With_Not_Existed_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = _provider.SIsMember(cacheKey, "s3"); - - Assert.False(i1); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SMembers_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var vals = _provider.SMembers(cacheKey); - - Assert.Equal(2, vals.Count); - Assert.Contains("s1", vals); - Assert.Contains("s2", vals); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRem_With_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var len = _provider.SRem(cacheKey, new List { "s1" }); - - Assert.Equal(1, len); - - var flag = _provider.SIsMember(cacheKey, "s1"); - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRem_Without_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var len = _provider.SRem(cacheKey); - - Assert.Equal(1, len); - - var flag = _baseProvider.Exists(cacheKey); - Assert.False(flag); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRandMember_With_Not_Exist_Should_Return_EmptyList() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var len = _provider.SRandMember(cacheKey); - - Assert.Empty(len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual void SRandMember_With_Exist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var res = _provider.SAdd(cacheKey, new List { "s1", "s2" }); - - var vals = _provider.SRandMember(cacheKey, 2); - - Assert.Equal(2, vals.Count); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task SAddAsync_And_SCardAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var len = await _provider.SCardAsync(cacheKey); - - Assert.Equal(2, len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SAddAsync_With_Expiration_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }, TimeSpan.FromSeconds(1)); - - Assert.Equal(2, res); - - await Task.Delay(1050); - - var len = await _provider.SCardAsync(cacheKey); - - Assert.Equal(0, len); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async Task SIsMemberAsync_With_Existed_Should_Return_True() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = await _provider.SIsMemberAsync(cacheKey, "s1"); - var i2 = await _provider.SIsMemberAsync(cacheKey, "s2"); - - Assert.True(i1); - Assert.True(i2); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SIsMemberAsync_With_Not_Existed_Should_Return_False() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - Assert.Equal(2, res); - - var i1 = await _provider.SIsMemberAsync(cacheKey, "s3"); - - Assert.False(i1); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SMembersAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var vals = await _provider.SMembersAsync(cacheKey); - - Assert.Equal(2, vals.Count); - Assert.Contains("s1", vals); - Assert.Contains("s2", vals); - - await _baseProvider.RemoveAsync(cacheKey); - } - - - [Fact] - protected virtual async Task SRemAsync_With_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var len = await _provider.SRemAsync(cacheKey, new List { "s1" }); - - Assert.Equal(1, len); - - var flag = await _provider.SIsMemberAsync(cacheKey, "s1"); - Assert.False(flag); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRemAsync_Without_Values_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var len = await _provider.SRemAsync(cacheKey); - - Assert.Equal(1, len); - - var flag = await _baseProvider.ExistsAsync(cacheKey); - Assert.False(flag); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRandMemberAsync_With_Not_Exist_Should_Return_EmptyList() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var len = await _provider.SRandMemberAsync(cacheKey); - - Assert.Empty(len); - - await _baseProvider.RemoveAsync(cacheKey); - } - - [Fact] - protected virtual async Task SRandMemberAsync_With_Exist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-srang-{Guid.NewGuid().ToString()}"; - - var res = await _provider.SAddAsync(cacheKey, new List { "s1", "s2" }); - - var vals = await _provider.SRandMemberAsync(cacheKey, 2); - - Assert.Equal(2, vals.Count); - - await _baseProvider.RemoveAsync(cacheKey); - } - #endregion - - #region Sorted Set - [Fact] - protected virtual void ZRangeByScore_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var dict = new Dictionary() - { - { "one", 1}, - { "two", 2}, - { "three", 3}, - }; - - var res = _provider.ZAdd(cacheKey, dict); - - Assert.Equal(3, res); - - var items = _provider.ZRangeByScore(cacheKey, 1, 2, 10, 0); - - Assert.Equal(2, items.Count); - Assert.Contains("one", items); - Assert.Contains("two", items); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async void ZRangeByScoreAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var dict = new Dictionary() - { - { "one", 1}, - { "two", 2}, - { "three", 3}, - }; - - var res = await _provider.ZAddAsync(cacheKey, dict); - - Assert.Equal(3, res); - - var items = await _provider.ZRangeByScoreAsync(cacheKey, 1, 2); - - Assert.Equal(2, items.Count); - Assert.Contains("one", items); - Assert.Contains("two", items); - - _baseProvider.Remove(cacheKey); - } - - [Fact] - protected virtual async void ZRangeRemByScoreAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-{Guid.NewGuid().ToString()}"; - - var dict = new Dictionary() - { - { "one", 1}, - { "two", 2}, - { "three", 3}, - { "four", 4}, - }; - - var res = await _provider.ZAddAsync(cacheKey, dict); - - var deletedCount = await _provider.ZRangeRemByScoreAsync(cacheKey, 1, 2); - - var items = await _provider.ZRangeByScoreAsync(cacheKey, 1, 4); - - Assert.Equal(2, deletedCount); - Assert.DoesNotContain("one", items); - Assert.DoesNotContain("two", items); - Assert.Contains("three", items); - Assert.Contains("four", items); - - _baseProvider.Remove(cacheKey); - } - - #endregion - - #region Hyperloglog - [Fact] - protected virtual void PfAdd_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-pfadd-{Guid.NewGuid().ToString()}"; - - var res1 = _provider.PfAdd(cacheKey, new List { "foo", "bar", "zap" }); - var res2 = _provider.PfAdd(cacheKey, new List { "zap", "zap", "zap" }); - Assert.True(res1); - Assert.False(res2); - - var count = _provider.PfCount(new List { cacheKey }); - - Assert.Equal(3, count); - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task PfAddAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-pfaddasync-{Guid.NewGuid().ToString()}"; - - var res1 = await _provider.PfAddAsync(cacheKey, new List { "foo", "bar", "zap" }); - var res2 = await _provider.PfAddAsync(cacheKey, new List { "zap", "zap", "zap" }); - Assert.True(res1); - Assert.False(res2); - - var count = await _provider.PfCountAsync(new List { cacheKey }); - - Assert.Equal(3, count); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void PfMerge_Should_Succeed() - { - var cacheKey0 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - var cacheKey1 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - var cacheKey2 = $"{_nameSpace}-pfmerge-{Guid.NewGuid().ToString()}"; - - var res1 = _provider.PfAdd(cacheKey1, new List { "foo", "bar", "zap", "a" }); - var res2 = _provider.PfAdd(cacheKey2, new List { "a", "b", "c", "foo" }); - Assert.True(res1); - Assert.True(res2); - - - var flag = _provider.PfMerge(cacheKey0, new List { cacheKey1, cacheKey2 }); - Assert.True(flag); - - var count = _provider.PfCount(new List { cacheKey0 }); - - Assert.Equal(6, count); - - _provider.KeyDel(cacheKey0); - _provider.KeyDel(cacheKey1); - _provider.KeyDel(cacheKey2); - } - - [Fact] - protected virtual async Task PfMergeAsync_Should_Succeed() - { - var cacheKey0 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - var cacheKey1 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - var cacheKey2 = $"{_nameSpace}-pfmergeasync-{Guid.NewGuid().ToString()}"; - - var res1 = await _provider.PfAddAsync(cacheKey1, new List { "foo", "bar", "zap", "a" }); - var res2 = await _provider.PfAddAsync(cacheKey2, new List { "a", "b", "c", "foo" }); - Assert.True(res1); - Assert.True(res2); - - - var flag = await _provider.PfMergeAsync(cacheKey0, new List { cacheKey1, cacheKey2 }); - Assert.True(flag); - - var count = await _provider.PfCountAsync(new List { cacheKey0 }); - - Assert.Equal(6, count); - - await _provider.KeyDelAsync(cacheKey0); - await _provider.KeyDelAsync(cacheKey1); - await _provider.KeyDelAsync(cacheKey2); - } - #endregion - - #region Geo - [Fact] - protected virtual void GeoAdd_And_GeoDist_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geoadd-{Guid.NewGuid().ToString()}"; - - var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var dist = _provider.GeoDist(cacheKey, "Palermo", "Catania"); - - // precision? - Assert.Equal(166274.1516, dist); - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task GeoAddAsync_And_GeoDistAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geoaddasync-{Guid.NewGuid().ToString()}"; - - var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var dist = await _provider.GeoDistAsync(cacheKey, "Palermo", "Catania"); - - // precision? - Assert.Equal(166274.1516, dist); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void GeoAdd_And_GeoHash_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; - - var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var hash = _provider.GeoHash(cacheKey, new List { "Palermo", "Catania" }); - - Assert.Equal(2, hash.Count); - Assert.Contains("sqc8b49rny0", hash); - Assert.Contains("sqdtr74hyu0", hash); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task GeoAddAsync_And_GeoHashAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohashasync-{Guid.NewGuid().ToString()}"; - - var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var hash = await _provider.GeoHashAsync(cacheKey, new List { "Palermo", "Catania" }); - - Assert.Equal(2, hash.Count); - Assert.Contains("sqc8b49rny0", hash); - Assert.Contains("sqdtr74hyu0", hash); - - await _provider.KeyDelAsync(cacheKey); - } - - [Fact] - protected virtual void GeoAdd_And_GeoPos_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohash-{Guid.NewGuid().ToString()}"; - - var res = _provider.GeoAdd(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var pos = _provider.GeoPos(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); - - Assert.Equal(3, pos.Count); - Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); - Assert.Contains(15.08726745843887329m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); - Assert.Contains(null, pos); - - _provider.KeyDel(cacheKey); - } - - [Fact] - protected virtual async Task GeoAddAsync_And_GeoPosAsync_Should_Succeed() - { - var cacheKey = $"{_nameSpace}-geohashasync-{Guid.NewGuid().ToString()}"; - - var res = await _provider.GeoAddAsync(cacheKey, new List<(double longitude, double latitude, string member)> { (13.361389, 38.115556, "Palermo"), (15.087269, 37.502669, "Catania") }); - - Assert.Equal(2, res); - - var pos = await _provider.GeoPosAsync(cacheKey, new List { "Palermo", "Catania", "NonExisting" }); - - Assert.Equal(3, pos.Count); - Assert.Contains(13.36138933897018433m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); - Assert.Contains(15.08726745843887329m, pos.Where(x => x.HasValue).Select(x => x.Value.longitude)); - Assert.Contains(null, pos); - - await _provider.KeyDelAsync(cacheKey); - } - #endregion - } -} diff --git a/test/EasyCaching.UnitTests/CachingTests/FasterKvCachingProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/FasterKvCachingProviderTest.cs index dd5d22fa..d16ecf8c 100644 --- a/test/EasyCaching.UnitTests/CachingTests/FasterKvCachingProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/FasterKvCachingProviderTest.cs @@ -76,13 +76,13 @@ protected override void Get_Parallel_Should_Succeed() { } - protected override Task GetAllByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace( + public override Task GetAllByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace( string preifx) { return Task.CompletedTask; } - protected override void GetAllByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace( + public override void GetAllByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace( string prefix) { } @@ -132,16 +132,16 @@ protected override void GetExpiration_Should_Succeed() { } - protected override void RemoveByPattern_Should_Succeed() + public override void RemoveByPattern_Should_Succeed() { } - protected override Task RemoveByPatternAsync_Should_Succeed() + public override Task RemoveByPatternAsync_Should_Succeed() { return Task.CompletedTask; } - protected override Task RemoveByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace( + public override Task RemoveByPrefix_Async_Should_Throw_ArgumentNullException_When_Prefix_IsNullOrWhiteSpace( string preifx) { @@ -152,7 +152,7 @@ protected override void RemoveByPrefix_Should_Succeed() { } - protected override void RemoveByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace( + public override void RemoveByPrefix_Should_Throw_ArgumentNullException_When_CacheKey_IsNullOrWhiteSpace( string prefix) { } diff --git a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs index d80d5a22..0d5cf30a 100644 --- a/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs +++ b/test/EasyCaching.UnitTests/CachingTests/MemcachedProviderTest.cs @@ -116,12 +116,12 @@ protected override void GetByPrefix_Should_Succeed() } [Fact] - protected override void RemoveByPattern_Should_Succeed() + public override void RemoveByPattern_Should_Succeed() { } [Fact] - protected override async Task RemoveByPatternAsync_Should_Succeed() + public override async Task RemoveByPatternAsync_Should_Succeed() { await Task.FromResult(1); } diff --git a/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs index 71f0da31..36347c60 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/CSRedisLockTest.cs @@ -7,7 +7,7 @@ namespace EasyCaching.UnitTests.DistributedLock { - public class CSRedisLockTest : DistributedLockTest + public class CSRedisLockTest : BaseDistributedLockTest { private static readonly IDistributedLockFactory Factory = new ServiceCollection() .AddLogging() diff --git a/test/EasyCaching.UnitTests/DistributedLock/DistributedLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/DistributedLockTest.cs deleted file mode 100644 index f4dbc5b9..00000000 --- a/test/EasyCaching.UnitTests/DistributedLock/DistributedLockTest.cs +++ /dev/null @@ -1,191 +0,0 @@ -using EasyCaching.Core.DistributedLock; -using System; -using System.Threading; -using System.Threading.Tasks; -using Xunit; -using Xunit.Abstractions; - -namespace EasyCaching.UnitTests.DistributedLock -{ - public abstract class DistributedLockTest - { - private readonly string _name; - private readonly IDistributedLockFactory _lockFactory; - private readonly ITestOutputHelper _output; - - protected DistributedLockTest(string name, IDistributedLockFactory lockFactory, ITestOutputHelper output) - { - _name = name; - _lockFactory = lockFactory; - - _output = output; - } - - [Fact] - public void Lock_Release() - { - var @lock = _lockFactory.CreateLock(_name, Guid.NewGuid().ToString()); - try - { - Assert.True(@lock.Lock(60000)); - } - finally - { - @lock.Release(); - } - } - - [Fact] - public async Task LockAsync_ReleaseAsync() - { - var @lock = _lockFactory.CreateLock(_name, Guid.NewGuid().ToString()); - try - { - Assert.True(await @lock.LockAsync(60000)); - } - finally - { - await @lock.ReleaseAsync(); - } - } - - [Fact] - public Task Lock_After_lock() - { - var key = Guid.NewGuid().ToString(); - const int timeout = 1000; - - var handle = new AutoResetEvent(false); - - return Task.WhenAll(Task.Run(() => Lock_After_lock1(key, timeout, handle)), Task.Run(() => Lock_After_lock2(key, timeout, handle))); - } - - private async Task Lock_After_lock1(string key, int timeout, EventWaitHandle handle) - { - var @lock = _lockFactory.CreateLock(_name, key); - try - { - Assert.True(await @lock.LockAsync(timeout)); - - handle.Set(); - - await Task.Delay(100); - - handle.WaitOne(10000); - } - finally - { - await @lock.ReleaseAsync(); - } - } - - private async Task Lock_After_lock2(string key, int timeout, EventWaitHandle handle) - { - var @lock = _lockFactory.CreateLock(_name, key); - try - { - handle.WaitOne(10000); - - Assert.False(await @lock.LockAsync(timeout)); - } - finally - { - handle.Set(); - } - } - - [Fact] - public async Task Repeat_Lock() - { - using (var lck = _lockFactory.CreateLock(_name, Guid.NewGuid().ToString())) - using (var lck2 = _lockFactory.CreateLock(_name, Guid.NewGuid().ToString())) - { - Assert.True(await lck.LockAsync(200)); - Assert.True(await lck2.LockAsync(200)); - - try - { - Assert.False(await lck.LockAsync(200)); - - Assert.True(false, "Fail"); - } - catch (DistributedLockException ex) - { - Assert.Equal("锁释放前请不要重复锁", ex.Message); - } - - await lck.ReleaseAsync(); - - Assert.True(await lck.LockAsync(200)); - } - } - - [Fact] - public async Task Multi_Thread_Lock() - { - const int timeout = 200; - - var lck = _lockFactory.CreateLock(_name, Guid.NewGuid().ToString()); - var handle = new AutoResetEvent(false); - - _ = Task.Run(async () => - { - _output.WriteLine("Start Lock1"); - - await lck.LockAsync(timeout); - - handle.Set(); - - await Task.Delay(100); - - _output.WriteLine("Start release1"); - - await lck.ReleaseAsync(); - - handle.Set(); - }); - - try - { - handle.WaitOne(10000); - - _output.WriteLine("Start Lock2"); - - Assert.False(await lck.LockAsync(timeout)); - - Assert.True(false, "Fail"); - } - catch (DistributedLockException ex) - { - Assert.Equal("锁释放前请不要重复锁", ex.Message); - } - finally - { - handle.WaitOne(10000); - - _output.WriteLine("Start Lock3"); - - Assert.True(await lck.LockAsync(timeout)); - - _output.WriteLine("Start release3"); - - await lck.ReleaseAsync(); - } - } - - [Fact] - public async Task Lock_With_CancellationToken() - { - using (var lock1 = _lockFactory.CreateLock(_name, nameof(Lock_With_CancellationToken))) - using (var lock2 = _lockFactory.CreateLock(_name, nameof(Lock_With_CancellationToken))) - using (var cts = new CancellationTokenSource()) - { - cts.CancelAfter(1500); - - Assert.True(await lock1.LockAsync(100, cts.Token)); - - Assert.Throws(() => lock2.Lock(2000, cts.Token)); - } - } - } -} diff --git a/test/EasyCaching.UnitTests/DistributedLock/MemcachedLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/MemcachedLockTest.cs index 4bc9b589..0207dbad 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/MemcachedLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/MemcachedLockTest.cs @@ -8,7 +8,7 @@ namespace EasyCaching.UnitTests.DistributedLock { - public class MemcachedLockTest : DistributedLockTest + public class MemcachedLockTest : BaseDistributedLockTest { private static readonly IDistributedLockFactory Factory = new ServiceCollection() .AddLogging() diff --git a/test/EasyCaching.UnitTests/DistributedLock/MemoryLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/MemoryLockTest.cs index 03fad325..39671c9d 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/MemoryLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/MemoryLockTest.cs @@ -8,7 +8,7 @@ namespace EasyCaching.UnitTests.DistributedLock { - public class MemoryLockTest : DistributedLockTest + public class MemoryLockTest : BaseDistributedLockTest { public static IDistributedLockFactory Factory = new MemoryLockFactory(); diff --git a/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs b/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs index 8eecc21f..fedb6e38 100644 --- a/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs +++ b/test/EasyCaching.UnitTests/DistributedLock/RedisLockTest.cs @@ -8,7 +8,7 @@ namespace EasyCaching.UnitTests.DistributedLock { - public class RedisLockTest : DistributedLockTest + public class RedisLockTest : BaseDistributedLockTest { private static readonly IDistributedLockFactory Factory = new ServiceCollection() .AddLogging() diff --git a/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj b/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj index 4c5c5b3b..b1f6327f 100644 --- a/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj +++ b/test/EasyCaching.UnitTests/EasyCaching.UnitTests.csproj @@ -17,16 +17,17 @@ + - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/EasyCaching.UnitTests/Infrastructure/IExampleService.cs b/test/EasyCaching.UnitTests/Infrastructure/IExampleService.cs index 34bfee48..078c0978 100644 --- a/test/EasyCaching.UnitTests/Infrastructure/IExampleService.cs +++ b/test/EasyCaching.UnitTests/Infrastructure/IExampleService.cs @@ -1,5 +1,6 @@ namespace EasyCaching.UnitTests.Infrastructure { + using EasyCaching.BaseTest.Infrastructure; using EasyCaching.Core.Interceptor; using EasyCaching.UnitTests.CustomInterceptors; using System; @@ -143,13 +144,13 @@ public interface IAspectCoreExampleService //: IEasyCaching Task PutTestAsync(int num, string str = "123"); [EasyCachingAble(Expiration = 1)] - Task> AbleIEnumerableTest(); + Task> AbleIEnumerableTest(); [EasyCachingAble(Expiration = 1)] - Task> AbleIListTest(); + Task> AbleIListTest(); [EasyCachingAble(Expiration = 1)] - Task> AbleListTest(); + Task> AbleListTest(); [EasyCachingAble(Expiration = 1)] Task AbleTestWithNullValueAsync(); @@ -210,21 +211,21 @@ public async Task PutTestAsync(int num, string str) return await Task.FromResult($"PutTestAsync-{num}"); } - public Task> AbleIEnumerableTest() + public Task> AbleIEnumerableTest() { - IEnumerable list = new List { new Model { Prop = "prop" } }; + IEnumerable list = new List { new SerializerTestModel { Prop = "prop" } }; return Task.FromResult(list); } - public Task> AbleIListTest() + public Task> AbleIListTest() { - IList list = new List { new Model { Prop = "prop" } }; + IList list = new List { new SerializerTestModel { Prop = "prop" } }; return Task.FromResult(list); } - public Task> AbleListTest() + public Task> AbleListTest() { - var list = new List { new Model { Prop = "prop" } }; + var list = new List { new SerializerTestModel { Prop = "prop" } }; return Task.FromResult(list); } diff --git a/test/EasyCaching.UnitTests/Infrastructure/Product.cs b/test/EasyCaching.UnitTests/Infrastructure/Product.cs deleted file mode 100644 index 881b0b08..00000000 --- a/test/EasyCaching.UnitTests/Infrastructure/Product.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace EasyCaching.UnitTests -{ - using System; - - [Serializable] - public class Product - { - public int Id { get; set; } - - public string Name { get; set; } - } -} diff --git a/test/EasyCaching.UnitTests/SerializerTests/BaseSerializerTest.cs b/test/EasyCaching.UnitTests/SerializerTests/BaseSerializerTest.cs deleted file mode 100644 index af0a563a..00000000 --- a/test/EasyCaching.UnitTests/SerializerTests/BaseSerializerTest.cs +++ /dev/null @@ -1,86 +0,0 @@ -namespace EasyCaching.UnitTests -{ - using System; - using EasyCaching.Core.Serialization; - using MemoryPack; - using ProtoBuf; - using Xunit; - - public abstract class BaseSerializerTest - { - protected IEasyCachingSerializer _serializer; - - [Fact] - public void Serialize_Object_Should_Succeed() - { - var res = _serializer.Serialize(new Model { Prop = "abc" }); - - Assert.NotEmpty(res); - } - - [Fact] - public void Deserialize_Object_Should_Succeed() - { - var bytes = _serializer.Serialize(new Model { Prop = "abc" }); - - var res = _serializer.Deserialize(bytes); - - Assert.Equal("abc", res.Prop); - } - - [Fact] - public void SerializeObject_should_Succeed() - { - object obj = new Model { Prop = "abc" }; - - var bytes = _serializer.SerializeObject(obj); - - Assert.NotEmpty(bytes); - } - - [Fact] - protected virtual void DeserializeObject_should_Succeed() - { - object obj = new Model { Prop = "abc" }; - - var bytes = _serializer.SerializeObject(obj); - - var desObj = _serializer.DeserializeObject(bytes) as Model; - - Assert.Equal("abc", desObj.Prop); - } - - - [Theory] - [InlineData("N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g")] - [InlineData("123abc")] - public void Serialize_String_Should_Succeed(string str) - { - var res = _serializer.Serialize(str); - - Assert.NotEmpty(res); - } - - [Theory] - [InlineData("N2L7KXa084WvelONYjkJ_traBMCCvy_UKmpUUzlrQ0EA2yNp3Iz6eSUrRG0bhaR_viswd50vDuPkY5nG43d1gbm-olT2KRMxOsVE08RfeD9lvK9lMguNG9kpIkKGZEjIf8Jv2m9fFhf8bnNa-yQH3g")] - [InlineData("123abc")] - public void Deserialize_String_Should_Succeed(string str) - { - var bytes = _serializer.Serialize(str); - - var res = _serializer.Deserialize(bytes); - - Assert.Equal(str, res); - } - - } - - [Serializable] - [ProtoContract] - [MemoryPackable] - public partial class Model - { - [ProtoMember(1)] - public string Prop { get; set; } - } -} \ No newline at end of file diff --git a/test/EasyCaching.UnitTests/SerializerTests/JsonSerializerTest.cs b/test/EasyCaching.UnitTests/SerializerTests/JsonSerializerTest.cs index c03832be..f62951be 100644 --- a/test/EasyCaching.UnitTests/SerializerTests/JsonSerializerTest.cs +++ b/test/EasyCaching.UnitTests/SerializerTests/JsonSerializerTest.cs @@ -1,8 +1,6 @@ namespace EasyCaching.UnitTests { using EasyCaching.Serialization.Json; - using FakeItEasy; - using Microsoft.Extensions.Options; using Newtonsoft.Json; using System; using System.Collections.Generic; diff --git a/test/EasyCaching.UnitTests/SerializerTests/MemoryPackSerializerTest.cs b/test/EasyCaching.UnitTests/SerializerTests/MemoryPackSerializerTest.cs index c6d06350..32cbbfb9 100644 --- a/test/EasyCaching.UnitTests/SerializerTests/MemoryPackSerializerTest.cs +++ b/test/EasyCaching.UnitTests/SerializerTests/MemoryPackSerializerTest.cs @@ -14,7 +14,7 @@ public MemoryPackSerializerTest() } //This should be overrided becuse it is not supported by memory-pack - protected override void DeserializeObject_should_Succeed() + public override void DeserializeObject_should_Succeed() { Person input = new("test", "test1"); var serialized = _serializer.Serialize(input); diff --git a/test/EasyCaching.UnitTests/Using.cs b/test/EasyCaching.UnitTests/Using.cs new file mode 100644 index 00000000..4c4b2fd6 --- /dev/null +++ b/test/EasyCaching.UnitTests/Using.cs @@ -0,0 +1 @@ +global using EasyCaching.BaseTest; \ No newline at end of file