mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
refactor(architecture): 重构架构初始化方法和测试基类
- 将InitializeAsync方法改为真正的异步方法并正确等待内部初始化 - 移除多余的返回语句以符合异步模式 - 创建TestArchitectureBase基类来统一测试架构的公共功能 - 将AsyncTestArchitecture和SyncTestArchitecture继承自TestArchitectureBase - 移除重复的属性和方法实现 - 添加完整的异步架构测试用例 - 包括组件初始化、阶段转换、异常处理等测试场景
This commit is contained in:
parent
3729e2ead2
commit
e19f4ce5a1
@ -1,7 +1,4 @@
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.system;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
@ -9,47 +6,17 @@ namespace GFramework.Core.Tests.architecture;
|
||||
/// <summary>
|
||||
/// 异步测试架构,用于测试异步模型和系统的初始化
|
||||
/// </summary>
|
||||
public class AsyncTestArchitecture : Architecture
|
||||
public class AsyncTestArchitecture : TestArchitectureBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化完成事件是否触发
|
||||
/// </summary>
|
||||
public bool ReadyEventFired { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Init 方法是否调用
|
||||
/// </summary>
|
||||
public bool InitCalled { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 阶段进入记录
|
||||
/// </summary>
|
||||
public List<ArchitecturePhase> PhaseHistory { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 异步初始化架构
|
||||
/// </summary>
|
||||
protected override void Init()
|
||||
{
|
||||
InitCalled = true;
|
||||
|
||||
// 注册模型
|
||||
RegisterModel(new AsyncTestModel());
|
||||
|
||||
// 注册系统
|
||||
RegisterSystem(new AsyncTestSystem());
|
||||
|
||||
// 订阅 Ready 事件
|
||||
Context.RegisterEvent<ArchitectureEvents.ArchitectureLifecycleReadyEvent>(_ => { ReadyEventFired = true; });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入阶段时记录
|
||||
/// </summary>
|
||||
/// <param name="next"></param>
|
||||
protected override void EnterPhase(ArchitecturePhase next)
|
||||
{
|
||||
base.EnterPhase(next);
|
||||
PhaseHistory.Add(next);
|
||||
base.Init();
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,4 @@
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.events;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.system;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
@ -9,52 +6,15 @@ namespace GFramework.Core.Tests.architecture;
|
||||
/// <summary>
|
||||
/// 同步测试架构类,用于测试架构的生命周期和事件处理
|
||||
/// </summary>
|
||||
public sealed class SyncTestArchitecture : Architecture
|
||||
public sealed class SyncTestArchitecture : TestArchitectureBase
|
||||
{
|
||||
private Action<SyncTestArchitecture>? _postRegistrationHook;
|
||||
|
||||
/// <summary>
|
||||
/// 获取就绪事件是否已触发的状态
|
||||
/// </summary>
|
||||
public bool ReadyEventFired { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取初始化方法是否已调用的状态
|
||||
/// </summary>
|
||||
public bool InitCalled { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构阶段历史记录列表
|
||||
/// </summary>
|
||||
public List<ArchitecturePhase> PhaseHistory { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 添加注册后钩子函数
|
||||
/// </summary>
|
||||
/// <param name="hook">要添加的钩子函数</param>
|
||||
public void AddPostRegistrationHook(Action<SyncTestArchitecture> hook) => _postRegistrationHook = hook;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化架构组件,注册模型、系统并设置事件监听器
|
||||
/// </summary>
|
||||
protected override void Init()
|
||||
{
|
||||
InitCalled = true;
|
||||
|
||||
RegisterModel(new TestModel());
|
||||
RegisterSystem(new TestSystem());
|
||||
_postRegistrationHook?.Invoke(this);
|
||||
Context.RegisterEvent<ArchitectureEvents.ArchitectureLifecycleReadyEvent>(_ => { ReadyEventFired = true; });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入指定架构阶段时的处理方法,记录阶段历史
|
||||
/// </summary>
|
||||
/// <param name="next">要进入的下一个架构阶段</param>
|
||||
protected override void EnterPhase(ArchitecturePhase next)
|
||||
{
|
||||
base.EnterPhase(next);
|
||||
// 记录进入的架构阶段到历史列表中
|
||||
PhaseHistory.Add(next);
|
||||
base.Init();
|
||||
}
|
||||
}
|
||||
55
GFramework.Core.Tests/architecture/TestArchitectureBase.cs
Normal file
55
GFramework.Core.Tests/architecture/TestArchitectureBase.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.events;
|
||||
|
||||
namespace GFramework.Core.Tests.architecture;
|
||||
|
||||
/// <summary>
|
||||
/// 测试架构基类,提供通用的测试架构功能
|
||||
/// </summary>
|
||||
public abstract class TestArchitectureBase : Architecture
|
||||
{
|
||||
private Action<TestArchitectureBase>? _postRegistrationHook;
|
||||
|
||||
/// <summary>
|
||||
/// 获取就绪事件是否已触发的状态
|
||||
/// </summary>
|
||||
public bool ReadyEventFired { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取初始化方法是否已调用的状态
|
||||
/// </summary>
|
||||
public bool InitCalled { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取架构阶段历史记录列表
|
||||
/// </summary>
|
||||
public List<ArchitecturePhase> PhaseHistory { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 添加注册后钩子函数
|
||||
/// </summary>
|
||||
/// <param name="hook">要添加的钩子函数</param>
|
||||
public void AddPostRegistrationHook(Action<TestArchitectureBase> hook) => _postRegistrationHook = hook;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化架构组件,注册模型、系统并设置事件监听器
|
||||
/// </summary>
|
||||
protected override void Init()
|
||||
{
|
||||
InitCalled = true;
|
||||
_postRegistrationHook?.Invoke(this);
|
||||
Context.RegisterEvent<ArchitectureEvents.ArchitectureLifecycleReadyEvent>(_ => { ReadyEventFired = true; });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 进入指定架构阶段时的处理方法,记录阶段历史
|
||||
/// </summary>
|
||||
/// <param name="next">要进入的下一个架构阶段</param>
|
||||
protected override void EnterPhase(ArchitecturePhase next)
|
||||
{
|
||||
base.EnterPhase(next);
|
||||
// 记录进入的架构阶段到历史列表中
|
||||
PhaseHistory.Add(next);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,9 @@
|
||||
using GFramework.Core.Tests.architecture;
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.Tests.architecture;
|
||||
using GFramework.Core.Tests.model;
|
||||
using GFramework.Core.Tests.system;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Legacy;
|
||||
|
||||
namespace GFramework.Core.Tests.tests;
|
||||
|
||||
@ -20,11 +24,122 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
|
||||
protected override AsyncTestArchitecture CreateArchitecture() => new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化架构的异步方法
|
||||
/// 测试架构是否正确初始化所有组件
|
||||
/// </summary>
|
||||
/// <returns>表示异步操作的Task</returns>
|
||||
protected override async Task InitializeArchitecture()
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task Architecture_Should_Initialize_All_Components_Correctly()
|
||||
{
|
||||
await Architecture!.InitializeAsync();
|
||||
|
||||
Assert.That(Architecture.InitCalled, Is.True);
|
||||
Assert.That(Architecture.Runtime, Is.Not.Null);
|
||||
Assert.That(Architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Ready));
|
||||
|
||||
var context = Architecture.Context;
|
||||
|
||||
var model = context.GetModel<AsyncTestModel>();
|
||||
Assert.That(model!.Initialized, Is.True);
|
||||
|
||||
var system = context.GetSystem<AsyncTestSystem>();
|
||||
Assert.That(system!.Initialized, Is.True);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试架构是否按正确顺序进入各个阶段
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task Architecture_Should_Enter_Phases_In_Correct_Order()
|
||||
{
|
||||
await Architecture!.InitializeAsync();
|
||||
|
||||
// 验证架构阶段历史记录是否符合预期顺序
|
||||
CollectionAssert.AreEqual(
|
||||
new[]
|
||||
{
|
||||
ArchitecturePhase.BeforeModelInit,
|
||||
ArchitecturePhase.AfterModelInit,
|
||||
ArchitecturePhase.BeforeSystemInit,
|
||||
ArchitecturePhase.AfterSystemInit,
|
||||
ArchitecturePhase.Ready
|
||||
},
|
||||
Architecture.PhaseHistory
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试在就绪状态后注册模型是否抛出异常
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task RegisterModel_AfterReady_Should_Throw()
|
||||
{
|
||||
await Architecture!.InitializeAsync();
|
||||
|
||||
Assert.Throws<InvalidOperationException>(() => Architecture.RegisterModel(new TestModel())
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试当模型初始化失败时架构是否停止初始化
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task Architecture_Should_Stop_Initialization_When_Model_Init_Fails()
|
||||
{
|
||||
Architecture!.AddPostRegistrationHook(a => { a.RegisterModel(new FailingModel()); });
|
||||
|
||||
await Architecture.InitializeAsync();
|
||||
|
||||
Assert.That(
|
||||
Architecture.CurrentPhase,
|
||||
Is.EqualTo(ArchitecturePhase.FailedInitialization)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试架构销毁是否正确销毁所有系统
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task Architecture_Destroy_Should_Destroy_All_Systems()
|
||||
{
|
||||
await Architecture!.InitializeAsync();
|
||||
Architecture.Destroy();
|
||||
|
||||
var system = Architecture.Context.GetSystem<TestSystem>();
|
||||
Assert.That(system, Is.Null);
|
||||
Assert.That(Architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.Destroyed));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试InitializeAsync方法是否不会阻塞
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task InitializeAsync_Should_Not_Block()
|
||||
{
|
||||
var task = Architecture!.InitializeAsync();
|
||||
|
||||
Assert.That(task.IsCompleted, Is.False);
|
||||
await task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试InitializeAsync方法是否正确处理异常
|
||||
/// </summary>
|
||||
/// <returns>异步任务</returns>
|
||||
[Test]
|
||||
public async Task InitializeAsync_Should_Handle_Exception_Correctly()
|
||||
{
|
||||
Architecture!.AddPostRegistrationHook(a => { a.RegisterModel(new FailingModel()); });
|
||||
|
||||
await Architecture.InitializeAsync();
|
||||
|
||||
Assert.That(
|
||||
Architecture.CurrentPhase,
|
||||
Is.EqualTo(ArchitecturePhase.FailedInitialization)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -253,11 +253,11 @@ public abstract class Architecture(
|
||||
/// 异步初始化方法,返回Task以便调用者可以等待初始化完成
|
||||
/// </summary>
|
||||
/// <returns>表示异步初始化操作的Task</returns>
|
||||
public Task InitializeAsync()
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
return InitializeInternalAsync(asyncMode: true);
|
||||
await InitializeInternalAsync(asyncMode: true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -265,7 +265,6 @@ public abstract class Architecture(
|
||||
EnterPhase(ArchitecturePhase.FailedInitialization);
|
||||
// 发送初始化失败事件
|
||||
TypeEventSystem.Send(new ArchitectureEvents.ArchitectureFailedInitializationEvent());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user