Files
glacier-ng/tests/test_lock_contention.lua
2026-10-29 09:15:34 -04:00

40 lines
1.4 KiB
Lua

-- test_lock_contention.lua
--
-- Confirms flock() alone is still enough to reject a second concurrent
-- stage transaction for the same uid/scope. This matters because the
-- fix for stale locks (dropping O_EXCL) could easily have accidentally
-- weakened real contention too — this test makes sure it didn't.
local glacier = require("glacier_ffi")
local h = require("test_helpers")
h.reset_glacier_root()
local uid = 1001
io.write("== test_lock_contention (uid " .. uid .. ") ==\n")
glacier.lib.gl_init_user(uid, false)
local ctx1 = glacier.new_context()
local st1 = glacier.lib.gl_init_stage_context(ctx1, glacier.SCOPE_USR, uid)
h.check(tonumber(st1) == 0,
"first stage context acquires the lock: " .. glacier.txn_name(st1))
local ctx2 = glacier.new_context()
local st2 = glacier.lib.gl_init_stage_context(ctx2, glacier.SCOPE_USR, uid)
h.check(tonumber(st2) == 1, -- GL_TXN_ERR_LOCK
"second concurrent stage context is rejected: got " .. glacier.txn_name(st2))
-- ctx2 failed to acquire, so it holds no lock — only abort ctx1.
glacier.lib.gl_abort_transaction(ctx1)
local ctx3 = glacier.new_context()
local st3 = glacier.lib.gl_init_stage_context(ctx3, glacier.SCOPE_USR, uid)
h.check(tonumber(st3) == 0,
"lock is available again after a clean abort: " .. glacier.txn_name(st3))
if tonumber(st3) == 0 then
glacier.lib.gl_abort_transaction(ctx3)
end
h.summary()