Files
seaweedFS/weed/util/lock_table_test.go
Chris Lu 08b79a30f6 Fix lock table shared wait condition (#8707)
* Fix lock table shared wait condition (#8696)

* Refactor lock table waiter check

* Add exclusive lock wait helper test
2026-03-19 16:08:24 -07:00

87 lines
2.1 KiB
Go

package util
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
func TestOrderedLock(t *testing.T) {
lt := NewLockTable[string]()
var wg sync.WaitGroup
// Simulate transactions requesting locks
for i := 1; i <= 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
key := "resource"
lockType := SharedLock
if i%5 == 0 {
lockType = ExclusiveLock
}
// Simulate attempting to acquire the lock
lock := lt.AcquireLock("", key, lockType)
// Lock acquired, perform some work
fmt.Printf("ActiveLock %d acquired lock %v\n", lock.ID, lockType)
// Simulate some work
time.Sleep(time.Duration(rand.Int31n(10)*10) * time.Millisecond)
// Release the lock
lt.ReleaseLock(key, lock)
fmt.Printf("ActiveLock %d released lock %v\n", lock.ID, lockType)
}(i)
}
wg.Wait()
}
func TestShouldWaitForSharedLock(t *testing.T) {
lock := &ActiveLock{ID: 2, lockType: SharedLock}
entry := &LockEntry{
waiters: []*ActiveLock{{ID: 1}, lock},
}
if !shouldWaitForSharedLock(lock, entry) {
t.Fatal("shared lock should wait behind earlier waiters")
}
entry.waiters = []*ActiveLock{lock}
entry.activeExclusiveLockOwnerCount = 1
if !shouldWaitForSharedLock(lock, entry) {
t.Fatal("shared lock should wait while an exclusive owner is active")
}
lock.isDeleted = true
if shouldWaitForSharedLock(lock, entry) {
t.Fatal("deleted shared lock should stop waiting even if an exclusive owner is active")
}
}
func TestShouldWaitForExclusiveLock(t *testing.T) {
lock := &ActiveLock{ID: 2, lockType: ExclusiveLock}
entry := &LockEntry{
waiters: []*ActiveLock{{ID: 1}, lock},
}
if !shouldWaitForExclusiveLock(lock, entry) {
t.Fatal("exclusive lock should wait behind earlier waiters")
}
entry.waiters = []*ActiveLock{lock}
entry.activeSharedLockOwnerCount = 1
if !shouldWaitForExclusiveLock(lock, entry) {
t.Fatal("exclusive lock should wait while shared owners are active")
}
lock.isDeleted = true
if shouldWaitForExclusiveLock(lock, entry) {
t.Fatal("deleted exclusive lock should stop waiting even if shared owners are active")
}
}