This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package dataext
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMutexSet_BasicLockUnlock(t *testing.T) {
|
||||
ms := NewMutexSet[string]()
|
||||
ms.Lock("a")
|
||||
ms.Unlock("a")
|
||||
ms.RLock("b")
|
||||
ms.RUnlock("b")
|
||||
}
|
||||
|
||||
func TestMutexSet_DifferentKeysIndependent(t *testing.T) {
|
||||
ms := NewMutexSet[int]()
|
||||
ms.Lock(1)
|
||||
ms.Lock(2)
|
||||
ms.Unlock(1)
|
||||
ms.Unlock(2)
|
||||
}
|
||||
|
||||
func TestMutexSet_SameKeyMutuallyExclusive(t *testing.T) {
|
||||
ms := NewMutexSet[string]()
|
||||
var counter int64
|
||||
const n = 50
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ms.Lock("shared")
|
||||
atomic.AddInt64(&counter, 1)
|
||||
ms.Unlock("shared")
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if atomic.LoadInt64(&counter) != n {
|
||||
t.Fatalf("got %d want %d", counter, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMutexSet_RLockMultiple(t *testing.T) {
|
||||
ms := NewMutexSet[string]()
|
||||
ms.RLock("k")
|
||||
ms.RLock("k")
|
||||
ms.RUnlock("k")
|
||||
ms.RUnlock("k")
|
||||
}
|
||||
Reference in New Issue
Block a user