seaweedfs/weed/util/lock_table_test.go

43 lines
824 B
Go
Raw Normal View History

2023-09-22 01:24:34 +08:00
package util
import (
2023-09-22 14:51:32 +08:00
"fmt"
2023-09-22 01:24:34 +08:00
"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
2023-09-22 14:51:32 +08:00
fmt.Printf("ActiveLock %d acquired lock %v\n", lock.ID, lockType)
2023-09-22 01:24:34 +08:00
// Simulate some work
time.Sleep(time.Duration(rand.Int31n(10)*10) * time.Millisecond)
// Release the lock
lt.ReleaseLock(key, lock)
2023-09-22 14:51:32 +08:00
fmt.Printf("ActiveLock %d released lock %v\n", lock.ID, lockType)
2023-09-22 01:24:34 +08:00
}(i)
}
wg.Wait()
}