添加Cache单元测试
Some checks failed
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
.NET Core / build (push) Has been cancelled

This commit is contained in:
LemonNoCry 2024-07-31 17:44:17 +08:00
parent c032993b2f
commit 64c2e8c0e4
No known key found for this signature in database
5 changed files with 116 additions and 53 deletions

View File

@ -758,21 +758,6 @@
Values控制器 Values控制器
</summary> </summary>
</member> </member>
<member name="M:Blog.Core.Controllers.ValuesController.#ctor(Blog.Core.IServices.IBlogArticleServices,AutoMapper.IMapper,Blog.Core.IServices.IAdvertisementServices,Blog.Core.Model.Love,Blog.Core.IServices.IRoleModulePermissionServices,Blog.Core.Common.HttpContextUser.IUser,Blog.Core.IServices.IPasswordLibServices,Blog.Core.Common.Https.HttpPolly.IHttpPollyHelper,Blog.Core.EventBus.IRabbitMQPersistentConnection,Microsoft.Extensions.Options.IOptions{Blog.Core.Common.Option.SeqOptions})">
<summary>
ValuesController
</summary>
<param name="blogArticleServices"></param>
<param name="mapper"></param>
<param name="advertisementServices"></param>
<param name="love"></param>
<param name="roleModulePermissionServices"></param>
<param name="user"></param>
<param name="passwordLibServices"></param>
<param name="httpPollyHelper"></param>
<param name="persistentConnection"></param>
<param name="seqOptions"></param>
</member>
<member name="M:Blog.Core.Controllers.ValuesController.TestRabbitMqPublish"> <member name="M:Blog.Core.Controllers.ValuesController.TestRabbitMqPublish">
<summary> <summary>
测试Rabbit消息队列发送 测试Rabbit消息队列发送
@ -900,6 +885,12 @@
</summary> </summary>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Blog.Core.Controllers.ValuesController.TestCacheAsync">
<summary>
测试缓存
</summary>
<returns></returns>
</member>
<member name="T:Blog.Core.Controllers.WeChatCompanyController"> <member name="T:Blog.Core.Controllers.WeChatCompanyController">
<summary> <summary>
WeChatCompanyController WeChatCompanyController

View File

@ -19,6 +19,7 @@ using RabbitMQ.Client.Events;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Text; using System.Text;
using Blog.Core.Common.Caches.Interface;
using Blog.Core.Common.Utility; using Blog.Core.Common.Utility;
namespace Blog.Core.Controllers namespace Blog.Core.Controllers
@ -45,29 +46,11 @@ namespace Blog.Core.Controllers
private readonly IHttpPollyHelper _httpPollyHelper; private readonly IHttpPollyHelper _httpPollyHelper;
private readonly IRabbitMQPersistentConnection _persistentConnection; private readonly IRabbitMQPersistentConnection _persistentConnection;
private readonly SeqOptions _seqOptions; private readonly SeqOptions _seqOptions;
private readonly ICaching _cache;
/// <summary> public ValuesController(IBlogArticleServices blogArticleServices, IMapper mapper, IAdvertisementServices advertisementServices, Love love,
/// ValuesController IRoleModulePermissionServices roleModulePermissionServices, IUser user, IPasswordLibServices passwordLibServices,
/// </summary> IHttpPollyHelper httpPollyHelper, IRabbitMQPersistentConnection persistentConnection, IOptions<SeqOptions> seqOptions, ICaching caching)
/// <param name="blogArticleServices"></param>
/// <param name="mapper"></param>
/// <param name="advertisementServices"></param>
/// <param name="love"></param>
/// <param name="roleModulePermissionServices"></param>
/// <param name="user"></param>
/// <param name="passwordLibServices"></param>
/// <param name="httpPollyHelper"></param>
/// <param name="persistentConnection"></param>
/// <param name="seqOptions"></param>
public ValuesController(IBlogArticleServices blogArticleServices
, IMapper mapper
, IAdvertisementServices advertisementServices
, Love love
, IRoleModulePermissionServices roleModulePermissionServices
, IUser user, IPasswordLibServices passwordLibServices
, IHttpPollyHelper httpPollyHelper
, IRabbitMQPersistentConnection persistentConnection
, IOptions<SeqOptions> seqOptions)
{ {
// 测试 Authorize 和 mapper // 测试 Authorize 和 mapper
_mapper = mapper; _mapper = mapper;
@ -85,6 +68,7 @@ namespace Blog.Core.Controllers
// httpPolly // httpPolly
_httpPollyHelper = httpPollyHelper; _httpPollyHelper = httpPollyHelper;
_persistentConnection = persistentConnection; _persistentConnection = persistentConnection;
_cache = caching;
_seqOptions = seqOptions.Value; _seqOptions = seqOptions.Value;
} }
@ -99,6 +83,7 @@ namespace Blog.Core.Controllers
{ {
_persistentConnection.TryConnect(); _persistentConnection.TryConnect();
} }
_persistentConnection.PublishMessage("Hello, RabbitMQ!", exchangeName: "blogcore", routingKey: "myRoutingKey"); _persistentConnection.PublishMessage("Hello, RabbitMQ!", exchangeName: "blogcore", routingKey: "myRoutingKey");
return Ok(); return Ok();
} }
@ -484,6 +469,31 @@ namespace Blog.Core.Controllers
{ {
return IdGeneratorUtility.NextId(); return IdGeneratorUtility.NextId();
} }
/// <summary>
/// 测试缓存
/// </summary>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<string>> TestCacheAsync()
{
await _cache.SetAsync("test", "test", new TimeSpan(0, 10, 0));
var result = await _cache.GetAsync<string>("test");
if (!"test".Equals(result))
{
return Failed("缓存失败,值不一样");
}
var count = _cache.GetAllCacheKeys().Count;
if (count <= 0)
{
return Failed("缓存失败,数量不对");
}
return Success<string>("");
}
} }
public class ClaimDto public class ClaimDto

