Skip to content

Commit ddaa115

Browse files
committed
Refactor to abstract out sync specs versus common spec functionality
1 parent 18572d0 commit ddaa115

6 files changed

Lines changed: 112 additions & 98 deletions

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,50 @@ public abstract class CacheRoundTripSpecsBase
77
{
88
protected const string OperationKey = "SomeOperationKey";
99

10+
public abstract void Should_roundtrip_this_variant_of<TResult>(TResult testValue);
11+
12+
[Theory]
13+
[MemberData(nameof(SampleClassData))]
14+
public void Should_roundtrip_all_variants_of_reference_type(SampleClass testValue)
15+
{
16+
Should_roundtrip_this_variant_of<SampleClass>(testValue);
17+
}
18+
19+
[Theory]
20+
[MemberData(nameof(SampleStringData))]
21+
public void Should_roundtrip_all_variants_of_string(String testValue)
22+
{
23+
Should_roundtrip_this_variant_of<String>(testValue);
24+
}
25+
26+
[Theory]
27+
[MemberData(nameof(SampleNumericData))]
28+
public void Should_roundtrip_all_variants_of_numeric(int testValue)
29+
{
30+
Should_roundtrip_this_variant_of<int>(testValue);
31+
}
32+
33+
[Theory]
34+
[MemberData(nameof(SampleEnumData))]
35+
public void Should_roundtrip_all_variants_of_enum(SampleEnum testValue)
36+
{
37+
Should_roundtrip_this_variant_of<SampleEnum>(testValue);
38+
}
39+
40+
[Theory]
41+
[MemberData(nameof(SampleBoolData))]
42+
public void Should_roundtrip_all_variants_of_bool(bool testValue)
43+
{
44+
Should_roundtrip_this_variant_of<bool>(testValue);
45+
}
46+
47+
[Theory]
48+
[MemberData(nameof(SampleNullableBoolData))]
49+
public void Should_roundtrip_all_variants_of_nullable_bool(bool? testValue)
50+
{
51+
Should_roundtrip_this_variant_of<bool?>(testValue);
52+
}
53+
1054
public static TheoryData<SampleClass> SampleClassData =>
1155
new TheoryData<SampleClass>
1256
{
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#if !NETCOREAPP1_1
2+
3+
using System;
4+
using FluentAssertions;
5+
6+
namespace Polly.Caching.Distributed.Specs.Integration
7+
{
8+
public abstract class CacheRoundTripSpecsSyncBase<TCache> : CacheRoundTripSpecsBase
9+
{
10+
public override void Should_roundtrip_this_variant_of<TResult>(TResult testValue)
11+
{
12+
// Arrange
13+
var (cacheProvider, cache) = CachePolicyFactory.CreateCachePolicy<TCache, TResult>();
14+
15+
// Assert - should not be in cache
16+
(bool cacheHit1, TResult fromCache1) = cacheProvider.TryGet(OperationKey);
17+
cacheHit1.Should().BeFalse();
18+
fromCache1.Should().Be(default(TResult));
19+
20+
// Act - should execute underlying delegate and place in cache
21+
int underlyingDelegateExecuteCount = 0;
22+
cache.Execute(ctx =>
23+
{
24+
underlyingDelegateExecuteCount++;
25+
return testValue;
26+
}, new Context(OperationKey))
27+
.ShouldBeEquivalentTo(testValue);
28+
29+
// Assert - should have executed underlying delegate
30+
underlyingDelegateExecuteCount.Should().Be(1);
31+
32+
// Assert - should be in cache
33+
(bool cacheHit2, TResult fromCache2) = cacheProvider.TryGet(OperationKey);
34+
cacheHit2.Should().BeTrue();
35+
fromCache2.ShouldBeEquivalentTo(testValue);
36+
37+
// Act - should execute underlying delegate and place in cache
38+
cache.Execute(ctx =>
39+
{
40+
underlyingDelegateExecuteCount++;
41+
throw new Exception("Cache should be used so this should not get invoked.");
42+
}, new Context(OperationKey))
43+
.ShouldBeEquivalentTo(testValue);
44+
underlyingDelegateExecuteCount.Should().Be(1);
45+
}
46+
}
47+
}
48+
49+
#endif

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

Lines changed: 0 additions & 97 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#if !NETCOREAPP1_1
2+
3+
namespace Polly.Caching.Distributed.Specs.Integration
4+
{
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray : CacheRoundTripSpecsSyncBase<byte[]> { }
6+
}
7+
8+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#if !NETCOREAPP1_1
2+
3+
namespace Polly.Caching.Distributed.Specs.Integration
4+
{
5+
public class CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String : CacheRoundTripSpecsSyncBase<string> { }
6+
}
7+
8+
#endif

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
<Compile Include="$(MSBuildThisFileDirectory)Integration\ByteArraySerializer.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)Integration\CachePolicyFactory.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsBase.cs" />
15-
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsSyncBase.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs" />
1618
<Compile Include="$(MSBuildThisFileDirectory)Unit\CacheProviderHelperTests.cs" />
1719
<Compile Include="$(MSBuildThisFileDirectory)Unit\NetStandardIDistributedCacheByteArrayProviderSpecs.cs" />
1820
<Compile Include="$(MSBuildThisFileDirectory)Unit\NetStandardIDistributedCacheStringProviderSpecs.cs" />

0 commit comments

Comments
 (0)