1 | /*
|
---|
2 | * Project: JobMarket
|
---|
3 | * Description: asynchronous Server/Controller/Client-approach to parallel computing, based on boost::asio
|
---|
4 | * Copyright (C) 2011 Frederik Heber. All rights reserved.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * FragmentQueueUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 23, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <cppunit/CompilerOutputter.h>
|
---|
21 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
22 | #include <cppunit/ui/text/TestRunner.h>
|
---|
23 |
|
---|
24 | #include "FragmentQueueUnitTest.hpp"
|
---|
25 |
|
---|
26 | #include <limits>
|
---|
27 | #include <vector>
|
---|
28 |
|
---|
29 | #include "CodePatterns/Assert.hpp"
|
---|
30 | #include "JobMarket/FragmentQueue.hpp"
|
---|
31 |
|
---|
32 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
33 |
|
---|
34 | #ifdef HAVE_TESTRUNNER
|
---|
35 | #include "JobMarket/UnitTestMain.hpp"
|
---|
36 | #endif /*HAVE_TESTRUNNER*/
|
---|
37 |
|
---|
38 | /********************************************** Test classes **************************************/
|
---|
39 |
|
---|
40 | #include "stubs/FragmentJobStub.hpp"
|
---|
41 | #include "stubs/ObserverStub.hpp"
|
---|
42 |
|
---|
43 | // Registers the fixture into the 'registry'
|
---|
44 | CPPUNIT_TEST_SUITE_REGISTRATION( FragmentQueueTest );
|
---|
45 |
|
---|
46 |
|
---|
47 | void FragmentQueueTest::setUp()
|
---|
48 | {
|
---|
49 | // Throw assertions
|
---|
50 | ASSERT_DO(Assert::Throw);
|
---|
51 |
|
---|
52 | queue = new FragmentQueue();
|
---|
53 | addobserver = new NotificationObserver(queue->getChannel(FragmentQueue::JobAdded));
|
---|
54 |
|
---|
55 | // and sign on
|
---|
56 | queue->signOn(addobserver, FragmentQueue::JobAdded);
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | void FragmentQueueTest::tearDown()
|
---|
61 | {
|
---|
62 | queue->signOff(addobserver, FragmentQueue::JobAdded);
|
---|
63 |
|
---|
64 | delete queue;
|
---|
65 | delete addobserver;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /** UnitTest for working JobQueue
|
---|
69 | */
|
---|
70 | void FragmentQueueTest::JobTest()
|
---|
71 | {
|
---|
72 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
73 | /// check for illegal id
|
---|
74 | #ifndef NDEBUG
|
---|
75 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
76 | CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure );
|
---|
77 | #endif
|
---|
78 | // set to valid id
|
---|
79 | testJob->setId(1);
|
---|
80 |
|
---|
81 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
---|
82 |
|
---|
83 | #ifndef NDEBUG
|
---|
84 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
85 | #else
|
---|
86 | queue->pushJob(testJob);
|
---|
87 | #endif
|
---|
88 | CPPUNIT_ASSERT( addobserver->wasNotified );
|
---|
89 | addobserver->wasNotified = false;
|
---|
90 |
|
---|
91 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
92 | CPPUNIT_ASSERT_EQUAL(true, queue->isJobPresent() );
|
---|
93 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
94 | {
|
---|
95 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
96 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
97 | CPPUNIT_ASSERT( queue->getNoResult() == iter->second );
|
---|
98 | }
|
---|
99 |
|
---|
100 | // push same id again
|
---|
101 | #ifndef NDEBUG
|
---|
102 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
103 | CPPUNIT_ASSERT_THROW( queue->pushJob(testJob), Assert::AssertionFailure );
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
107 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
108 |
|
---|
109 | FragmentJob::ptr poppedJob;
|
---|
110 | #ifndef NDEBUG
|
---|
111 | CPPUNIT_ASSERT_NO_THROW( poppedJob = queue->popJob() );
|
---|
112 | #else
|
---|
113 | poppedJob = queue->popJob();
|
---|
114 | #endif
|
---|
115 | CPPUNIT_ASSERT( !addobserver->wasNotified );
|
---|
116 | CPPUNIT_ASSERT( poppedJob == testJob );
|
---|
117 |
|
---|
118 | CPPUNIT_ASSERT_EQUAL((size_t)0, queue->jobs.size());
|
---|
119 | CPPUNIT_ASSERT_EQUAL(false, queue->isJobPresent() );
|
---|
120 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
121 | {
|
---|
122 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
123 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
124 | CPPUNIT_ASSERT( queue->getNoResultQueued() == iter->second );
|
---|
125 | }
|
---|
126 |
|
---|
127 | #ifndef NDEBUG
|
---|
128 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
129 | CPPUNIT_ASSERT_THROW( queue->popJob(), Assert::AssertionFailure );
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** UnitTest for adding multiple jobs at a time.
|
---|
134 | */
|
---|
135 | void FragmentQueueTest::JobsTest()
|
---|
136 | {
|
---|
137 | // prepare some jobs
|
---|
138 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
139 | testJob->setId((JobId_t)1);
|
---|
140 | FragmentJob::ptr anothertestJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
141 | anothertestJob->setId((JobId_t)2);
|
---|
142 |
|
---|
143 | // prepare a vector of them
|
---|
144 | std::vector<FragmentJob::ptr> testjobs;
|
---|
145 | testjobs.push_back(testJob);
|
---|
146 | testjobs.push_back(anothertestJob);
|
---|
147 |
|
---|
148 | // prepare another vector of them
|
---|
149 | std::vector<FragmentJob::ptr> sametestjobs;
|
---|
150 | sametestjobs.push_back(testJob);
|
---|
151 | sametestjobs.push_back(anothertestJob);
|
---|
152 |
|
---|
153 | // push the vector
|
---|
154 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->jobs.size() );
|
---|
155 | #ifndef NDEBUG
|
---|
156 | CPPUNIT_ASSERT_NO_THROW( queue->pushJobs(testjobs) );
|
---|
157 | #else
|
---|
158 | queue->pushJobs(testjobs);
|
---|
159 | #endif
|
---|
160 | CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() );
|
---|
161 | CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size());
|
---|
162 | {
|
---|
163 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)1);
|
---|
164 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
165 | CPPUNIT_ASSERT( queue->getNoResult() == iter->second );
|
---|
166 | }
|
---|
167 | {
|
---|
168 | FragmentQueue::ResultMap::const_iterator iter = queue->results.find((JobId_t)2);
|
---|
169 | CPPUNIT_ASSERT( iter != queue->results.end() );
|
---|
170 | CPPUNIT_ASSERT( queue->getNoResult() == iter->second );
|
---|
171 | }
|
---|
172 | // push again
|
---|
173 | #ifndef NDEBUG
|
---|
174 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
175 | CPPUNIT_ASSERT_THROW( queue->pushJobs(testjobs), Assert::AssertionFailure );
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | CPPUNIT_ASSERT_EQUAL( (size_t)2, queue->jobs.size() );
|
---|
179 | CPPUNIT_ASSERT_EQUAL((size_t)2, queue->results.size());
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** UnitTest for adding multiple jobs with different priorities.
|
---|
183 | */
|
---|
184 | void FragmentQueueTest::PriorityTest()
|
---|
185 | {
|
---|
186 | // prepare some jobs
|
---|
187 | const size_t MAXJOBS = 10;
|
---|
188 | std::vector<FragmentJob::ptr> testjobs;
|
---|
189 | for(size_t i=1; i<= MAXJOBS; ++i) {
|
---|
190 | FragmentJob::ptr testJob(new FragmentJobStub((JobId_t)i));
|
---|
191 | FragmentJob::priority_t priority = rand() % 10;
|
---|
192 | testJob->setPriority(priority);
|
---|
193 | testjobs.push_back(testJob);
|
---|
194 | }
|
---|
195 |
|
---|
196 | // push the vector
|
---|
197 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->jobs.size() );
|
---|
198 | #ifndef NDEBUG
|
---|
199 | CPPUNIT_ASSERT_NO_THROW( queue->pushJobs(testjobs) );
|
---|
200 | #else
|
---|
201 | queue->pushJobs(testjobs);
|
---|
202 | #endif
|
---|
203 | CPPUNIT_ASSERT_EQUAL( MAXJOBS, queue->jobs.size() );
|
---|
204 | CPPUNIT_ASSERT_EQUAL( MAXJOBS, queue->results.size());
|
---|
205 |
|
---|
206 | // pop and check their ids
|
---|
207 | FragmentJob::priority_t old_priority =
|
---|
208 | std::numeric_limits<FragmentJob::priority_t>::max();
|
---|
209 | while(queue->isJobPresent()) {
|
---|
210 | FragmentJob::ptr testjob = queue->popJob();
|
---|
211 | const FragmentJob::priority_t &new_priority =
|
---|
212 | testjob->priority;
|
---|
213 | CPPUNIT_ASSERT( old_priority >= new_priority);
|
---|
214 | old_priority = new_priority;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /** UnitTest for working ResultMap
|
---|
220 | */
|
---|
221 | void FragmentQueueTest::ResultsTest()
|
---|
222 | {
|
---|
223 | // prepare a job
|
---|
224 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
225 | testJob->setId(1);
|
---|
226 | #ifndef NDEBUG
|
---|
227 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
228 | #else
|
---|
229 | queue->pushJob(testJob);
|
---|
230 | #endif
|
---|
231 |
|
---|
232 | // check for present job to do
|
---|
233 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getPresentJobs() );
|
---|
234 |
|
---|
235 | #ifndef NDEBUG
|
---|
236 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
237 | #else
|
---|
238 | queue->popJob();
|
---|
239 | #endif
|
---|
240 |
|
---|
241 | // check for present job to do
|
---|
242 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
243 |
|
---|
244 | // prepare a result
|
---|
245 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
246 | FragmentResult::ptr wrongIdResult( new FragmentResult(2) );
|
---|
247 |
|
---|
248 | // check that none are present and we can't get result yet
|
---|
249 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
250 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
251 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
252 | #ifndef NDEBUG
|
---|
253 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
254 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
255 | #endif
|
---|
256 |
|
---|
257 | /// check for admonishing wrong id
|
---|
258 | #ifndef NDEBUG
|
---|
259 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
260 | CPPUNIT_ASSERT_THROW( queue->pushResult(wrongIdResult), Assert::AssertionFailure );
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | // push correct result
|
---|
264 | #ifndef NDEBUG
|
---|
265 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
266 | #else
|
---|
267 | queue->pushResult(testResult);
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | // check presence again
|
---|
271 | CPPUNIT_ASSERT( queue->isResultPresent(1) );
|
---|
272 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
---|
273 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
274 |
|
---|
275 | // obtain result again
|
---|
276 | #ifndef NDEBUG
|
---|
277 | CPPUNIT_ASSERT_NO_THROW( queue->getResult(1) );
|
---|
278 | #else
|
---|
279 | queue->getResult(1);
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | // check presence one more time
|
---|
283 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
284 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getPresentJobs() );
|
---|
285 | CPPUNIT_ASSERT( !queue->isResultPresent(1) );
|
---|
286 | CPPUNIT_ASSERT( !queue->isResultPresent(2) );
|
---|
287 | #ifndef NDEBUG
|
---|
288 | std::cout << "The following assertion is intended and does not indicate a failure." << std::endl;
|
---|
289 | CPPUNIT_ASSERT_THROW( queue->getResult(1), Assert::AssertionFailure );
|
---|
290 | #endif
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** UnitTest for working ResultMap
|
---|
294 | */
|
---|
295 | void FragmentQueueTest::AllResultsTest()
|
---|
296 | {
|
---|
297 | // prepare a job
|
---|
298 | FragmentJob::ptr testJob( new FragmentJobStub(JobId::IllegalJob) );
|
---|
299 | testJob->setId((JobId_t)1);
|
---|
300 | FragmentJob::ptr anothertestJob( new FragmentJobStub(JobId::IllegalJob) );
|
---|
301 | anothertestJob->setId((JobId_t)2);
|
---|
302 |
|
---|
303 | #ifndef NDEBUG
|
---|
304 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
305 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(anothertestJob) );
|
---|
306 | #else
|
---|
307 | queue->pushJob(testJob);
|
---|
308 | queue->pushJob(anothertestJob);
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | // check that no results are returned.
|
---|
312 | {
|
---|
313 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
314 | CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() );
|
---|
315 | }
|
---|
316 |
|
---|
317 | // pop both as if some work was being done
|
---|
318 | #ifndef NDEBUG
|
---|
319 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
320 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
321 | #else
|
---|
322 | queue->popJob();
|
---|
323 | queue->popJob();
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | // prepare a result
|
---|
327 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
328 | FragmentResult::ptr anothertestResult( new FragmentResult(2) );
|
---|
329 |
|
---|
330 | // push correct result
|
---|
331 | #ifndef NDEBUG
|
---|
332 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
333 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(anothertestResult) );
|
---|
334 | #else
|
---|
335 | queue->pushResult(testResult);
|
---|
336 | queue->pushResult(anothertestResult);
|
---|
337 | #endif
|
---|
338 |
|
---|
339 | // check that two results are returned.
|
---|
340 | {
|
---|
341 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
342 | CPPUNIT_ASSERT_EQUAL( (size_t)2, results.size() );
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Unit test for resubmitJob().
|
---|
347 | *
|
---|
348 | */
|
---|
349 | void FragmentQueueTest::removeWaitingResultsTest()
|
---|
350 | {
|
---|
351 | // prepare a job
|
---|
352 | FragmentJob::ptr testJob(new FragmentJobStub(JobId::IllegalJob));
|
---|
353 | testJob->setId(1);
|
---|
354 | #ifndef NDEBUG
|
---|
355 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
356 | #else
|
---|
357 | queue->pushJob(testJob);
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->results.size() );
|
---|
361 | // now remove all waiting results although there are none
|
---|
362 | queue->removeWaitingResults();
|
---|
363 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->results.size() );
|
---|
364 |
|
---|
365 | // pop job as if we do some work
|
---|
366 | queue->popJob();
|
---|
367 |
|
---|
368 | // prepare a result for the job
|
---|
369 | FragmentResult::ptr testResult( new FragmentResult(1) );
|
---|
370 | // push correct result
|
---|
371 | #ifndef NDEBUG
|
---|
372 | CPPUNIT_ASSERT_NO_THROW( queue->pushResult(testResult) );
|
---|
373 | #else
|
---|
374 | queue->pushResult(testResult);
|
---|
375 | #endif
|
---|
376 |
|
---|
377 | // check that one job is done
|
---|
378 | CPPUNIT_ASSERT_EQUAL( (size_t)1, queue->getDoneJobs() );
|
---|
379 |
|
---|
380 | // now remove all waiting results
|
---|
381 | queue->removeWaitingResults();
|
---|
382 |
|
---|
383 | // check that no results are returned.
|
---|
384 | {
|
---|
385 | CPPUNIT_ASSERT_EQUAL( (size_t)0, queue->getDoneJobs() );
|
---|
386 | const std::vector<FragmentResult::ptr> results = queue->getAllResults();
|
---|
387 | CPPUNIT_ASSERT_EQUAL( (size_t)0, results.size() );
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | /** Unit test for resubmitJob().
|
---|
392 | *
|
---|
393 | */
|
---|
394 | void FragmentQueueTest::resubmitTest()
|
---|
395 | {
|
---|
396 | FragmentJob::ptr testJob(new FragmentJobStub(1));
|
---|
397 |
|
---|
398 | // push a Job into queue
|
---|
399 | #ifndef NDEBUG
|
---|
400 | CPPUNIT_ASSERT_NO_THROW( queue->pushJob(testJob) );
|
---|
401 | #else
|
---|
402 | queue->pushJob(testJob);
|
---|
403 | #endif
|
---|
404 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
405 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
406 |
|
---|
407 | // pop the job
|
---|
408 | // pop both as if some work was being done
|
---|
409 | #ifndef NDEBUG
|
---|
410 | CPPUNIT_ASSERT_NO_THROW( queue->popJob() );
|
---|
411 | #else
|
---|
412 | queue->popJob();
|
---|
413 | #endif
|
---|
414 | CPPUNIT_ASSERT_EQUAL((size_t)0, queue->jobs.size());
|
---|
415 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
416 |
|
---|
417 | // resubmit
|
---|
418 | #ifndef NDEBUG
|
---|
419 | CPPUNIT_ASSERT_NO_THROW( queue->resubmitJob((JobId_t)1) );
|
---|
420 | #else
|
---|
421 | queue->resubmitJob((JobId_t)1);
|
---|
422 | #endif
|
---|
423 |
|
---|
424 | // check whethers it's present again
|
---|
425 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->jobs.size());
|
---|
426 | CPPUNIT_ASSERT_EQUAL((size_t)1, queue->results.size());
|
---|
427 | }
|
---|
428 |
|
---|