-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_test.go
176 lines (131 loc) · 3.52 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mogul
import (
"testing"
"time"
"fmt"
"context"
"math/rand"
"sync"
"sync/atomic"
"github.com/pascaldekloe/goe/verify"
)
func TestSingleNode(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
l := m.NewMutex("Key", "routine1")
feed := []struct {
duration time.Duration
success bool
lock *Mutex
}{
0: {
time.Hour * 1,
true,
l,
},
1: {
time.Millisecond * 1000,
false,
m.NewMutex("Key", "routine2"),
},
2: {
time.Millisecond * 1000,
true,
l,
},
}
for i, v := range feed {
got, _ := v.lock.TryLock(v.duration)
verify.Values(t, fmt.Sprintf("%d : Outcome of lock was not ok", i), got, v.success)
defer v.lock.Unlock()
}
}
func TestMultipleRoutines(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
var wg sync.WaitGroup
hits := 0
for i := 1; i <= 10; i++ {
wg.Add(1)
user := fmt.Sprintf("User#%d", i)
go func() {
time.Sleep(time.Microsecond * time.Duration(rand.Float32()*10000))
l := m.NewMutex("Multiple", user)
if got, _ := l.TryLock(time.Hour); got {
hits++
}
wg.Done()
}()
}
wg.Wait()
verify.Values(t, "Should get single value", hits, 1)
}
func TestManyLocks(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
var wg sync.WaitGroup
var hits int32
for i := 1; i <= 100; i++ {
wg.Add(1)
id := fmt.Sprintf("Multiple%d", i)
go func() {
time.Sleep(time.Microsecond * time.Duration(rand.Float32()*10000))
l := m.NewMutex(id, "host")
if got, err := l.TryLock(time.Hour); got {
defer l.Unlock()
atomic.AddInt32(&hits, 1)
} else if err != nil {
t.Errorf("Fail %s", err)
}
wg.Done()
}()
}
wg.Wait()
verify.Values(t, "Should get one value per iteration", hits, int32(100))
}
func TestExtendTime(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
l := m.NewMutex("Reclaim after deadline", "host")
l.TryLock(time.Minute)
l.TryLock(time.Minute * 5)
if l.doc.ExpiresAtUtc.Sub(time.Now().UTC()) < time.Minute {
t.Error("Deadline should be extended")
}
l.Unlock()
}
func TestMutex_IsExpired(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
l := m.NewMutex("Key", "routine1")
l.TryLock(time.Millisecond * 100)
verify.Values(t, "Not expired at start", l.IsExpired(), false)
time.Sleep(time.Millisecond * 100)
verify.Values(t, "Not expired at start", l.IsExpired(), true)
}
func TestWorkWithLockTwice(t *testing.T) {
session := initDB(t)
defer clearDB(t, session)
var m MutexCreator = New(session.DB(database).C(collection), session.DB(database).C("tasks"))
l := m.NewMutex("Reclaim after deadline", "host")
next := m.NewMutex("Reclaim after deadline", "second host")
timeFrame := time.Millisecond * 100
if got, _ := l.TryLock(timeFrame); got {
deadline := time.Now().Add(timeFrame)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
select {
case <-ctx.Done():
break
}
got, _ := next.TryLock(timeFrame)
verify.Values(t, "Failed to get lock after expiration", got, true)
} else {
t.Errorf("Fail")
}
}