View File

@ -42,4 +42,14 @@ public static class InternalApp
{ {
RootServices = app.Services; RootServices = app.Services;
} }
public static void ConfigureApplication(this IServiceCollection services)
{
InternalServices = services;
}
public static void ConfigureApplication(this IServiceProvider services)
{
RootServices = services;
}
} }

View File

@ -0,0 +1,37 @@
using Autofac;
using Blog.Core.Common;
using Blog.Core.Common.Caches.Interface;
using Xunit;
using Xunit.Abstractions;
namespace Blog.Core.Tests.Common_Test;
public class CacheTest
{
private readonly ITestOutputHelper _testOutputHelper;
DI_Test dI_Test = new DI_Test();
private readonly ICaching _cache;
public CacheTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
dI_Test.Build();
_cache = App.GetService<ICaching>();
}
[Fact]
public void TestCaching()
{
_cache.Set("test", "test", new TimeSpan(0, 10, 0));
var result = _cache.Get<string>("test");
Assert.Equal("test", result);
var caches = _cache.GetAllCacheKeys();
_testOutputHelper.WriteLine(caches.ToJson());
Assert.NotNull(caches);
var count = _cache.GetAllCacheKeys().Count;
Assert.Equal(1, count);
}
}

View File

@ -18,6 +18,8 @@ using Microsoft.IdentityModel.Tokens;
using System.Reflection; using System.Reflection;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using Blog.Core.Common.Core;
using Blog.Core.Extensions.ServiceExtensions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Blog.Core.Tests namespace Blog.Core.Tests
@ -51,7 +53,11 @@ namespace Blog.Core.Tests
var basePath = AppContext.BaseDirectory; var basePath = AppContext.BaseDirectory;
IServiceCollection services = new ServiceCollection(); IServiceCollection services = new ServiceCollection();
services.ConfigureApplication();
services.AddLogging();
services.AddAutoMapperSetup(); services.AddAutoMapperSetup();
services.AddCacheSetup();
services.AddSingleton(new AppSettings(basePath)); services.AddSingleton(new AppSettings(basePath));
services.AddScoped<DBSeed>(); services.AddScoped<DBSeed>();
@ -136,9 +142,18 @@ namespace Blog.Core.Tests
builder.Populate(services); builder.Populate(services);
//使用已进行的组件登记创建新容器 //使用已进行的组件登记创建新容器
var ApplicationContainer = builder.Build(); var applicationContainer = builder.Build();
return applicationContainer;
}
return ApplicationContainer; public IServiceProvider Build()
{
var container = DICollections();
var serviceProvider = new AutofacServiceProvider(container);
serviceProvider.ConfigureApplication();
App.IsBuild = true;
App.IsRun = true;
return serviceProvider;
} }
} }
} }