aboutsummaryrefslogtreecommitdiff
path: root/weed/util/lock_table_test.go
diff options
context:
space:
mode:
authorchrislu <chris.lu@gmail.com>2023-09-22 21:54:08 -0700
committerchrislu <chris.lu@gmail.com>2023-09-22 21:54:08 -0700
commit900abd44c158ea9ff3e0ca3eddabff9f9c3f1bf6 (patch)
tree9cc17ea8305abda04023e2ba32421e37ebe408a3 /weed/util/lock_table_test.go
parentdf027fdddf7e71c31c8828ed598dc9510f15fa08 (diff)
parent9fc79bebf97be1c52b824fea03a431320ca097c1 (diff)
downloadseaweedfs-900abd44c158ea9ff3e0ca3eddabff9f9c3f1bf6.tar.xz
seaweedfs-900abd44c158ea9ff3e0ca3eddabff9f9c3f1bf6.zip
Merge branch 'track-mount-e2e' of https://github.com/seaweedfs/seaweedfs into track-mount-e2e
Diffstat (limited to 'weed/util/lock_table_test.go')
-rw-r--r--weed/util/lock_table_test.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/weed/util/lock_table_test.go b/weed/util/lock_table_test.go
new file mode 100644
index 000000000..001fa0bdf
--- /dev/null
+++ b/weed/util/lock_table_test.go
@@ -0,0 +1,42 @@
+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()
+}