-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclusterliblock.cc
504 lines (466 loc) · 15.8 KB
/
clusterliblock.cc
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright (c) 2010 Yahoo! Inc. 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. See accompanying
* LICENSE file.
*
* $Id$
*/
#include "clusterlib.h"
#include "testparams.h"
#include "MPITestFixture.h"
extern TestParams globalTestParams;
using namespace std;
using namespace boost;
using namespace clusterlib;
const string appName = "unittests-lock-app";
const string groupName = "lock-group";
/**
* Helper function for thread lock testing in testLock28.
*/
void *testLock28ThreadFunc(void *groupP)
{
shared_ptr<Group> group = *(reinterpret_cast<shared_ptr<Group> *>(groupP));
group->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
sleep(1);
group->releaseLock(CLString::NOTIFYABLE_LOCK);
return NULL;
}
/**
* Helper function for thread lock testing in testLock29.
*/
void *testLock29ThreadFunc(void *groupP)
{
shared_ptr<Group> group = *(reinterpret_cast<shared_ptr<Group> *>(groupP));
group->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
group->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
sleep(1);
group->releaseLock(CLString::NOTIFYABLE_LOCK);
group->releaseLock(CLString::NOTIFYABLE_LOCK);
return NULL;
}
class ClusterlibLock : public MPITestFixture
{
CPPUNIT_TEST_SUITE(ClusterlibLock);
CPPUNIT_TEST(testLock1);
CPPUNIT_TEST(testLock2);
CPPUNIT_TEST(testLock3);
CPPUNIT_TEST(testLock4);
CPPUNIT_TEST(testLock5);
CPPUNIT_TEST(testLock6);
CPPUNIT_TEST(testLock20);
CPPUNIT_TEST(testLock21);
CPPUNIT_TEST(testLock22);
CPPUNIT_TEST(testLock23);
CPPUNIT_TEST(testLock24);
CPPUNIT_TEST(testLock25);
CPPUNIT_TEST(testLock26);
CPPUNIT_TEST(testLock27);
CPPUNIT_TEST(testLock28);
CPPUNIT_TEST(testLock29);
CPPUNIT_TEST_SUITE_END();
public:
ClusterlibLock()
: MPITestFixture(globalTestParams),
_factory(NULL) {}
/**
* Runs prior to each test
*/
virtual void setUp()
{
_factory = new Factory(
globalTestParams.getZkServerPortList());
MPI_CPPUNIT_ASSERT(_factory != NULL);
_client0 = _factory->createClient();
MPI_CPPUNIT_ASSERT(_client0 != NULL);
_app0 = _client0->getRoot()->getApplication(appName,
CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_app0 != NULL);
_group0 = _app0->getGroup(groupName, CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_group0 != NULL);
}
/**
* Runs after each test
*/
virtual void tearDown()
{
cleanAndBarrierMPITest(_factory, true);
delete _factory;
_factory = NULL;
}
/**
* Simple test for a process to lock/unlock the group
*/
void testLock1()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock1");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(
CLString::NOTIFYABLE_LOCK);
}
}
/**
* Test a process locking/unlocking the group 3x.
*/
void testLock2()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock2");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
}
/**
* Test a process locking/unlocking the group with purposeful
* double release.
*/
void testLock3()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock3");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
try {
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
MPI_CPPUNIT_ASSERT("SHOULD HAVE THROWN EXCEPTION" == 0);
} catch (const InvalidMethodException &e) {
}
}
}
/**
* Test a process locking/unlocking the group with purposeful
* double release.
*/
void testLock4()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock4");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
try {
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
MPI_CPPUNIT_ASSERT("SHOULD HAVE THROWN EXCEPTION" == 0);
} catch (const InvalidArgumentsException &e) {
}
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
}
/**
* Test a process getting a DIST_LOCK_SHARED lock.
*/
void testLock5()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock5");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
}
/**
* Test a process getting a DIST_LOCK_SHARED lock with children.
*/
void testLock6()
{
initializeAndBarrierMPITest(1,
true,
_factory,
true,
"testLock6");
if (isMyRank(0)) {
MPI_CPPUNIT_ASSERT(_app0);
_app0->acquireLock(
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_SHARED,
true);
NameList nl = _app0->getLockBids(
CLString::NOTIFYABLE_LOCK, true);
NameList::const_iterator nlIt;
for (nlIt = nl.begin(); nlIt != nl.end(); ++nlIt) {
cerr << "testLock6: Lock bid = " << *nlIt << endl;
}
MPI_CPPUNIT_ASSERT(nl.size() >= 2);
_app0->releaseLock(CLString::NOTIFYABLE_LOCK,
true);
}
}
/**
* Test many processes to lock/unlock the group
*/
void testLock20()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock20");
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
/**
* Test many processes to lock/unlock the group
*/
void testLock21()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock21");
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
/**
* Test many processes where one has the lock then the others try
* and fail to get it.
*/
void testLock22()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock22");
MPI_CPPUNIT_ASSERT(_group0);
if (isMyRank(0)) {
NotifyableLocker l(_group0,
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_EXCL);
barrier(_factory, true);
barrier(_factory, true);
}
else {
barrier(_factory, true);
MPI_CPPUNIT_ASSERT(_group0->acquireLockWaitMsecs(
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_EXCL,
10LL) == false);
barrier(_factory, true);
}
}
/**
* Test many processes all try to get the lock, but only 1 and
* exactly one gets it.
*/
void testLock23()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock23");
MPI_CPPUNIT_ASSERT(_group0);
bool gotLock = _group0->acquireLockWaitMsecs(
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_EXCL,
0LL);
if (gotLock) {
cerr << getRank() << ": I got the lock" << endl;
barrier(_factory, true);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
else {
MPI_CPPUNIT_ASSERT(_group0->acquireLockWaitMsecs(
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_EXCL,
0LL) == false);
barrier(_factory, true);
}
}
/**
* Test many processes to lock/unlock the group with the
* NotifyableLocker.
*/
void testLock24()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock24");
MPI_CPPUNIT_ASSERT(_group0);
NotifyableLocker l(_group0,
CLString::NOTIFYABLE_LOCK,
DIST_LOCK_EXCL);
MPI_CPPUNIT_ASSERT(l.hasLock() == true);
}
/**
* Test all processes to getting a DIST_LOCK_SHARED, verifying they
* simultaneously got access and then releasing.
*/
void testLock25()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock25");
MPI_CPPUNIT_ASSERT(_group0);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
barrier(_factory, true);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
/**
* DIST_LOCK_SHARED then DIST_LOCK_EXCL
*/
void testLock26()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock26");
MPI_CPPUNIT_ASSERT(_group0);
if (isMyRank(0)) {
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
barrier(_factory, true);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
else {
barrier(_factory, true);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
}
/**
* DIST_LOCK_EXCL then DIST_LOCK_SHARED.
*/
void testLock27()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock27");
MPI_CPPUNIT_ASSERT(_group0);
if (isMyRank(0)) {
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_EXCL);
barrier(_factory, true);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
barrier(_factory, true);
}
else {
barrier(_factory, true);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
barrier(_factory, true);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
}
}
/**
* Acquire a shared lock, and then another with another 2 threads on
* the same process. When they release the locks, there should be
* no problem.
*/
void testLock28()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock28");
MPI_CPPUNIT_ASSERT(_group0);
Thread groupLockThread1;
Thread groupLockThread2;
groupLockThread1.Create(&_group0, &testLock28ThreadFunc);
groupLockThread2.Create(&_group0, &testLock28ThreadFunc);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
sleep(1);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
groupLockThread1.Join();
groupLockThread2.Join();
}
/**
* Same as testLock28, but add one more level of complexity by
* using the refCount as well.
*/
void testLock29()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testLock29");
MPI_CPPUNIT_ASSERT(_group0);
Thread groupLockThread1;
Thread groupLockThread2;
groupLockThread1.Create(&_group0, &testLock29ThreadFunc);
groupLockThread2.Create(&_group0, &testLock29ThreadFunc);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
_group0->acquireLock(
CLString::NOTIFYABLE_LOCK, DIST_LOCK_SHARED);
sleep(1);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
_group0->releaseLock(CLString::NOTIFYABLE_LOCK);
groupLockThread1.Join();
groupLockThread2.Join();
}
private:
Factory *_factory;
Client *_client0;
shared_ptr<Application> _app0;
shared_ptr<Group> _group0;
};
/* Registers the fixture into the 'registry' */
CPPUNIT_TEST_SUITE_REGISTRATION(ClusterlibLock);