From d513ab16c09d2b7bdb13d495fd2e057a78391990 Mon Sep 17 00:00:00 2001 From: Joe Date: Sun, 1 Feb 2026 22:10:18 -0600 Subject: [PATCH] Refactor stress test files for improved readability and functionality - Simplified the reify stress test by consolidating the reifyInstance call into a single line for better clarity. - Added a cleanup function in the stressPatterns test to manage knownIds, ensuring that IDs of destroyed instances are removed. - Enhanced the logic for adding, removing, and updating instances in the stressPatterns test to verify the existence of parent and child instances before operations, improving test robustness. - Reformatted code in LargeTreeGenerator and MockApiContext for consistency and readability. --- plugin/src/Reconciler/reify.stress.spec.lua | 9 +--- plugin/src/chaos/stressPatterns.spec.lua | 52 ++++++++++++++------- plugin/src/testUtils/LargeTreeGenerator.lua | 3 +- plugin/src/testUtils/MockApiContext.lua | 15 +++--- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/plugin/src/Reconciler/reify.stress.spec.lua b/plugin/src/Reconciler/reify.stress.spec.lua index 82fb4db5..2cd592b7 100644 --- a/plugin/src/Reconciler/reify.stress.spec.lua +++ b/plugin/src/Reconciler/reify.stress.spec.lua @@ -527,13 +527,8 @@ return function() local startTime = os.clock() local deferredRefs = {} - local unappliedPatch = reifyInstance( - deferredRefs, - instanceMap, - treeData.virtualInstances, - treeData.rootId, - container - ) + local unappliedPatch = + reifyInstance(deferredRefs, instanceMap, treeData.virtualInstances, treeData.rootId, container) applyDeferredRefs(instanceMap, deferredRefs, unappliedPatch) local elapsed = os.clock() - startTime diff --git a/plugin/src/chaos/stressPatterns.spec.lua b/plugin/src/chaos/stressPatterns.spec.lua index eae5177c..a1d99a25 100644 --- a/plugin/src/chaos/stressPatterns.spec.lua +++ b/plugin/src/chaos/stressPatterns.spec.lua @@ -266,19 +266,34 @@ return function() local knownIds = { rootId } + -- Helper to clean up knownIds by removing IDs that no longer exist in instanceMap. + -- This is necessary because when a parent is removed, its children are also + -- destroyed by Roblox, but their IDs may still be in knownIds. + local function cleanupKnownIds() + local validIds = {} + for _, id in ipairs(knownIds) do + if instanceMap.fromIds[id] ~= nil then + table.insert(validIds, id) + end + end + knownIds = validIds + end + for _ = 1, 100 do local operation = math.random(1, 3) if operation == 1 and #knownIds < 500 then - -- Add + -- Add: verify parent still exists (may have been destroyed as descendant) local parentId = knownIds[math.random(1, #knownIds)] - local patch = PatchGenerator.createAdditionsPatch({ - count = 1, - parentId = parentId, - }) - applyPatch(instanceMap, patch) - for id in pairs(patch.added) do - table.insert(knownIds, id) + if instanceMap.fromIds[parentId] ~= nil then + local patch = PatchGenerator.createAdditionsPatch({ + count = 1, + parentId = parentId, + }) + applyPatch(instanceMap, patch) + for id in pairs(patch.added) do + table.insert(knownIds, id) + end end elseif operation == 2 and #knownIds > 1 then -- Remove (not root) @@ -288,16 +303,21 @@ return function() local patch = PatchGenerator.createRemovalsPatch({ ids = { idToRemove } }) applyPatch(instanceMap, patch) + + -- Clean up knownIds to remove any descendants that were destroyed + cleanupKnownIds() elseif operation == 3 and #knownIds > 0 then - -- Update + -- Update: verify instance still exists local idToUpdate = knownIds[math.random(1, #knownIds)] - local patch = PatchSet.newEmpty() - table.insert(patch.updated, { - id = idToUpdate, - changedName = "Updated_" .. tostring(os.clock()), - changedProperties = {}, - }) - applyPatch(instanceMap, patch) + if instanceMap.fromIds[idToUpdate] ~= nil then + local patch = PatchSet.newEmpty() + table.insert(patch.updated, { + id = idToUpdate, + changedName = "Updated_" .. tostring(os.clock()), + changedProperties = {}, + }) + applyPatch(instanceMap, patch) + end end end diff --git a/plugin/src/testUtils/LargeTreeGenerator.lua b/plugin/src/testUtils/LargeTreeGenerator.lua index c87192ae..8b38aaff 100644 --- a/plugin/src/testUtils/LargeTreeGenerator.lua +++ b/plugin/src/testUtils/LargeTreeGenerator.lua @@ -208,7 +208,8 @@ function LargeTreeGenerator.createVirtualTree(options) if withDuplicates and currentDepth <= 1 then for _ = 1, duplicateCount do local duplicateName = prefix .. "_1" -- Same as first child - local duplicateId = createVirtualInstance(duplicateName, id, currentDepth + 1, duplicateName .. "_dup") + local duplicateId = + createVirtualInstance(duplicateName, id, currentDepth + 1, duplicateName .. "_dup") table.insert(children, duplicateId) end end diff --git a/plugin/src/testUtils/MockApiContext.lua b/plugin/src/testUtils/MockApiContext.lua index e7d0688e..a0d70f44 100644 --- a/plugin/src/testUtils/MockApiContext.lua +++ b/plugin/src/testUtils/MockApiContext.lua @@ -108,13 +108,14 @@ function MockApiContext:connect() end -- Default connect response - return true, { - serverVersion = "0.0.0-mock", - protocolVersion = 4, - sessionId = HttpService:GenerateGUID(false), - expectedPlaceIds = {}, - rootInstanceId = HttpService:GenerateGUID(false), - } + return true, + { + serverVersion = "0.0.0-mock", + protocolVersion = 4, + sessionId = HttpService:GenerateGUID(false), + expectedPlaceIds = {}, + rootInstanceId = HttpService:GenerateGUID(false), + } end --[[