-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclusterlibtimer.cc
207 lines (187 loc) · 5.93 KB
/
clusterlibtimer.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
/*
* 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-timer-app";
/*
* Helper class that counts how many times a timer fired.
*/
class MyTimerEventHandler
: public TimerEventHandler
{
public:
/*
* Constructor.
*/
MyTimerEventHandler() : counter(0) {}
/*
* Utility method to reset the counter.
*/
void resetCounter()
{
Locker l(&counterLock);
counter = 0;
}
/*
* Utility method to retrieve the counter.
*/
int32_t getCounter()
{
Locker l(&counterLock);
return counter;
}
/*
* Actually handle the timer event, just increment
* our counter.
*/
virtual void handleTimerEvent(TimerId id, ClientData data)
{
Locker l(&counterLock);
++counter;
}
private:
/**
* Makes counter thread-safe.
*/
Mutex counterLock;
/*
* Count how many times a timer event was fired.
*/
int32_t counter;
};
/*
* The test class itself.
*/
class ClusterlibTimer
: public MPITestFixture
{
CPPUNIT_TEST_SUITE(ClusterlibTimer);
CPPUNIT_TEST(testTimer1);
CPPUNIT_TEST(testTimer2);
CPPUNIT_TEST(testTimer3);
CPPUNIT_TEST_SUITE_END();
public:
ClusterlibTimer()
: MPITestFixture(globalTestParams),
_factory(NULL),
_client0(NULL),
_zk(NULL),
_timer0(NULL)
{
}
/* Runs prior to each test */
virtual void setUp()
{
_factory =
new Factory(globalTestParams.getZkServerPortList());
MPI_CPPUNIT_ASSERT(_factory != NULL);
_zk = _factory->getRepository();
MPI_CPPUNIT_ASSERT(_zk != NULL);
_client0 = _factory->createClient();
MPI_CPPUNIT_ASSERT(_client0 != NULL);
_app0 = _client0->getRoot()->getApplication(
appName, CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_app0 != NULL);
_grp0 = _app0->getGroup("bar-group", CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_grp0 != NULL);
_nod0 = _grp0->getNode("nod3", CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_nod0 != NULL);
_dist0 = _grp0->getDataDistribution("dist1", CREATE_IF_NOT_FOUND);
MPI_CPPUNIT_ASSERT(_dist0 != NULL);
_timer0 = new MyTimerEventHandler();
MPI_CPPUNIT_ASSERT(_timer0 != NULL);
}
/* Runs after each test */
virtual void tearDown()
{
cleanAndBarrierMPITest(_factory, true);
/*
* Delete only the factory, that automatically deletes
* all the other objects.
*/
delete _factory;
_factory = NULL;
_client0 = NULL;
_zk = NULL;
delete _timer0;
_timer0 = NULL;
}
void testTimer1()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testTimer1");
MPI_CPPUNIT_ASSERT(_timer0->getCounter() == 0);
TimerId id1 = _client0->registerTimer(_timer0, 5000, (ClientData) NULL);
bool cancelled = _client0->cancelTimer(id1);
MPI_CPPUNIT_ASSERT(cancelled == true);
cancelled = _client0->cancelTimer(id1);
MPI_CPPUNIT_ASSERT(cancelled == false);
cerr << "counter=" << _timer0->getCounter() << endl;
MPI_CPPUNIT_ASSERT(_timer0->getCounter() == 0);
}
void testTimer2()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testTimer2");
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
sleep(1);
MPI_CPPUNIT_ASSERT(_timer0->getCounter() == 1);
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
sleep(1);
cerr << "counter=" << _timer0->getCounter() << endl;
MPI_CPPUNIT_ASSERT(_timer0->getCounter() == 4);
}
void testTimer3()
{
initializeAndBarrierMPITest(-1,
true,
_factory,
true,
"testTimer3");
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
TimerId id2 =
_client0->registerTimer(_timer0, 5000, (ClientData) NULL);
(void) _client0->registerTimer(_timer0, 200, (ClientData) NULL);
bool cancelled = _client0->cancelTimer(id2);
MPI_CPPUNIT_ASSERT(cancelled == true);
sleep(1);
cerr << "counter=" << _timer0->getCounter() << endl;
MPI_CPPUNIT_ASSERT(_timer0->getCounter() == 2);
}
private:
Factory *_factory;
Client *_client0;
shared_ptr<Application> _app0;
shared_ptr<Group> _grp0;
shared_ptr<Node> _nod0;
shared_ptr<DataDistribution> _dist0;
zk::ZooKeeperAdapter *_zk;
MyTimerEventHandler *_timer0;
};
/* Registers the fixture into the 'registry' */
CPPUNIT_TEST_SUITE_REGISTRATION(ClusterlibTimer);