-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_test.go
96 lines (86 loc) · 2.34 KB
/
lock_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cq
import (
"testing"
"time"
)
func TestMemoryLockerExists(t *testing.T) {
ml := NewMemoryLocker[string]()
if ok := ml.Exists("non-existant"); ok {
t.Error("Exists() = true, want false")
}
}
func TestMemoryLockerGet(t *testing.T) {
ml := NewMemoryLocker[string]()
t.Run("non-existant", func(t *testing.T) {
// Should be not found, thus no real expire time.
lock, ok := ml.Get("non-existant")
if !lock.ExpiresAt.IsZero() || ok {
t.Errorf("Get() = (%v, true), want %v, false", lock.ExpiresAt, time.Time{})
}
})
t.Run("exists", func(t *testing.T) {
// Should allow since none exist yet.
tn := time.Now().Add(1 * time.Minute)
lock := LockValue[string]{
ExpiresAt: tn,
Value: "",
}
if ok := ml.Aquire("exists", lock); !ok {
t.Error("Aquire() = false, want true")
}
// Should be found.
lv, ok := ml.Get("exists")
if lv.ExpiresAt != lock.ExpiresAt || !ok {
t.Errorf("Get() = (%v, false), want (%v, true)", lv, lock)
}
})
}
func TestMemoryLockerAquire(t *testing.T) {
ml := NewMemoryLocker[string]()
lock := LockValue[string]{
ExpiresAt: time.Now().Add(1 * time.Minute),
Value: "",
}
// Should allow since none exist yet.
if ok := ml.Aquire("test", lock); !ok {
t.Error("Aquire() = false, want true")
}
// Should not allow since lock now exists.
if ok := ml.Aquire("test", lock); ok {
t.Error("Aquire() = true, want false")
}
}
func TestMemoryLockerAquireExpired(t *testing.T) {
ml := NewMemoryLocker[string]()
lexpp := LockValue[string]{
ExpiresAt: time.Now().Add(-1 * time.Minute), // Expireation in past.
Value: "",
}
lexpf := LockValue[string]{
ExpiresAt: time.Now().Add(1 * time.Minute), // Expiration in future.
Value: "",
}
// Should allow since none exist yet.
if ok := ml.Aquire("test", lexpp); !ok {
t.Error("Aquire() = false, want true")
}
// Should allow since lock should be expired.
if ok := ml.Aquire("test", lexpf); !ok {
t.Error("Aquire() = false, want true")
}
}
func TestMemoryLockerRelease(t *testing.T) {
ml := NewMemoryLocker[string]()
lock := LockValue[string]{
ExpiresAt: time.Now().Add(1 * time.Minute),
Value: "",
}
// Should allow since none exist yet.
if ok := ml.Aquire("test", lock); !ok {
t.Error("Aquire() = false, want true")
}
// Should allow.
if ok := ml.Release("test"); !ok {
t.Error("Release() = false, want true")
}
}