-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractMongoDistributedLockTest.scala
208 lines (192 loc) · 8.47 KB
/
AbstractMongoDistributedLockTest.scala
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2019 WeirdDev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.weirddev.scalalock.mongo
import com.weirddev.scalalock.api.DistributedLock
import org.specs2.matcher.MatchResult
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.{Duration, MILLISECONDS, _}
import scala.concurrent.{Await, Future}
/**
* Date: 3/27/2019
* @author Yaron Yamin
*
*/
abstract class AbstractMongoDistributedLockTest extends Specification with Mockito
{
private val LockExpirationDurationMillis = 1000
private val ResultWaitTime = 3.seconds
private val TaskName = "test_task"
protected val mongoDistLock:DistributedLock
"MongoDistributedLock accepting non-future returning task" should {
val TaskExecMsgPrefix = "running synchronized task... no."
"return computation result when lock is open" in {
Thread.sleep( LockExpirationDurationMillis )
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 1")
"I'm done"
}
Await.result(result, ResultWaitTime) === Some("I'm done")
}
"return None when locked" in {
Thread.sleep(LockExpirationDurationMillis)
val result = mongoDistLock.acquire(TaskName + "2B", Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 2")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
val result2 = mongoDistLock.acquire(TaskName+ "2B", Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 3")
"I'm can't get into the lock"
}
Await.result(result, ResultWaitTime) === Some("I'm done")
Await.result(result2, ResultWaitTime) === None
}
"return result when lock expired" in {
Thread.sleep(LockExpirationDurationMillis + 100)
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS )){
println(TaskExecMsgPrefix + " 4")
Thread.sleep(LockExpirationDurationMillis * 2)
"I'm done"
}
Thread.sleep(LockExpirationDurationMillis + 50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 5")
"opened it again"
}
Await.result(result, ResultWaitTime) === Some("I'm done")
Await.result(result2, ResultWaitTime) === Some("opened it again")
}
"return result when task is done" in {
Thread.sleep(LockExpirationDurationMillis )
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 6")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
Thread.sleep(LockExpirationDurationMillis/2 +50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 7")
"opened it again"
}
Await.result(result, ResultWaitTime) === Some("I'm done")
Await.result(result2, ResultWaitTime) === Some("opened it again")
}
"keep lock is releaseLockWhenDone is set to false" in {
Thread.sleep(LockExpirationDurationMillis )
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS),releaseLockWhenDone = false){
println(TaskExecMsgPrefix + " 6B")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
Thread.sleep(LockExpirationDurationMillis/2 +50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 7B")
"should not be able to open"
}
Await.result(result, ResultWaitTime) === Some("I'm done")
Await.result(result2, ResultWaitTime) === None
}
}
"MongoDistributedLock accepting a future returning task" should {
val TaskExecMsgPrefix = "running a future synchronized task ... no."
"return computation result when lock is open" in {
Thread.sleep( LockExpirationDurationMillis )
val result: Future[Option[Future[String]]] = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
Future {
println(TaskExecMsgPrefix + " 1")
Thread.sleep(400)
"I'm done"
}
}
assertFutureResult(result, "I'm done")
}
"return None and dont run future task when locked" in {
Thread.sleep(LockExpirationDurationMillis)
val result = mongoDistLock.acquire(TaskName + 2, Duration(LockExpirationDurationMillis,MILLISECONDS)){
Future {
println(TaskExecMsgPrefix + " 2")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
}
val result2 = mongoDistLock.acquire(TaskName + 2, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 3")
"I'm can't get into the lock"
}
assertFutureResult(result, "I'm done")
Await.result(result2, ResultWaitTime) === None
}
"return result when lock expired" in {
Thread.sleep(LockExpirationDurationMillis + 100)
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS )){
Future {
println(TaskExecMsgPrefix + " 4")
Thread.sleep(LockExpirationDurationMillis * 2)
"I'm done"
}
}
Thread.sleep(LockExpirationDurationMillis + 50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS)){
println(TaskExecMsgPrefix + " 5")
"opened it again"
}
assertFutureResult(result, "I'm done")
Await.result(result2, ResultWaitTime) === Some("opened it again")
}
"return result when task is done" in {
Thread.sleep(LockExpirationDurationMillis )
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS),releaseLockWhenDone = true,Some("task returning a future")){
Future{
println(TaskExecMsgPrefix + " 6")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
}
Thread.sleep(LockExpirationDurationMillis/2 +50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS),releaseLockWhenDone = true,Some("a 2nd task returning a future")){
println(TaskExecMsgPrefix + " 7")
"opened it again"
}
assertFutureResult(result, "I'm done")
Await.result(result2, ResultWaitTime) === Some("opened it again")
}
"keep lock is releaseLockWhenDone is set to false" in {
Thread.sleep(LockExpirationDurationMillis )
val result = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS),releaseLockWhenDone = false,Some("task returning a future")){
Future{
println(TaskExecMsgPrefix + " 8")
Thread.sleep(LockExpirationDurationMillis/2)
"I'm done"
}
}
Thread.sleep(LockExpirationDurationMillis/2 +50)
val result2 = mongoDistLock.acquire(TaskName, Duration(LockExpirationDurationMillis,MILLISECONDS),releaseLockWhenDone = true,Some("a 2nd task returning a future")){
println(TaskExecMsgPrefix + " 7")
"should not be able to open"
}
assertFutureResult(result, "I'm done")
Await.result(result2, ResultWaitTime) === None
}
}
private def assertFutureResult(result: Future[Option[Future[String]]], expectedResult: String): MatchResult[String] = {
val maybeEventualString = Await.result(result, ResultWaitTime)
maybeEventualString.getClass === classOf[Some[Future[String]]]
Await.result(maybeEventualString.get, ResultWaitTime) === expectedResult
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme