Skip to content

Commit a853a22

Browse files
committed
Refactor to add async tests
1 parent ddaa115 commit a853a22

7 files changed

Lines changed: 111 additions & 16 deletions

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Polly.Caching.Distributed.Specs.Integration
1111
{
1212
public class CachePolicyFactory
1313
{
14-
public static (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateCachePolicy<TCache, TResult>()
14+
public static (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateSyncCachePolicy<TCache, TResult>()
1515
{
1616
var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
1717
ISyncCacheProvider<TResult> memoryIDistributedCacheProvider;
@@ -31,6 +31,27 @@ public static (ISyncCacheProvider<TResult>, ISyncPolicy<TResult>) CreateCachePol
3131
var policy = Policy.Cache<TResult>(memoryIDistributedCacheProvider, TimeSpan.FromHours(1));
3232
return (memoryIDistributedCacheProvider, policy);
3333
}
34+
35+
public static (IAsyncCacheProvider<TResult>, IAsyncPolicy<TResult>) CreateAsyncCachePolicy<TCache, TResult>()
36+
{
37+
var memoryIDistributedCache = new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions()));
38+
IAsyncCacheProvider<TResult> memoryIDistributedCacheProvider;
39+
if (typeof(TCache) == typeof(string))
40+
{
41+
memoryIDistributedCacheProvider = memoryIDistributedCache.AsAsyncCacheProvider<string>().WithSerializer<TResult, string>(new JsonSerializer<TResult>(new JsonSerializerSettings()));
42+
}
43+
else if (typeof(TCache) == typeof(byte[]))
44+
{
45+
memoryIDistributedCacheProvider = memoryIDistributedCache.AsAsyncCacheProvider<byte[]>().WithSerializer(new ByteArraySerializer()).WithSerializer<TResult, string>(new JsonSerializer<TResult>(new JsonSerializerSettings()));
46+
}
47+
else
48+
{
49+
throw new ArgumentException($"{nameof(TCache)} must be either {typeof(string).Name} or {typeof(byte).Name}[]", nameof(TCache));
50+
}
51+
52+
var policy = Policy.CacheAsync<TResult>(memoryIDistributedCacheProvider, TimeSpan.FromHours(1));
53+
return (memoryIDistributedCacheProvider, policy);
54+
}
3455
}
3556
}
3657

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Xunit;
34

