Files
UnrealLuaDemo/Script/lua/Tests/Extra/LifecycleAndMemoryTest.lua
T

93 lines
3.8 KiB
Lua

local ____exports = {}
local ____TestRunner = require("Tests.Harness.TestRunner")
local Suite = ____TestRunner.Suite
local Test = ____TestRunner.Test
local AssertEqual = ____TestRunner.AssertEqual
local AssertTrue = ____TestRunner.AssertTrue
local AssertFalse = ____TestRunner.AssertFalse
local AssertNil = ____TestRunner.AssertNil
local AssertArrayEqual = ____TestRunner.AssertArrayEqual
local ____MockUnLuaRuntime = require("Tests.Harness.MockUnLuaRuntime")
local AdvanceMockTime = ____MockUnLuaRuntime.AdvanceMockTime
local CreateBoundInstance = ____MockUnLuaRuntime.CreateBoundInstance
local GetMockState = ____MockUnLuaRuntime.GetMockState
local ResetMockState = ____MockUnLuaRuntime.ResetMockState
local EventBus = require("Framework.EventBus")
local LifecycleSafetyActor = require("Tests.Extra.LifecycleSafetyActor")
function ____exports.RunLifecycleAndMemoryTests()
Suite(
"Extra.LifecycleAndMemorySafety",
function()
Test(
"multiple @unlua instances maintain isolated state (no shared class-table arrays)",
function()
ResetMockState()
EventBus.Clear()
local cls = LifecycleSafetyActor
local a = CreateBoundInstance(cls, "AActor")
local b = CreateBoundInstance(cls, "AActor")
a:ReceiveBeginPlay()
b:ReceiveBeginPlay()
a:PushItem("only_in_a")
b:PushItem("only_in_b")
AssertArrayEqual(
a:GetItems(),
{"only_in_a"}
)
AssertArrayEqual(
b:GetItems(),
{"only_in_b"}
)
a:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
b:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
EventBus.Clear()
end
)
Test(
"paired cleanup on ReceiveEndPlay prevents leaked EventBus, Delegate, Timer & manual Ref",
function()
ResetMockState()
EventBus.Clear()
local actor = CreateBoundInstance(LifecycleSafetyActor, "AActor")
local asset = UE.UObject.Load("/Game/Data/DA_Config.DA_Config")
local weak = UE.FWeakObjectPtr(actor)
actor:ReceiveBeginPlay()
actor:HoldManualRef(asset)
AssertEqual(
GetMockState().manualRefs[asset],
1
)
EventBus.Emit("Lifecycle.Broadcast", "msg1")
actor.OnTakeAnyDamage:Broadcast(
actor,
25,
nil,
nil,
nil
)
AdvanceMockTime(1)
AssertEqual(
actor:GetReceivedEvents(),
3
)
AssertTrue(weak:IsValid())
actor:ReceiveEndPlay(UE.EEndPlayReason.Destroyed)
actor:K2_DestroyActor()
AssertFalse(actor.OnTakeAnyDamage:IsBound())
AssertFalse(weak:IsValid())
AssertNil(weak:Get())
AssertNil(GetMockState().manualRefs[asset])
EventBus.Emit("Lifecycle.Broadcast", "msg2")
AdvanceMockTime(3)
AssertEqual(
actor:GetReceivedEvents(),
3
)
EventBus.Clear()
end
)
end
)
end
return ____exports