Skip to content

Commit 62eab65

Browse files
committed
Refactor to make tests re-useable for different CacheProvider types
1 parent a853a22 commit 62eab65

10 files changed

Lines changed: 53 additions & 20 deletions

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ namespace Polly.Caching.Distributed.Specs.Integration
66
{
77
public abstract class CacheRoundTripSpecsBase
88
{
9+
protected CacheRoundTripSpecsBase(ICachePolicyFactory cachePolicyFactory)
10+
{
11+
CachePolicyFactory = cachePolicyFactory;
12+
}
13+
14+
protected ICachePolicyFactory CachePolicyFactory { get; }
15+
916
protected const string OperationKey = "SomeOperationKey";
1017

1118
public abstract Task Should_roundtrip_this_variant_of<TResult>(TResult testValue);

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecsSyncBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
#if !NETCOREAPP1_1
2-
3-
using System;
1+
using System;
42
using System.Threading.Tasks;
53
using FluentAssertions;
64

75
namespace Polly.Caching.Distributed.Specs.Integration
86
{
97
public abstract class CacheRoundTripSpecsSyncBase<TCache> : CacheRoundTripSpecsBase
108
{
9+
protected CacheRoundTripSpecsSyncBase(ICachePolicyFactory cachePolicyFactory) : base(cachePolicyFactory)
10+
{
11+
}
12+
1113
public override Task Should_roundtrip_this_variant_of<TResult>(TResult testValue)
1214
{
1315
// Arrange
@@ -47,6 +49,4 @@ public override Task Should_roundtrip_this_variant_of<TResult>(TResult testValue
4749
return Task.CompletedTask;
4850
}
4951
}
50-
}
51-
52-
#endif
52+
}

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace Polly.Caching.Distributed.Specs.Integration
44
{
5-
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray : CacheRoundTripSpecsAsyncBase<byte[]> { }
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray : CacheRoundTripSpecsAsyncBase<byte[]> {
6+
7+
public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray() : base(new MemoryDistributedCachePolicyFactory())
8+
{
9+
}
10+
}
611
}
712

813
#endif

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Polly.Caching.Distributed.Specs.Integration
44
{
5-
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String : CacheRoundTripSpecsAsyncBase<string> { }
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String : CacheRoundTripSpecsAsyncBase<string> {
6+
public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String() : base(new MemoryDistributedCachePolicyFactory())
7+
{
8+
}
9+
}
610
}
711

812
#endif

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Polly.Caching.Distributed.Specs.Integration
44
{
5-
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray : CacheRoundTripSpecsSyncBase<byte[]> { }
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray : CacheRoundTripSpecsSyncBase<byte[]> {
6+
public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray() : base(new MemoryDistributedCachePolicyFactory())
7+
{
8+
}
9+
}
610
}
711

812
#endif

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Polly.Caching.Distributed.Specs.Integration
44
{
5-
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String : CacheRoundTripSpecsSyncBase<string> { }
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String : CacheRoundTripSpecsSyncBase<string> {
6+
public CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String() : base(new MemoryDistributedCachePolicyFactory())
7+
{
8+
}
9+
}
610
}
711

812
#endif

src/Polly.Caching.Distributed.SharedSpecs/Integration/CacheRoundTripspecsAsyncBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if !NETCOREAPP1_1
2-
3-
using System;
1+
using System;
42
using System.Threading;
53
using System.Threading.Tasks;
64
using FluentAssertions;
@@ -9,6 +7,10 @@ namespace Polly.Caching.Distributed.Specs.Integration
97
{
108
public abstract class CacheRoundTripSpecsAsyncBase<TCache> : CacheRoundTripSpecsBase
119
{
10+
protected CacheRoundTripSpecsAsyncBase(ICachePolicyFactory cachePolicyFactory) : base(cachePolicyFactory)
11+
{
12+
}
13+
1214
public override async Task Should_roundtrip_this_variant_of<TResult>(TResult testValue)
1315
{
1416
// Arrange
@@ -46,6 +48,4 @@ public override async Task Should_roundtrip_this_variant_of<TResult>(TResult tes
4648
underlyingDelegateExecuteCount.Should().Be(1);
4749
}
4850
}
49-
}
50-
51-
#endif
51+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Polly.Caching.Distributed.Specs.Integration
2+
{
3+
public interface ICachePolicyFactory
4+
{
5+
(ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateSyncCachePolicy<TCache, TResult>();
6+
(IAsyncCacheProvider<TResult>, IAsyncPolicy<TResult>) CreateAsyncCachePolicy<TCache, TResult>();
7+
}
8+
}

src/Polly.Caching.Distributed.SharedSpecs/Integration/CachePolicyFactory.cs renamed to src/Polly.Caching.Distributed.SharedSpecs/Integration/MemoryDistributedCachePolicyFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Polly.Caching.Distributed.Specs.Integration
1111
{
12-
public class CachePolicyFactory
12+
public class MemoryDistributedCachePolicyFactory : ICachePolicyFactory
1313
{
14-
public static (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateSyncCachePolicy<TCache, TResult>()
14+
public (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateSyncCachePolicy<TCache, TResult>()
1515
{
1616
var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
1717
ISyncCacheProvider<TResult> memoryIDistributedCacheProvider;
@@ -32,7 +32,7 @@ public static (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateSyncCach
3232
return (memoryIDistributedCacheProvider, policy);
3333
}
3434

35-
public static (IAsyncCacheProvider<TResult>, IAsyncPolicy<TResult>) CreateAsyncCachePolicy<TCache, TResult>()
35+
public (IAsyncCacheProvider<TResult>, IAsyncPolicy<TResult>) CreateAsyncCachePolicy<TCache, TResult>()
3636
{
3737
var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
3838
IAsyncCacheProvider<TResult> memoryIDistributedCacheProvider;

src/Polly.Caching.Distributed.SharedSpecs/Polly.Caching.Distributed.SharedSpecs.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)Integration\ByteArraySerializer.cs" />
13-
<Compile Include="$(MSBuildThisFileDirectory)Integration\CachePolicyFactory.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)Integration\MemoryDistributedCachePolicyFactory.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsAsyncBase.cs" />
1515
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsBase.cs" />
1616
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsSyncBase.cs" />
1717
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs" />
1818
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs" />
1919
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs" />
2020
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs" />
21+
<Compile Include="$(MSBuildThisFileDirectory)Integration\ICachePolicyFactory.cs" />
2122
<Compile Include="$(MSBuildThisFileDirectory)Unit\CacheProviderHelperTests.cs" />
2223
<Compile Include="$(MSBuildThisFileDirectory)Unit\NetStandardIDistributedCacheByteArrayProviderSpecs.cs" />
2324
<Compile Include="$(MSBuildThisFileDirectory)Unit\NetStandardIDistributedCacheStringProviderSpecs.cs" />

0 commit comments

Comments
 (0)