45
namespace Polly.Caching.Distributed.Specs.Integration
@@ -7,48 +8,48 @@ public abstract class CacheRoundTripSpecsBase
78
{
89
protected const string OperationKey = "SomeOperationKey";
910

10-
public abstract void Should_roundtrip_this_variant_of<TResult>(TResult testValue);
11+
public abstract Task Should_roundtrip_this_variant_of<TResult>(TResult testValue);
1112

1213
[Theory]
1314
[MemberData(nameof(SampleClassData))]
14-
public void Should_roundtrip_all_variants_of_reference_type(SampleClass testValue)
15+
public async Task Should_roundtrip_all_variants_of_reference_type(SampleClass testValue)
1516
{
16-
Should_roundtrip_this_variant_of<SampleClass>(testValue);
17+
await Should_roundtrip_this_variant_of<SampleClass>(testValue);
1718
}
1819

1920
[Theory]
2021
[MemberData(nameof(SampleStringData))]
21-
public void Should_roundtrip_all_variants_of_string(String testValue)
22+
public async Task Should_roundtrip_all_variants_of_string(String testValue)
2223
{
23-
Should_roundtrip_this_variant_of<String>(testValue);
24+
await Should_roundtrip_this_variant_of<String>(testValue);
2425
}
2526

2627
[Theory]
2728
[MemberData(nameof(SampleNumericData))]
28-
public void Should_roundtrip_all_variants_of_numeric(int testValue)
29+
public async Task Should_roundtrip_all_variants_of_numeric(int testValue)
2930
{
30-
Should_roundtrip_this_variant_of<int>(testValue);
31+
await Should_roundtrip_this_variant_of<int>(testValue);
3132
}
3233

3334
[Theory]
3435
[MemberData(nameof(SampleEnumData))]
35-
public void Should_roundtrip_all_variants_of_enum(SampleEnum testValue)
36+
public async Task Should_roundtrip_all_variants_of_enum(SampleEnum testValue)
3637
{
37-
Should_roundtrip_this_variant_of<SampleEnum>(testValue);
38+
await Should_roundtrip_this_variant_of<SampleEnum>(testValue);
3839
}
3940

4041
[Theory]
4142
[MemberData(nameof(SampleBoolData))]
42-
public void Should_roundtrip_all_variants_of_bool(bool testValue)
43+
public async Task Should_roundtrip_all_variants_of_bool(bool testValue)
4344
{
44-
Should_roundtrip_this_variant_of<bool>(testValue);
45+
await Should_roundtrip_this_variant_of<bool>(testValue);
4546
}
4647

4748
[Theory]
4849
[MemberData(nameof(SampleNullableBoolData))]
49-
public void Should_roundtrip_all_variants_of_nullable_bool(bool? testValue)
50+
public async Task Should_roundtrip_all_variants_of_nullable_bool(bool? testValue)
5051
{
51-
Should_roundtrip_this_variant_of<bool?>(testValue);
52+
await Should_roundtrip_this_variant_of<bool?>(testValue);
5253
}
5354

5455
public static TheoryData<SampleClass> SampleClassData =>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#if !NETCOREAPP1_1
22

33
using System;
4+
using System.Threading.Tasks;
45
using FluentAssertions;
56

67
namespace Polly.Caching.Distributed.Specs.Integration
78
{
89
public abstract class CacheRoundTripSpecsSyncBase<TCache> : CacheRoundTripSpecsBase
910
{
10-
public override void Should_roundtrip_this_variant_of<TResult>(TResult testValue)
11+
public override Task Should_roundtrip_this_variant_of<TResult>(TResult testValue)
1112
{
1213
// Arrange
13-
var (cacheProvider, cache) = CachePolicyFactory.CreateCachePolicy<TCache, TResult>();
14+
var (cacheProvider, cache) = CachePolicyFactory.CreateSyncCachePolicy<TCache, TResult>();
1415

1516
// Assert - should not be in cache
1617
(bool cacheHit1, TResult fromCache1) = cacheProvider.TryGet(OperationKey);
@@ -42,6 +43,8 @@ public override void Should_roundtrip_this_variant_of<TResult>(TResult testValue
4243
}, new Context(OperationKey))
4344
.ShouldBeEquivalentTo(testValue);
4445
underlyingDelegateExecuteCount.Should().Be(1);
46+
47+
return Task.CompletedTask;
4548
}
4649
}
4750
}
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_Async_ByteArray : CacheRoundTripSpecsAsyncBase<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_Async_String : CacheRoundTripSpecsAsyncBase<string> { }
6+
}
7+
8+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#if !NETCOREAPP1_1
2+
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using FluentAssertions;
7+
8+
namespace Polly.Caching.Distributed.Specs.Integration
9+
{
10+
public abstract class CacheRoundTripSpecsAsyncBase<TCache> : CacheRoundTripSpecsBase
11+
{
12+
public override async Task Should_roundtrip_this_variant_of<TResult>(TResult testValue)
13+
{
14+
// Arrange
15+
var (cacheProvider, cache) = CachePolicyFactory.CreateAsyncCachePolicy<TCache, TResult>();
16+
17+
// Assert - should not be in cache
18+
(bool cacheHit1, TResult fromCache1) = await cacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false);
19+
cacheHit1.Should().BeFalse();
20+
fromCache1.Should().Be(default(TResult));
21+
22+
// Act - should execute underlying delegate and place in cache
23+
int underlyingDelegateExecuteCount = 0;
24+
(await cache.ExecuteAsync(ctx =>
25+
{
26+
underlyingDelegateExecuteCount++;
27+
return Task.FromResult(testValue);
28+
}, new Context(OperationKey)))
29+
.ShouldBeEquivalentTo(testValue);
30+
31+
// Assert - should have executed underlying delegate
32+
underlyingDelegateExecuteCount.Should().Be(1);
33+
34+
// Assert - should be in cache
35+
(bool cacheHit2, TResult fromCache2) = await cacheProvider.TryGetAsync(OperationKey, CancellationToken.None, false);
36+
cacheHit2.Should().BeTrue();
37+
fromCache2.ShouldBeEquivalentTo(testValue);
38+
39+
// Act - should execute underlying delegate and place in cache
40+
(await cache.ExecuteAsync(ctx =>
41+
{
42+
underlyingDelegateExecuteCount++;
43+
throw new Exception("Cache should be used so this should not get invoked.");
44+
}, new Context(OperationKey)))
45+
.ShouldBeEquivalentTo(testValue);
46+
underlyingDelegateExecuteCount.Should().Be(1);
47+
}
48+
}
49+
}
50+
51+
#endif

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)Integration\ByteArraySerializer.cs" />
1313
<Compile Include="$(MSBuildThisFileDirectory)Integration\CachePolicyFactory.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsAsyncBase.cs" />
1415
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsBase.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsSyncBase.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_ByteArray.cs" />
18+
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Async_String.cs" />
1619
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_ByteArray.cs" />
1720
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecs_NetStandardIDistributedCacheProvider_Sync_String.cs" />
1821
<Compile Include="$(MSBuildThisFileDirectory)Unit\CacheProviderHelperTests.cs" />

0 commit comments

Comments
 (0)