feat: 完成单元测试搭建
This commit is contained in:
@@ -1,110 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Volo.Abp.Uow;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Abp.Test.Demo
|
||||
{
|
||||
public class ThreadDb_Test : YiAbpTestBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 并发
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Repository_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
var rep = GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
List<Task> tasks = new List<Task>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
await rep.GetListAsync();
|
||||
}));
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
await Console.Out.WriteLineAsync("成功");
|
||||
}
|
||||
catch
|
||||
(Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工作单元
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Uow_In_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
var rep = GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
List<Task> tasks = new List<Task>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
using (var uow = GetRequiredService<IUnitOfWorkManager>().Begin(true, true))
|
||||
{
|
||||
await rep.GetListAsync();
|
||||
await uow.CompleteAsync();
|
||||
}
|
||||
}));
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
await Console.Out.WriteLineAsync("成功");
|
||||
}
|
||||
catch
|
||||
(Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 工作单元
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Uow_Out_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
var rep = GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
List<Task> tasks = new List<Task>();
|
||||
using (var uow = GetRequiredService<IUnitOfWorkManager>().Begin(true, true))
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
await rep.GetListAsync();
|
||||
await uow.CompleteAsync();
|
||||
}));
|
||||
}
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
await Console.Out.WriteLineAsync("成功");
|
||||
}
|
||||
catch
|
||||
(Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
|
||||
namespace Yi.Abp.Test.Demo
|
||||
{
|
||||
public class User_Test : YiAbpTestBase
|
||||
{
|
||||
[Fact]
|
||||
public async Task Get_User_List_Test()
|
||||
{
|
||||
var service = GetRequiredService<IUserService>();
|
||||
var user = await service.GetListAsync(new Framework.Rbac.Application.Contracts.Dtos.User.UserGetListInputVo { UserName = UserConst.Admin });
|
||||
user.ShouldNotBeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,6 @@ namespace Yi.Abp.Test
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
Configure<AbpBackgroundWorkerOptions>(options=>options.IsEnabled=false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NSubstitute.Extensions;
|
||||
|
||||
namespace Yi.Abp.Test
|
||||
namespace Yi.Abp.Test;
|
||||
|
||||
public class YiAbpTestWebBase:YiAbpTestBase
|
||||
{
|
||||
public class YiAbpTestWebBase:YiAbpTestBase
|
||||
public HttpContext HttpContext { get; private set; }
|
||||
public YiAbpTestWebBase():base()
|
||||
{
|
||||
public HttpContext HttpContext { get; private set; }
|
||||
public YiAbpTestWebBase():base()
|
||||
{
|
||||
HttpContext httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
|
||||
this.ConfigureHttpContext(httpContext);
|
||||
HttpContext = httpContext;
|
||||
IApplicationBuilder app = new ApplicationBuilder(this.ServiceProvider);
|
||||
RequestDelegate httpDelegate = app.Build();
|
||||
httpDelegate.Invoke(httpContext);
|
||||
}
|
||||
HttpContext httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
|
||||
this.ConfigureHttpContext(httpContext);
|
||||
HttpContext = httpContext;
|
||||
IApplicationBuilder app = new ApplicationBuilder(this.ServiceProvider);
|
||||
RequestDelegate httpDelegate = app.Build();
|
||||
httpDelegate.Invoke(httpContext);
|
||||
}
|
||||
|
||||
public override void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||
{
|
||||
service.Replace(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(DefaultHttpContextAccessor), ServiceLifetime.Singleton));
|
||||
base.ConfigureServices(host, service);
|
||||
}
|
||||
public override void ConfigureServices(HostBuilderContext host, IServiceCollection service)
|
||||
{
|
||||
service.Replace(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(DefaultHttpContextAccessor), ServiceLifetime.Singleton));
|
||||
base.ConfigureServices(host, service);
|
||||
}
|
||||
|
||||
protected virtual void ConfigureHttpContext(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Request.Path= "/test";
|
||||
}
|
||||
protected virtual void ConfigureHttpContext(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Request.Path= "/test";
|
||||
}
|
||||
}
|
||||
|
||||
internal class DefaultHttpContextAccessor : IHttpContextAccessor
|
||||
{
|
||||
internal static HttpContext? CurrentHttpContext { get; set; } = new DefaultHttpContext();
|
||||
public HttpContext? HttpContext { get => CurrentHttpContext; set => throw new NotImplementedException(); }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Yi.Abp.Test.Demo
|
||||
namespace Yi.Abp.Test.example
|
||||
{
|
||||
public class HttpUser_Test : YiAbpTestWebBase
|
||||
{
|
||||
52
Yi.Abp.Net8/test/Yi.Abp.Test/example/ThreadDb_Test.cs
Normal file
52
Yi.Abp.Net8/test/Yi.Abp.Test/example/ThreadDb_Test.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Volo.Abp.Uow;
|
||||
using Xunit;
|
||||
using Yi.Framework.Rbac.Domain.Entities;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Abp.Test.example
|
||||
{
|
||||
public class ThreadDb_Test : YiAbpTestBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 工作单元
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Fact]
|
||||
public async Task Uow_In_Test()
|
||||
{
|
||||
try
|
||||
{
|
||||
var uowManager = GetRequiredService<IUnitOfWorkManager>();
|
||||
var tasks = new List<Task>();
|
||||
// 创建10个任务但不立即执行
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var task = new Task(async () =>
|
||||
{
|
||||
using (var uow = uowManager.Begin())
|
||||
{
|
||||
var rep = GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
var result = await rep.GetListAsync();
|
||||
await uow.CompleteAsync();
|
||||
}
|
||||
});
|
||||
tasks.Add(task);
|
||||
}
|
||||
|
||||
// 同时启动所有任务
|
||||
foreach (var task in tasks)
|
||||
{
|
||||
task.Start();
|
||||
}
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
// 如果执行到这里没有抛出异常,说明并发测试成功
|
||||
Assert.True(true, "并发工作单元测试成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Assert.True(false, $"并发工作单元测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,12 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test.System
|
||||
{
|
||||
public class Account_Test : YiTestWebBase
|
||||
public class AccountFrameworkRbacTest : YiFrameworkRbacTestWebBase
|
||||
{
|
||||
|
||||
private IAccountService _accountService;
|
||||
private ISqlSugarRepository<UserAggregateRoot> _userRepository;
|
||||
public Account_Test()
|
||||
public AccountFrameworkRbacTest()
|
||||
{
|
||||
_accountService = GetRequiredService<IAccountService>();
|
||||
_userRepository = GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
@@ -13,11 +13,11 @@ using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test.System
|
||||
{
|
||||
public class User_Test : YiTestBase
|
||||
public class UserFrameworkRbacTest : YiFrameworkRbacTestBase
|
||||
{
|
||||
private IUserService _userService;
|
||||
private ISqlSugarRepository<UserAggregateRoot> _repository;
|
||||
public User_Test()
|
||||
public UserFrameworkRbacTest()
|
||||
{
|
||||
_userService = ServiceProvider.GetRequiredService<IUserService>();
|
||||
_repository = ServiceProvider.GetRequiredService<ISqlSugarRepository<UserAggregateRoot>>();
|
||||
@@ -8,11 +8,11 @@ using Yi.Framework.Rbac.SqlSugarCore.Repositories;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test
|
||||
{
|
||||
public class YiTestBase : AbpTestBaseWithServiceProvider
|
||||
public class YiFrameworkRbacTestBase : AbpTestBaseWithServiceProvider
|
||||
{
|
||||
public ILogger Logger { get; private set; }
|
||||
protected IServiceScope TestServiceScope { get; }
|
||||
public YiTestBase()
|
||||
public YiFrameworkRbacTestBase()
|
||||
{
|
||||
//在启动之前,清除sqlite全库,由于非常危险,建议使用sqlite
|
||||
//Microsoft.Data.Sqlite.SqliteConnection.ClearAllPools();
|
||||
@@ -15,8 +15,7 @@ namespace Yi.Framework.Rbac.Test
|
||||
typeof(YiFrameworkRbacApplicationModule),
|
||||
typeof(YiFrameworkRbacSqlSugarCoreModule),
|
||||
|
||||
typeof(AbpAutofacModule),
|
||||
typeof(AbpAuditingModule)
|
||||
typeof(AbpAutofacModule)
|
||||
)]
|
||||
public class YiFrameworkRbacTestModule : AbpModule
|
||||
{
|
||||
|
||||
@@ -12,10 +12,10 @@ using NSubstitute.Extensions;
|
||||
|
||||
namespace Yi.Framework.Rbac.Test
|
||||
{
|
||||
public class YiTestWebBase : YiTestBase
|
||||
public class YiFrameworkRbacTestWebBase : YiFrameworkRbacTestBase
|
||||
{
|
||||
public HttpContext HttpContext { get; private set; }
|
||||
public YiTestWebBase() : base()
|
||||
public YiFrameworkRbacTestWebBase() : base()
|
||||
{
|
||||
HttpContext httpContext = DefaultHttpContextAccessor.CurrentHttpContext;
|
||||
ConfigureHttpContext(httpContext);
|
||||
Reference in New Issue
Block a user