-
Notifications
You must be signed in to change notification settings - Fork 1
/
lockable_test.go
53 lines (45 loc) · 925 Bytes
/
lockable_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package lockable_test
import (
"github.com/MysteriousPotato/go-lockable"
"sync"
"testing"
)
func TestIsLocked(t *testing.T) {
lock := lockable.New[string]()
lock.RLockKey("potato")
if !lock.IsLocked("potato") {
t.Fatal("expected isLocked true, got false")
}
lock.RUnlockKey("potato")
if lock.IsLocked("potato") {
t.Fatal("expected isLocked false, got true")
}
}
func BenchmarkLockableLock(b *testing.B) {
l := lockable.New[string]()
for i := 0; i < b.N; i++ {
l.LockKey("key")
l.UnlockKey("key")
}
}
func BenchmarkLockableRLock(b *testing.B) {
l := lockable.New[string]()
for i := 0; i < b.N; i++ {
l.RLockKey("key")
l.RUnlockKey("key")
}
}
func BenchmarkStdMutexLock(b *testing.B) {
l := &sync.RWMutex{}
for i := 0; i < b.N; i++ {
l.Lock()
l.Unlock()
}
}
func BenchmarkStdMutexRLock(b *testing.B) {
mu := &sync.RWMutex{}
for i := 0; i < b.N; i++ {
mu.Lock()
mu.Unlock()
}
}