- 新增 IShrinkEventBus、ShrinkEventBusBuilder 与 ShrinkEventBusInstance,支持独立 bus、异常策略、启动开关、类型校验和按 phase 派发 - 新增 SubscribeEvent 可释放订阅句柄与订阅快照接口,保留 EventBus 静态门面并委托到默认实例 - 重构监听列表为父子事件链共享锁与快照缓存,支持父事件监听子事件、跨父子数字优先级归并和防御性快照拷贝 - 优化 EventBase 元数据缓存、懒生成 EventId、派发快照挂接与对象池双重归还保护,移除旧 EventCache 热路径 - 恢复本地 ILPostProcessor 兜底并生成静态订阅注册表,避免共享 CodeGen 覆盖外程序集自动注册失效 - 改进事件查看器、Benchmark、README 与 CHANGELOG,并新增 Dispatch/Priority/Registration/Subscription 测试覆盖核心行为 BREAKING CHANGE: 移除 EventBase.GetListenerList(),int 数字优先级重载不再提供默认值,数字优先级 0 现在映射为 NORMAL。
95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using NUnit.Framework;
|
|
using UnityEngine;
|
|
|
|
namespace ShrinkEventBus.Tests
|
|
{
|
|
[TestFixture]
|
|
public sealed class EventBusSubscriptionTests
|
|
{
|
|
private sealed class SampleEvent : EventBase
|
|
{
|
|
public int Value { get; set; }
|
|
}
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
EventBus.UnregisterAllEvents();
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
EventBus.UnregisterAllEvents();
|
|
}
|
|
|
|
[Test]
|
|
public void SubscribeEvent_Dispose_RemovesOnlyOwnedSubscription()
|
|
{
|
|
var firstHits = 0;
|
|
var secondHits = 0;
|
|
|
|
using var first = EventBus.SubscribeEvent<SampleEvent>(_ => firstHits++);
|
|
using var second = EventBus.SubscribeEvent<SampleEvent>(_ => secondHits++);
|
|
|
|
EventBus.TriggerEvent(new SampleEvent());
|
|
Assert.AreEqual(1, firstHits);
|
|
Assert.AreEqual(1, secondHits);
|
|
|
|
first.Dispose();
|
|
|
|
EventBus.TriggerEvent(new SampleEvent());
|
|
Assert.AreEqual(1, firstHits);
|
|
Assert.AreEqual(2, secondHits);
|
|
}
|
|
|
|
[Test]
|
|
public void ActiveSubscriptionSnapshot_ContainsExpectedMetadata()
|
|
{
|
|
using var subscription = EventBus.SubscribeEvent<SampleEvent>(_ => { }, EventPriority.HIGH, true);
|
|
|
|
var snapshots = EventBus.GetActiveSubscriptionsSnapshot();
|
|
|
|
Assert.AreEqual(1, snapshots.Count);
|
|
Assert.AreEqual(subscription.SubscriptionId, snapshots[0].SubscriptionId);
|
|
Assert.AreEqual(typeof(SampleEvent), snapshots[0].EventType);
|
|
Assert.AreEqual(EventPriority.HIGH, snapshots[0].Priority);
|
|
Assert.IsTrue(snapshots[0].ReceiveCanceled);
|
|
Assert.IsFalse(string.IsNullOrWhiteSpace(snapshots[0].MethodName));
|
|
}
|
|
|
|
[Test]
|
|
public void RegisterEvent_StillWorksWithoutDisposableSubscription()
|
|
{
|
|
var hits = 0;
|
|
|
|
void Handler(SampleEvent evt)
|
|
{
|
|
hits += evt.Value;
|
|
}
|
|
|
|
EventBus.RegisterEvent<SampleEvent>(Handler);
|
|
EventBus.TriggerEvent(new SampleEvent { Value = 3 });
|
|
|
|
Assert.AreEqual(3, hits);
|
|
|
|
EventBus.UnregisterEvent<SampleEvent>(Handler);
|
|
EventBus.TriggerEvent(new SampleEvent { Value = 5 });
|
|
|
|
Assert.AreEqual(3, hits);
|
|
}
|
|
|
|
[Test]
|
|
public void ActiveSubscriptionSnapshot_EmptyAfterDispose()
|
|
{
|
|
var subscription = EventBus.SubscribeEvent<SampleEvent>(_ => Debug.Log("noop"));
|
|
subscription.Dispose();
|
|
|
|
var snapshots = EventBus.GetActiveSubscriptionsSnapshot();
|
|
|
|
Assert.AreEqual(0, snapshots.Count);
|
|
Assert.IsTrue(subscription.IsDisposed);
|
|
}
|
|
}
|
|
}
|