Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/MockDataStoreService/MockGlobalDataStore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,21 @@ function MockGlobalDataStore:UpdateAsync(key, transformFunction)
error("UpdateAsync rejected with error (request was throttled, but throttled queue was full)", 2)
end

local value = transformFunction(Utils.deepcopy(self.__data[key]))
local thread = coroutine.create(function()
return transformFunction(Utils.deepcopy(self.__data[key]))
end)

local ok, value = coroutine.resume(thread)

if coroutine.status(thread) == "suspended" then
task.spawn(error, "Transfom function error Callbacks cannot yield")
return
end

if not ok then
task.spawn(error, `Transform function error {value}`)
return
end

if value == nil then -- cancel update after remote call
Utils.simulateYield()
Expand Down
37 changes: 37 additions & 0 deletions spec/MockDataStoreService/MockGlobalDataStore.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,43 @@ return function()

end)

it("should not throw error to caller when transform function errors", function()
Test.reset()
Test.setStaticBudgets(100)
local MockGlobalDataStore = Test.Service:GetDataStore("Test")

local ok, value = pcall(function()
return MockGlobalDataStore:UpdateAsync("TestKey1", function()
error("intentional error")
return "new value"
end)
end)

expect(ok).to.equal(true)
expect(value).never.to.be.ok()

end)

it("should not succeed when transform function yields", function()
Test.reset()
Test.setStaticBudgets(100)
local MockGlobalDataStore = Test.Service:GetDataStore("Test")

MockGlobalDataStore:ImportFromJSON({TestKey1 = "value"})

MockGlobalDataStore:UpdateAsync("TestKey1", function()
task.wait()
return "new value"
end)

task.wait()

local exported = HttpService:JSONDecode(MockGlobalDataStore:ExportToJSON())

expect(exported.TestKey1).to.equal("value")

end)

it("should consume budgets correctly", function()
Test.reset()
Test.setStaticBudgets(100)
Expand Down