You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
811 B

1 year ago
1 year ago
  1. package util
  2. import (
  3. "math/rand"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. func TestOrderedLock(t *testing.T) {
  9. lt := NewLockTable[string]()
  10. var wg sync.WaitGroup
  11. // Simulate transactions requesting locks
  12. for i := 1; i <= 50; i++ {
  13. wg.Add(1)
  14. go func(i int) {
  15. defer wg.Done()
  16. key := "resource"
  17. lockType := SharedLock
  18. if i%5 == 0 {
  19. lockType = ExclusiveLock
  20. }
  21. // Simulate attempting to acquire the lock
  22. lock := lt.AcquireLock("", key, lockType)
  23. // Lock acquired, perform some work
  24. glog.V(4).Infof("ActiveLock %d acquired the lock.\n", lock.ID)
  25. // Simulate some work
  26. time.Sleep(time.Duration(rand.Int31n(10)*10) * time.Millisecond)
  27. // Release the lock
  28. lt.ReleaseLock(key, lock)
  29. glog.V(4).Infof("ActiveLock %d released the lock.\n", lock.ID)
  30. }(i)
  31. }
  32. wg.Wait()
  33. }