Skip to content

Commit 46e132b

Browse files
authored
Merge pull request #7 from cnblogs/add-testing
Add testing
2 parents c99fdf1 + 9ef6ebe commit 46e132b

File tree

9 files changed

+193
-5
lines changed

9 files changed

+193
-5
lines changed

.github/workflows/ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
branches: [ "main" ]
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
container: mcr.microsoft.com/dotnet/sdk:7.0
12+
13+
services:
14+
redis:
15+
image: redis
16+
options: >-
17+
--health-cmd "redis-cli ping"
18+
--health-interval 10s
19+
--health-timeout 5s
20+
--health-retries 5
21+
22+
steps:
23+
- name: Check out repository code
24+
uses: actions/checkout@v3
25+
- name: Build
26+
run: dotnet build -c Release
27+
- name: Test
28+
run: dotnet test -c Release

ServiceStackRedisCache.sln

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27130.2010
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.4.33205.214
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5A04042D-438D-407B-B239-0CAD6A2BD8C2}"
66
EndProject
77
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Caching.ServiceStackRedis", "src\Microsoft.Extensions.Caching.ServiceStackRedis\Microsoft.Extensions.Caching.ServiceStackRedis.csproj", "{5694BC38-B266-447E-A743-DA1139FCB257}"
88
EndProject
9+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{59F8766F-A9B2-4EE5-85AE-4D0E590EB65F}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceStackRedisCacheTests", "test\ServiceStackRedisCacheTests\ServiceStackRedisCacheTests.csproj", "{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB}"
12+
EndProject
913
Global
1014
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1115
Debug|Any CPU = Debug|Any CPU
@@ -16,12 +20,17 @@ Global
1620
{5694BC38-B266-447E-A743-DA1139FCB257}.Debug|Any CPU.Build.0 = Debug|Any CPU
1721
{5694BC38-B266-447E-A743-DA1139FCB257}.Release|Any CPU.ActiveCfg = Release|Any CPU
1822
{5694BC38-B266-447E-A743-DA1139FCB257}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB}.Release|Any CPU.Build.0 = Release|Any CPU
1927
EndGlobalSection
2028
GlobalSection(SolutionProperties) = preSolution
2129
HideSolutionNode = FALSE
2230
EndGlobalSection
2331
GlobalSection(NestedProjects) = preSolution
2432
{5694BC38-B266-447E-A743-DA1139FCB257} = {5A04042D-438D-407B-B239-0CAD6A2BD8C2}
33+
{A7224FF5-F598-44E8-90DD-7D34EE8AC0FB} = {59F8766F-A9B2-4EE5-85AE-4D0E590EB65F}
2534
EndGlobalSection
2635
GlobalSection(ExtensibilityGlobals) = postSolution
2736
SolutionGuid = {101BFF2A-6CDA-4ED7-A483-7186847DF520}

src/Microsoft.Extensions.Caching.ServiceStackRedis/ServiceStackRedisCacheServiceCollectionExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ public static IServiceCollection AddDistributedServiceStackRedisCache(this IServ
2323

2424
services.AddOptions();
2525
services.Configure(setupAction);
26-
services.Add(ServiceDescriptor.Singleton<IDistributedCache, ServiceStackRedisCache>());
26+
services.TryAddSingleton<IDistributedCache, ServiceStackRedisCache>();
2727

2828
return services;
2929
}
3030

31+
#if NET6_0_OR_GREATER
32+
public static IServiceCollection AddDistributedServiceStackRedisCache(this IServiceCollection services, string configName)
33+
{
34+
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
35+
return services.AddDistributedServiceStackRedisCache(config.GetSection(configName));
36+
}
37+
#endif
38+
3139
public static IServiceCollection AddDistributedServiceStackRedisCache(this IServiceCollection services, IConfigurationSection section)
3240
{
3341
if (services == null)
@@ -41,8 +49,7 @@ public static IServiceCollection AddDistributedServiceStackRedisCache(this IServ
4149
}
4250

4351
services.Configure<ServiceStackRedisCacheOptions>(section);
44-
45-
services.Add(ServiceDescriptor.Transient<IDistributedCache, ServiceStackRedisCache>());
52+
services.TryAddSingleton<IDistributedCache, ServiceStackRedisCache>();
4653

4754
return services;
4855
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace ServiceStackRedisCacheTests
8+
{
9+
[CollectionDefinition(nameof(DistributedCacheCollection), DisableParallelization = true)]
10+
public class DistributedCacheCollection : ICollectionFixture<DistributedCacheFixture>
11+
{
12+
}
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Markup;
7+
using Microsoft.Extensions.Caching.Distributed;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.DependencyInjection.Extensions;
11+
using Xunit;
12+
using Xunit.Abstractions;
13+
14+
namespace ServiceStackRedisCacheTests;
15+
16+
public class DistributedCacheFixture
17+
{
18+
public IDistributedCache DistributedCache { get; private set; }
19+
public string KeyPostfix { get; private set; }
20+
21+
public DistributedCacheFixture()
22+
{
23+
DistributedCache = GetInstance();
24+
KeyPostfix = "-" + Guid.NewGuid();
25+
}
26+
27+
private IDistributedCache GetInstance()
28+
{
29+
IServiceCollection services = new ServiceCollection();
30+
IConfiguration conf = new ConfigurationBuilder().
31+
AddJsonFile("appsettings.json", optional: false)
32+
.Build();
33+
services.AddSingleton(conf);
34+
services.AddDistributedServiceStackRedisCache("redis");
35+
return services.BuildServiceProvider().GetRequiredService<IDistributedCache>();
36+
}
37+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.Extensions.Caching.Distributed;
2+
using Xunit.Priority;
3+
using PriorityAttribute = Xunit.Priority.PriorityAttribute;
4+
5+
namespace ServiceStackRedisCacheTests;
6+
7+
[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]
8+
[Collection(nameof(DistributedCacheCollection))]
9+
public class DistributedCacheTests
10+
{
11+
private readonly string _key = "distributed_cache";
12+
private readonly string _keyAsync = "distributed_cache_async";
13+
private const string _value = "Coding changes the world";
14+
private readonly IDistributedCache _distributedCache;
15+
16+
public DistributedCacheTests(DistributedCacheFixture fixture)
17+
{
18+
_distributedCache = fixture.DistributedCache;
19+
_key += fixture.KeyPostfix;
20+
_keyAsync += fixture.KeyPostfix;
21+
}
22+
23+
[Fact, Priority(1)]
24+
public async Task Sets_a_value_with_the_given_key()
25+
{
26+
_distributedCache.SetString(_key, _value);
27+
await _distributedCache.SetStringAsync(_keyAsync, _value);
28+
}
29+
30+
[Fact, Priority(2)]
31+
public async Task Gets_a_value_with_the_given_key()
32+
{
33+
Assert.Equal(_value, _distributedCache.GetString(_key));
34+
Assert.Equal(_value, await _distributedCache.GetStringAsync(_keyAsync));
35+
}
36+
37+
[Fact, Priority(3)]
38+
public async Task Refreshes_a_value_in_the_cache_based_on_its_key()
39+
{
40+
_distributedCache.Refresh(_key);
41+
await _distributedCache.RefreshAsync(_keyAsync);
42+
}
43+
44+
[Fact, Priority(4)]
45+
public async Task Removes_the_value_with_the_given_key()
46+
{
47+
_distributedCache.Remove(_key);
48+
await _distributedCache.RemoveAsync(_keyAsync);
49+
Assert.Null(_distributedCache.Get(_key));
50+
Assert.Null(await _distributedCache.GetAsync(_keyAsync));
51+
}
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
13+
<PackageReference Include="xunit" Version="2.4.2" />
14+
<PackageReference Include="Xunit.Priority" Version="1.1.6" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.1.2">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\src\Microsoft.Extensions.Caching.ServiceStackRedis\Microsoft.Extensions.Caching.ServiceStackRedis.csproj" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<None Update="appsettings.json">
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</None>
33+
</ItemGroup>
34+
35+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using Xunit;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"redis": {
3+
"host": "redis",
4+
"port": 6379
5+
}
6+
}

0 commit comments

Comments
 (0)