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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
* Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.drools.core.lock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.onap.policy.drools.core.lock.TestUtils.expectException;
import java.util.LinkedList;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Test;
public class SimpleLockManagerTest {
private static final String NULL_RESOURCE_ID = "null resourceId";
private static final String NULL_OWNER = "null owner";
private static final String RESOURCE_A = "resource.a";
private static final String RESOURCE_B = "resource.b";
private static final String RESOURCE_C = "resource.c";
private static final String OWNER1 = "owner.one";
private static final String OWNER2 = "owner.two";
private static final String OWNER3 = "owner.three";
private Future<Boolean> fut;
private SimpleLockManager mgr;
@Before
public void setUp() {
mgr = new SimpleLockManager();
}
@Test
public void testLock() throws Exception {
fut = mgr.lock(RESOURCE_A, OWNER1, null);
assertTrue(fut.isDone());
assertTrue(fut.get());
assertTrue(mgr.isLocked(RESOURCE_A));
assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1));
assertFalse(mgr.isLocked(RESOURCE_B));
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER2));
// null callback - not locked yet
fut = mgr.lock(RESOURCE_C, OWNER3, null);
assertTrue(fut.isDone());
assertTrue(fut.get());
// null callback - already locked
fut = mgr.lock(RESOURCE_A, OWNER3, null);
assertTrue(fut.isDone());
assertFalse(fut.get());
}
@Test
public void testLock_AlreadyLocked() throws Exception {
mgr.lock(RESOURCE_A, OWNER1, null);
fut = mgr.lock(RESOURCE_A, OWNER2, null);
assertTrue(fut.isDone());
assertFalse(fut.get());
}
@Test(expected = IllegalStateException.class)
public void testLock_SameOwner() throws Exception {
mgr.lock(RESOURCE_A, OWNER1, null);
// should throw an exception
mgr.lock(RESOURCE_A, OWNER1, null);
}
@Test
public void testLock_ArgEx() {
IllegalArgumentException ex =
expectException(IllegalArgumentException.class, xxx -> mgr.lock(null, OWNER1, null));
assertEquals(NULL_RESOURCE_ID, ex.getMessage());
ex = expectException(IllegalArgumentException.class, xxx -> mgr.lock(RESOURCE_A, null, null));
assertEquals(NULL_OWNER, ex.getMessage());
// this should not throw an exception
mgr.lock(RESOURCE_A, OWNER1, null);
}
@Test
public void testUnlock() throws Exception {
mgr.lock(RESOURCE_A, OWNER1, null);
// unlock it
assertTrue(mgr.unlock(RESOURCE_A, OWNER1));
}
@Test
public void testUnlock_ArgEx() {
IllegalArgumentException ex = expectException(IllegalArgumentException.class, xxx -> mgr.unlock(null, OWNER1));
assertEquals(NULL_RESOURCE_ID, ex.getMessage());
ex = expectException(IllegalArgumentException.class, xxx -> mgr.unlock(RESOURCE_A, null));
assertEquals(NULL_OWNER, ex.getMessage());
}
@Test
public void testUnlock_NotLocked() {
assertFalse(mgr.unlock(RESOURCE_A, OWNER1));
}
@Test
public void testIsLocked() {
assertFalse(mgr.isLocked(RESOURCE_A));
mgr.lock(RESOURCE_A, OWNER1, null);
mgr.lock(RESOURCE_B, OWNER1, null);
assertTrue(mgr.isLocked(RESOURCE_A));
assertTrue(mgr.isLocked(RESOURCE_B));
assertFalse(mgr.isLocked(RESOURCE_C));
// unlock from first resource
mgr.unlock(RESOURCE_A, OWNER1);
assertFalse(mgr.isLocked(RESOURCE_A));
assertTrue(mgr.isLocked(RESOURCE_B));
assertFalse(mgr.isLocked(RESOURCE_C));
// unlock from second resource
mgr.unlock(RESOURCE_B, OWNER1);
assertFalse(mgr.isLocked(RESOURCE_A));
assertFalse(mgr.isLocked(RESOURCE_B));
assertFalse(mgr.isLocked(RESOURCE_C));
}
@Test
public void testIsLocked_ArgEx() {
IllegalArgumentException ex = expectException(IllegalArgumentException.class, xxx -> mgr.isLocked(null));
assertEquals(NULL_RESOURCE_ID, ex.getMessage());
}
@Test
public void testIsLockedBy() {
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER1));
mgr.lock(RESOURCE_A, OWNER1, null);
assertFalse(mgr.isLockedBy(RESOURCE_B, OWNER1));
assertTrue(mgr.isLockedBy(RESOURCE_A, OWNER1));
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER2));
// unlock from the resource
mgr.unlock(RESOURCE_A, OWNER1);
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER1));
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER2));
assertFalse(mgr.isLockedBy(RESOURCE_B, OWNER1));
}
@Test
public void testIsLockedBy_ArgEx() {
IllegalArgumentException ex =
expectException(IllegalArgumentException.class, xxx -> mgr.isLockedBy(null, OWNER1));
assertEquals(NULL_RESOURCE_ID, ex.getMessage());
ex = expectException(IllegalArgumentException.class, xxx -> mgr.isLockedBy(RESOURCE_A, null));
assertEquals(NULL_OWNER, ex.getMessage());
}
@Test
public void testIsLockedBy_NotLocked() {
mgr.lock(RESOURCE_A, OWNER1, null);
// different resource, thus no lock
assertFalse(mgr.isLockedBy(RESOURCE_B, OWNER1));
}
@Test
public void testIsLockedBy_LockedButNotOwner() {
mgr.lock(RESOURCE_A, OWNER1, null);
// different owner
assertFalse(mgr.isLockedBy(RESOURCE_A, OWNER2));
}
@Test
public void testMultiThreaded() throws InterruptedException {
int nthreads = 10;
int nlocks = 100;
LinkedList<Thread> threads = new LinkedList<>();
String[] resources = {RESOURCE_A, RESOURCE_B};
AtomicInteger nfail = new AtomicInteger(0);
CountDownLatch stopper = new CountDownLatch(1);
CountDownLatch completed = new CountDownLatch(nthreads);
for (int x = 0; x < nthreads; ++x) {
String owner = "owner." + x;
Thread t = new Thread() {
@Override
public void run() {
for (int y = 0; y < nlocks; ++y) {
String res = resources[y % resources.length];
try {
// some locks will be acquired, some denied
mgr.lock(res, owner, null).get();
// do some "work"
stopper.await(1L, TimeUnit.MILLISECONDS);
mgr.unlock(res, owner);
} catch (CancellationException | ExecutionException e) {
nfail.incrementAndGet();
} catch (InterruptedException expected) {
Thread.currentThread().interrupt();
break;
}
}
completed.countDown();
}
};
t.setDaemon(true);
threads.add(t);
}
// start the threads
for (Thread t : threads) {
t.start();
}
// wait for them to complete
completed.await(5000L, TimeUnit.SECONDS);
// stop the threads from sleeping
stopper.countDown();
completed.await(1L, TimeUnit.SECONDS);
// interrupt those that are still alive
for (Thread t : threads) {
if (t.isAlive()) {
t.interrupt();
}
}
assertEquals(0, nfail.get());
}
}
|