Skip to content

Commit cc6f4c1

Browse files
author
DwGoing
committed
🧪 test(): 增加Core单元测试
1 parent 913cbb8 commit cc6f4c1

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

.github/workflows/Ubuntu.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ on:
44
push:
55
branches:
66
- 5.1.x
7+
paths-ignore:
8+
- ".github/**"
9+
- "build/**"
10+
- ".gitignore"
11+
- "LICENSE"
12+
- "*.md"
13+
- "*.yml"
714
pull_request:
815
branches:
916
- 5.1.x

DwFramework.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DwFramework.Web", "src\DwFr
2727
EndProject
2828
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DwFramework.Quartz", "src\DwFramework.Quartz\DwFramework.Quartz.csproj", "{468D79FF-74BF-42DD-AC04-7A30146B4D7D}"
2929
EndProject
30+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{789A7942-7765-48EE-B1FE-DD9399850EFF}"
31+
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core.Tests", "test\Core.Tests\Core.Tests.csproj", "{24EB5FE1-6275-4D73-AD71-0582E252DE15}"
33+
EndProject
3034
Global
3135
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3236
Debug|Any CPU = Debug|Any CPU
@@ -160,6 +164,18 @@ Global
160164
{DCD0D056-5104-4710-9F52-FDED3EEACED0}.Release|x64.Build.0 = Release|Any CPU
161165
{DCD0D056-5104-4710-9F52-FDED3EEACED0}.Release|x86.ActiveCfg = Release|Any CPU
162166
{DCD0D056-5104-4710-9F52-FDED3EEACED0}.Release|x86.Build.0 = Release|Any CPU
167+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
168+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|Any CPU.Build.0 = Debug|Any CPU
169+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|x64.ActiveCfg = Debug|Any CPU
170+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|x64.Build.0 = Debug|Any CPU
171+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|x86.ActiveCfg = Debug|Any CPU
172+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Debug|x86.Build.0 = Debug|Any CPU
173+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|Any CPU.ActiveCfg = Release|Any CPU
174+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|Any CPU.Build.0 = Release|Any CPU
175+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|x64.ActiveCfg = Release|Any CPU
176+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|x64.Build.0 = Release|Any CPU
177+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|x86.ActiveCfg = Release|Any CPU
178+
{24EB5FE1-6275-4D73-AD71-0582E252DE15}.Release|x86.Build.0 = Release|Any CPU
163179
EndGlobalSection
164180
GlobalSection(NestedProjects) = preSolution
165181
{A0F6F39C-CDC3-48CC-ACB4-E622D7D8690F} = {BBBF6BB9-A10F-4D57-8F43-3D2D2C657895}
@@ -172,5 +188,6 @@ Global
172188
{691625CC-692D-4D1E-804A-FF07F1117F65} = {98D6A6FD-8ABC-49BC-B19D-CAE15BE955C9}
173189
{468D79FF-74BF-42DD-AC04-7A30146B4D7D} = {98D6A6FD-8ABC-49BC-B19D-CAE15BE955C9}
174190
{DCD0D056-5104-4710-9F52-FDED3EEACED0} = {BBBF6BB9-A10F-4D57-8F43-3D2D2C657895}
191+
{24EB5FE1-6275-4D73-AD71-0582E252DE15} = {789A7942-7765-48EE-B1FE-DD9399850EFF}
175192
EndGlobalSection
176193
EndGlobal

src/DwFramework.Core/Plugins/Generator/GeneratorExtension.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Autofac;
34

45
namespace DwFramework.Core.Generator
@@ -13,13 +14,21 @@ public static class GeneratorExtension
1314
/// <param name="startTime"></param>
1415
/// <param name="isGlobal"></param>
1516
/// <returns></returns>
16-
public static ServiceHost cSnowflakeGenerator(this ServiceHost host, long workerId, DateTime startTime, bool isGlobal = true)
17+
public static ServiceHost ConfigureSnowflakeGenerator(this ServiceHost host, long workerId, DateTime startTime, bool isGlobal = true)
1718
{
1819
return host.ConfigureContainer(builder =>
1920
{
2021
var registration = builder.Register(_ => new SnowflakeGenerator(workerId, startTime));
2122
if (isGlobal) registration.SingleInstance();
2223
});
2324
}
25+
26+
/// <summary>
27+
/// 获取雪花生成器
28+
/// </summary>
29+
/// <param name="provider"></param>
30+
/// <typeparam name="SnowflakeGenerator"></typeparam>
31+
/// <returns></returns>
32+
public static SnowflakeGenerator GetSnowflakeGenerator(this IServiceProvider provider) => provider.GetService<SnowflakeGenerator>();
2433
}
2534
}

test/Core.Tests/Core.Tests.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net5.0</TargetFramework>
4+
<IsPackable>false</IsPackable>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
8+
<PackageReference Include="xunit" Version="2.4.1" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
<PrivateAssets>all</PrivateAssets>
12+
</PackageReference>
13+
<PackageReference Include="coverlet.collector" Version="3.0.2">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\src\DwFramework.Core\DwFramework.Core.csproj" />
20+
</ItemGroup>
21+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Xunit;
3+
using DwFramework.Core;
4+
using DwFramework.Core.Generator;
5+
6+
namespace Core.Tests
7+
{
8+
public class ExtensionUnitTest
9+
{
10+
public ExtensionUnitTest()
11+
{
12+
var host = new ServiceHost();
13+
host.ConfigureSnowflakeGenerator(1, DateTime.Parse("1970.01.01"));
14+
_ = host.RunAsync();
15+
}
16+
17+
[Fact]
18+
public void GenerateSnowflakeId()
19+
{
20+
var generator = ServiceHost.ServiceProvider.GetSnowflakeGenerator();
21+
var id = generator.GenerateId();
22+
Console.WriteLine(id);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)