From 62eab65c10b2a5f05acccf22f5e603dca93b81ed Mon Sep 17 00:00:00 2001 From: reisenberger Date: Sat, 26 Jan 2019 11:53:34 +0000 Subject: [PATCH] Refactor to make tests re-useable for different CacheProvider types --- .../Integration/CacheRoundTripSpecsBase.cs | 7 +++++++ .../Integration/CacheRoundTripSpecsSyncBase.cs | 12 ++++++------ ...ndardIDistributedCacheProvider_Async_ByteArray.cs | 7 ++++++- ...StandardIDistributedCacheProvider_Async_String.cs | 6 +++++- ...andardIDistributedCacheProvider_Sync_ByteArray.cs | 6 +++++- ...tStandardIDistributedCacheProvider_Sync_String.cs | 6 +++++- .../Integration/CacheRoundTripspecsAsyncBase.cs | 12 ++++++------ .../Integration/ICachePolicyFactory.cs | 8 ++++++++ ...ory.cs => MemoryDistributedCachePolicyFactory.cs} | 6 +++--- .../Polly.Caching.Distributed.SharedSpecs.projitems | 3 ++- 10 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/Polly.Caching.Distributed.SharedSpecs/Integration/ICachePolicyFactory.cs rename src/Polly.Caching.Distributed.SharedSpecs/Integration/{CachePolicyFactory.cs => MemoryDistributedCachePolicyFactory.cs} (90%) diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsBase.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsBase.cs index 19d2397..f97dacb 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsBase.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsBase.cs @@ -6,6 +6,13 @@ namespace Polly.Caching.Distributed.Specs.Integration { public abstract class CacheRoundTripSpecsBase { + protected CacheRoundTripSpecsBase(ICachePolicyFactory cachePolicyFactory) + { + CachePolicyFactory = cachePolicyFactory; + } + + protected ICachePolicyFactory CachePolicyFactory { get; } + protected const string OperationKey = "SomeOperationKey"; public abstract Task Should_roundtrip_this_variant_of(TResult testValue); diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsSyncBase.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsSyncBase.cs index 85f3cdb..7c95cfd 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsSyncBase.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsSyncBase.cs @@ -1,6 +1,4 @@ -#if !NETCOREAPP1_1 - -using System; +using System; using System.Threading.Tasks; using FluentAssertions; @@ -8,6 +6,10 @@ namespace Polly.Caching.Distributed.Specs.Integration { public abstract class CacheRoundTripSpecsSyncBase : CacheRoundTripSpecsBase { + protected CacheRoundTripSpecsSyncBase(ICachePolicyFactory cachePolicyFactory) : base(cachePolicyFactory) + { + } + public override Task Should_roundtrip_this_variant_of(TResult testValue) { // Arrange @@ -47,6 +49,4 @@ public override Task Should_roundtrip_this_variant_of(TResult testValue return Task.CompletedTask; } } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs index 19b67b9..bcc0e32 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs @@ -2,7 +2,12 @@ namespace Polly.Caching.Distributed.Specs.Integration { - public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray : CacheRoundTripSpecsAsyncBase { } + public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray : CacheRoundTripSpecsAsyncBase { + + public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray() : base(new MemoryDistributedCachePolicyFactory()) + { + } + } } #endif \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs index 950ac1f..8fee536 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs @@ -2,7 +2,11 @@ namespace Polly.Caching.Distributed.Specs.Integration { - public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String : CacheRoundTripSpecsAsyncBase { } + public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String : CacheRoundTripSpecsAsyncBase { + public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String() : base(new MemoryDistributedCachePolicyFactory()) + { + } + } } #endif \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs index 7d5b7dc..e9d920f 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs @@ -2,7 +2,11 @@ namespace Polly.Caching.Distributed.Specs.Integration { - public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray : CacheRoundTripSpecsSyncBase { } + public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray : CacheRoundTripSpecsSyncBase { + public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray() : base(new MemoryDistributedCachePolicyFactory()) + { + } + } } #endif \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs index b97ad46..455ead1 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs @@ -2,7 +2,11 @@ namespace Polly.Caching.Distributed.Specs.Integration { - public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String : CacheRoundTripSpecsSyncBase { } + public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String : CacheRoundTripSpecsSyncBase { + public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String() : base(new MemoryDistributedCachePolicyFactory()) + { + } + } } #endif \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripspecsAsyncBase.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripspecsAsyncBase.cs index 4466511..52e4ec1 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripspecsAsyncBase.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripspecsAsyncBase.cs @@ -1,6 +1,4 @@ -#if !NETCOREAPP1_1 - -using System; +using System; using System.Threading; using System.Threading.Tasks; using FluentAssertions; @@ -9,6 +7,10 @@ namespace Polly.Caching.Distributed.Specs.Integration { public abstract class CacheRoundTripSpecsAsyncBase : CacheRoundTripSpecsBase { + protected CacheRoundTripSpecsAsyncBase(ICachePolicyFactory cachePolicyFactory) : base(cachePolicyFactory) + { + } + public override async Task Should_roundtrip_this_variant_of(TResult testValue) { // Arrange @@ -46,6 +48,4 @@ public override async Task Should_roundtrip_this_variant_of(TResult tes underlyingDelegateExecuteCount.Should().Be(1); } } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/ICachePolicyFactory.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/ICachePolicyFactory.cs new file mode 100644 index 0000000..e4126a9 --- /dev/null +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/ICachePolicyFactory.cs @@ -0,0 +1,8 @@ +namespace Polly.Caching.Distributed.Specs.Integration +{ + public interface ICachePolicyFactory + { + (ISyncCacheProvider, ISyncPolicy) CreateSyncCachePolicy(); + (IAsyncCacheProvider, IAsyncPolicy) CreateAsyncCachePolicy(); + } +} diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CachePolicyFactory.cs b/src/Polly.Caching.Distributed.SharedSpecs/Integration/MemoryDistributedCachePolicyFactory.cs similarity index 90% rename from src/Polly.Caching.Distributed.SharedSpecs/Integration/CachePolicyFactory.cs rename to src/Polly.Caching.Distributed.SharedSpecs/Integration/MemoryDistributedCachePolicyFactory.cs index 399427b..7aa4b4c 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Integration/CachePolicyFactory.cs +++ b/src/Polly.Caching.Distributed.SharedSpecs/Integration/MemoryDistributedCachePolicyFactory.cs @@ -9,9 +9,9 @@ namespace Polly.Caching.Distributed.Specs.Integration { - public class CachePolicyFactory + public class MemoryDistributedCachePolicyFactory : ICachePolicyFactory { - public static (ISyncCacheProvider, ISyncPolicy) CreateSyncCachePolicy() + public (ISyncCacheProvider, ISyncPolicy) CreateSyncCachePolicy() { var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); ISyncCacheProvider memoryIDistributedCacheProvider; @@ -32,7 +32,7 @@ public static (ISyncCacheProvider, ISyncPolicy) CreateSyncCach return (memoryIDistributedCacheProvider, policy); } - public static (IAsyncCacheProvider, IAsyncPolicy) CreateAsyncCachePolicy() + public (IAsyncCacheProvider, IAsyncPolicy) CreateAsyncCachePolicy() { var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())); IAsyncCacheProvider memoryIDistributedCacheProvider; diff --git a/src/Polly.Caching.Distributed.SharedSpecs/Polly.Caching.Distributed.SharedSpecs.projitems b/src/Polly.Caching.Distributed.SharedSpecs/Polly.Caching.Distributed.SharedSpecs.projitems index 5a74324..e54580f 100644 --- a/src/Polly.Caching.Distributed.SharedSpecs/Polly.Caching.Distributed.SharedSpecs.projitems +++ b/src/Polly.Caching.Distributed.SharedSpecs/Polly.Caching.Distributed.SharedSpecs.projitems @@ -10,7 +10,7 @@ - + @@ -18,6 +18,7 @@ +