[12747] | 1 | #include "gtest/gtest.h"
|
---|
| 2 | #include "ariba/utility/system/SystemQueue.h"
|
---|
| 3 |
|
---|
[12750] | 4 | #include <unistd.h> // usleep
|
---|
[12756] | 5 | #include <boost/date_time.hpp> // local_time()
|
---|
[12750] | 6 |
|
---|
[12749] | 7 | #include <ostream>
|
---|
| 8 |
|
---|
[12747] | 9 | using namespace ::testing;
|
---|
| 10 | using namespace ariba::utility;
|
---|
[12749] | 11 | using namespace std;
|
---|
[12747] | 12 |
|
---|
[12756] | 13 | using boost::posix_time::microsec_clock;
|
---|
| 14 |
|
---|
| 15 |
|
---|
[12748] | 16 | /**
|
---|
| 17 | * Tests if the SystemQueue is initialized empty and not running.
|
---|
| 18 | */
|
---|
| 19 | TEST(SystemQueue, Instantiation)
|
---|
[12747] | 20 | {
|
---|
| 21 | SystemQueue& sysq = SystemQueue::instance();
|
---|
[12748] | 22 |
|
---|
[12749] | 23 | // cout << &sysq << endl;
|
---|
| 24 |
|
---|
[12748] | 25 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 26 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * Tests whether calling the SystemQueue::instance() always returns the same object.
|
---|
| 32 | *
|
---|
| 33 | * NOTE: This is an easy case, since this is the same compile unit..
|
---|
| 34 | * But can't hurt to test it anyway.
|
---|
| 35 | */
|
---|
| 36 | TEST(SystemQueue, Singleton)
|
---|
| 37 | {
|
---|
| 38 | SystemQueue& sysq = SystemQueue::instance();
|
---|
[12747] | 39 | SystemQueue& sysq2 = SystemQueue::instance();
|
---|
[12748] | 40 |
|
---|
| 41 | // cout << &sysq << endl;
|
---|
[12749] | 42 |
|
---|
| 43 | EXPECT_TRUE( &sysq == &sysq2 );
|
---|
[12747] | 44 | }
|
---|
[12748] | 45 |
|
---|
| 46 |
|
---|
[12749] | 47 | /**
|
---|
| 48 | * Start and stop the SystemQueue
|
---|
| 49 | */
|
---|
| 50 | TEST(SystemQueue, StartStop)
|
---|
| 51 | {
|
---|
| 52 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 53 |
|
---|
| 54 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 55 |
|
---|
| 56 | // start
|
---|
| 57 | sysq.run();
|
---|
| 58 |
|
---|
| 59 | EXPECT_TRUE( sysq.isRunning() );
|
---|
| 60 |
|
---|
| 61 | // stop
|
---|
| 62 | sysq.cancel();
|
---|
| 63 |
|
---|
[12761] | 64 | // XXX weg, wenn das join klappt!!
|
---|
| 65 | usleep(100);
|
---|
| 66 |
|
---|
[12749] | 67 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Test fixture
|
---|
| 73 | *
|
---|
| 74 | * class that can be called by the SystemQueue
|
---|
| 75 | */
|
---|
| 76 | // To use a test fixture, derive a class from testing::Test.
|
---|
| 77 | class SystemQueueTest :
|
---|
| 78 | public testing::Test
|
---|
| 79 | {
|
---|
| 80 | public:
|
---|
| 81 |
|
---|
[12751] | 82 | // sleep time when waiting for the system queue (max total / each step)
|
---|
| 83 | #define MAX_WAIT 100 // microseconds
|
---|
| 84 | #define SLEEP_TIME 10 // microseconds
|
---|
| 85 |
|
---|
[12749] | 86 | SystemQueueTest() :
|
---|
[12751] | 87 | checkmark(false),
|
---|
| 88 | last_ordered_call(0)
|
---|
[12749] | 89 | {
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | void Check()
|
---|
| 93 | {
|
---|
[12761] | 94 | cout << "### Check ### "<< endl;
|
---|
| 95 |
|
---|
[12749] | 96 | checkmark = true;
|
---|
| 97 | }
|
---|
[12763] | 98 |
|
---|
| 99 | void Cancel()
|
---|
| 100 | {
|
---|
[12764] | 101 | ASSERT_THROW(SystemQueue::instance().cancel(), std::logic_error);
|
---|
| 102 |
|
---|
[12763] | 103 | checkmark = true;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[12765] | 106 | void Leave()
|
---|
| 107 | {
|
---|
| 108 | // let's do something before leaving..
|
---|
| 109 | this->LongRunner();
|
---|
| 110 | checkmark = false;
|
---|
| 111 |
|
---|
| 112 | cout << "### Leaving SysQ ### "<< endl;
|
---|
| 113 | SystemQueue::instance().leave();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[12751] | 116 | void LongRunner()
|
---|
| 117 | {
|
---|
| 118 | usleep( MAX_WAIT / 2 );
|
---|
| 119 |
|
---|
| 120 | checkmark = true;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | /// wait for the checkmark to be set by a SystemQueue call, (but not too long!)
|
---|
| 125 | void wait_for_checkmark(int max_wait = MAX_WAIT)
|
---|
| 126 | {
|
---|
| 127 | for ( int i = 0; i < max_wait / SLEEP_TIME; i++)
|
---|
| 128 | {
|
---|
| 129 | if ( checkmark )
|
---|
| 130 | break;
|
---|
| 131 |
|
---|
[12761] | 132 | cout << "### sleeping for " << SLEEP_TIME << " microseconds ..." << endl;
|
---|
[12751] | 133 | usleep(SLEEP_TIME);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[12749] | 136 |
|
---|
[12751] | 137 | /// call that checks wheather it was performed in order
|
---|
| 138 | void OrderedCall(int num)
|
---|
| 139 | {
|
---|
[12754] | 140 | // XXX
|
---|
| 141 | cout << "### OrderedCall num: " << num << endl;
|
---|
| 142 |
|
---|
[12751] | 143 | // check ordering
|
---|
| 144 | EXPECT_EQ( num, last_ordered_call + 1);
|
---|
| 145 |
|
---|
| 146 | last_ordered_call = num;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /// like OrderedCall, but calls itself to test nested calls
|
---|
| 150 | void NestedOrderedCall(int from, int to)
|
---|
| 151 | {
|
---|
| 152 | // check ordering
|
---|
| 153 | OrderedCall(from);
|
---|
| 154 |
|
---|
| 155 | // nested call
|
---|
| 156 | if ( from < to )
|
---|
| 157 | {
|
---|
| 158 | SystemQueue::instance().scheduleCall(
|
---|
| 159 | boost::bind(&SystemQueueTest::NestedOrderedCall,
|
---|
| 160 | this,
|
---|
| 161 | from+1,
|
---|
| 162 | to)
|
---|
| 163 | );
|
---|
| 164 | }
|
---|
| 165 | else
|
---|
| 166 | {
|
---|
| 167 | /// XXX because the current/old SystemQueue does not pass the Threading test,
|
---|
| 168 | /// we have to signal, that when all nested calls are finished,
|
---|
| 169 | /// so we need to set the checkmark here..
|
---|
| 170 |
|
---|
| 171 | checkmark = true;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[12749] | 175 | bool checkmark;
|
---|
[12751] | 176 | int last_ordered_call;
|
---|
[12749] | 177 | };
|
---|
| 178 |
|
---|
[12751] | 179 |
|
---|
| 180 | /**
|
---|
[12763] | 181 | * Enqueues an event but then cancels the SystemQueue without running
|
---|
| 182 | */
|
---|
| 183 | TEST_F(SystemQueueTest, EmptyAfterCancel)
|
---|
| 184 | {
|
---|
| 185 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 186 |
|
---|
| 187 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
| 188 |
|
---|
| 189 | // enqueue event
|
---|
| 190 | sysq.scheduleCall(
|
---|
| 191 | boost::bind(&SystemQueueTest::Check, this)
|
---|
| 192 | );
|
---|
| 193 |
|
---|
| 194 | EXPECT_FALSE( sysq.isEmpty() );
|
---|
| 195 |
|
---|
| 196 | // cancel
|
---|
| 197 | sysq.cancel();
|
---|
| 198 |
|
---|
| 199 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /**
|
---|
| 204 | * cancels the SystemQueue from inside a scheduled event
|
---|
[12764] | 205 | *
|
---|
| 206 | * --> and expects that this throws an exception
|
---|
| 207 | *
|
---|
| 208 | * NOTE: due to the SystemQueue singleton design, explicit cleanup is necessary!
|
---|
[12763] | 209 | */
|
---|
| 210 | TEST_F(SystemQueueTest, CancelFromInsideEvent)
|
---|
| 211 | {
|
---|
| 212 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 213 | checkmark = false; // just to be sure..
|
---|
| 214 |
|
---|
| 215 | // start
|
---|
| 216 | sysq.run();
|
---|
| 217 |
|
---|
| 218 | // scheduleCall
|
---|
| 219 | sysq.scheduleCall(
|
---|
| 220 | boost::bind(&SystemQueueTest::Cancel, this)
|
---|
| 221 | );
|
---|
| 222 |
|
---|
| 223 | // wait for the event to happen
|
---|
| 224 | wait_for_checkmark(MAX_WAIT);
|
---|
| 225 |
|
---|
[12764] | 226 | // CLEANUP
|
---|
| 227 | sysq.cancel();
|
---|
| 228 |
|
---|
| 229 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 230 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
[12763] | 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
[12765] | 235 | * leaves the SystemQueue from inside a scheduled event (in contrast to cancel)
|
---|
| 236 | *
|
---|
| 237 | * NOTE: due to the SystemQueue singleton design, explicit cleanup is necessary!
|
---|
| 238 | *
|
---|
| 239 | * [ NOTE: the old System Queue does not support this. ]
|
---|
| 240 | */
|
---|
| 241 | TEST_F(SystemQueueTest, LeaveFromInsideEvent)
|
---|
| 242 | {
|
---|
| 243 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 244 |
|
---|
| 245 | // start
|
---|
| 246 | sysq.run();
|
---|
| 247 |
|
---|
| 248 | // scheduleCall
|
---|
| 249 | sysq.scheduleCall(
|
---|
| 250 | boost::bind(&SystemQueueTest::Leave, this)
|
---|
| 251 | );
|
---|
| 252 |
|
---|
| 253 | // schedule another which must NOT be performed
|
---|
| 254 | sysq.scheduleCall(
|
---|
| 255 | boost::bind(&SystemQueueTest::Check, this)
|
---|
| 256 | );
|
---|
| 257 |
|
---|
| 258 | // sleep until the SystemQueue thread has finished
|
---|
| 259 | sysq.join();
|
---|
| 260 |
|
---|
| 261 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 262 |
|
---|
| 263 | // ensure Check() was not perfomed
|
---|
| 264 | EXPECT_FALSE( sysq.isEmpty() );
|
---|
| 265 | EXPECT_FALSE( checkmark );
|
---|
| 266 |
|
---|
| 267 | // CLEANUP
|
---|
| 268 | sysq.cancel();
|
---|
| 269 |
|
---|
| 270 | EXPECT_FALSE( sysq.isRunning() );
|
---|
| 271 | EXPECT_TRUE( sysq.isEmpty() );
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
[12751] | 276 | * schedule a call and test whether it is actually performed by the SystemQueue
|
---|
| 277 | */
|
---|
[12749] | 278 | TEST_F(SystemQueueTest, ScheduleCall)
|
---|
| 279 | {
|
---|
| 280 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 281 | checkmark = false; // just to be sure..
|
---|
| 282 |
|
---|
| 283 | // start
|
---|
| 284 | sysq.run();
|
---|
| 285 |
|
---|
| 286 | // scheduleCall
|
---|
| 287 | sysq.scheduleCall(
|
---|
| 288 | boost::bind(&SystemQueueTest::Check, this)
|
---|
| 289 | );
|
---|
[12751] | 290 |
|
---|
| 291 | // wait for the event to happen
|
---|
| 292 | wait_for_checkmark(MAX_WAIT);
|
---|
[12749] | 293 |
|
---|
[12751] | 294 | // stop
|
---|
| 295 | sysq.cancel();
|
---|
| 296 |
|
---|
| 297 | EXPECT_TRUE( checkmark ) << "Function was not called within " << MAX_WAIT << " microseconds.";
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
| 301 | /**
|
---|
| 302 | * This test actually tests two things [sorry, but it's hard to test them separately!]
|
---|
| 303 | *
|
---|
| 304 | * - first: the SystemQueue should not consider itself empty, while an event is processed
|
---|
| 305 | * - second: SystemQueue events should be processed in parallel to the main thread
|
---|
| 306 | *
|
---|
| 307 | * NOTE: The timings here are not obvious, maybe they have to be adjusted on very slow machines
|
---|
| 308 | *
|
---|
| 309 | * NOTE: !! The current/old SystemQueue does NOT pass this test!!
|
---|
| 310 | *
|
---|
| 311 | * That's also why we need the unhandy wait_for_checkmark function,
|
---|
| 312 | * instead a wait_until_empty function.
|
---|
| 313 | */
|
---|
[12762] | 314 | TEST_F(SystemQueueTest, Threading)
|
---|
[12751] | 315 | {
|
---|
| 316 | SystemQueue& sysq = SystemQueue::instance();
|
---|
| 317 | checkmark = false; // just to be sure..
|
---|
| 318 |
|
---|
| 319 | // start
|
---|
| 320 | sysq.run();
|
---|
| 321 |
|
---|
| 322 | // scheduleCall
|
---|
| 323 | sysq.scheduleCall(
|
---|
| 324 | boost::bind(&SystemQueueTest::LongRunner, this)
|
---|
| 325 | );
|
---|
| 326 |
|
---|
| 327 | // SystemQueue must not be empty as long as the event is not finished
|
---|
| 328 | if ( sysq.isEmpty() )
|
---|
[12750] | 329 | {
|
---|
[12751] | 330 | // assert that this test is actually meaningful
|
---|
| 331 | ASSERT_FALSE( checkmark )
|
---|
| 332 | << "NOTE: This is not necessarily a bug, maybe the timing just have to adjusted. Try to increase MAX_WAIT.";
|
---|
| 333 |
|
---|
| 334 | EXPECT_TRUE( ! sysq.isEmpty() || checkmark );
|
---|
[12750] | 335 | }
|
---|
[12751] | 336 |
|
---|
| 337 | // wait for the event to finish
|
---|
| 338 | wait_for_checkmark(MAX_WAIT);
|
---|
| 339 |
|
---|
| 340 | // stop
|
---|
| 341 | sysq.cancel();
|
---|
| 342 |
|
---|
| 343 | // even the long-runner should finally finish
|
---|
| 344 | EXPECT_TRUE( checkmark ) << "Function has not finished within " << MAX_WAIT << " microseconds.";
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | /**
|
---|
| 348 | * schedule multiple calls
|
---|
| 349 | *
|
---|
| 350 | * each call must be performed, in the correct order
|
---|
| 351 | *
|
---|
| 352 | * NOTE: The nested calls are not necessarily in order with calls scheduled from the main thread,
|
---|
| 353 | * that's fine, therefore we make sure the nested calls are done, before scheduling new ones.
|
---|
| 354 | */
|
---|
| 355 | TEST_F(SystemQueueTest, MultipleCalls)
|
---|
| 356 | {
|
---|
| 357 | SystemQueue& sysq = SystemQueue::instance();
|
---|
[12750] | 358 |
|
---|
[12751] | 359 | // start
|
---|
| 360 | sysq.run();
|
---|
| 361 |
|
---|
| 362 | sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 1) );
|
---|
| 363 | sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 2) );
|
---|
| 364 | sysq.scheduleCall( boost::bind(&SystemQueueTest::NestedOrderedCall, this, 3, 5) );
|
---|
| 365 |
|
---|
| 366 | // make sure all nested calls are done
|
---|
| 367 | wait_for_checkmark(MAX_WAIT); // XXX should be "wait_until_empty() ...."
|
---|
| 368 |
|
---|
| 369 | checkmark = false;
|
---|
| 370 | sysq.scheduleCall( boost::bind(&SystemQueueTest::OrderedCall, this, 6) );
|
---|
| 371 |
|
---|
| 372 | // XXX same here... [ wait_until_empty() ]
|
---|
| 373 | sysq.scheduleCall( boost::bind(&SystemQueueTest::Check, this) );
|
---|
| 374 | wait_for_checkmark(MAX_WAIT);
|
---|
| 375 |
|
---|
| 376 | // evaluation
|
---|
| 377 | EXPECT_EQ( 6, last_ordered_call);
|
---|
| 378 |
|
---|
[12749] | 379 | // stop
|
---|
| 380 | sysq.cancel();
|
---|
[12751] | 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
| 386 | * This subclass has some special member functions suitable for timing tests
|
---|
[12756] | 387 | *
|
---|
| 388 | * NOTE: Timing tests are not always reproducable.. sorry. :-/
|
---|
[12751] | 389 | */
|
---|
| 390 | class SystemQueueTimingTest :
|
---|
| 391 | public SystemQueueTest
|
---|
| 392 | {
|
---|
| 393 | public:
|
---|
[12753] | 394 |
|
---|
[12752] | 395 | /**
|
---|
| 396 | * typical delay time
|
---|
| 397 | *
|
---|
| 398 | * should be long enough for meaningful tests,
|
---|
| 399 | * but should not lengthen the test excessivly
|
---|
| 400 | */
|
---|
[12754] | 401 | #define DELAY_TIME 20 // ms
|
---|
[12764] | 402 | #define DELAY_MARGIN 1000 // microseconds (1/1000 ms) // NOTE: maybe a meaningful value depends
|
---|
| 403 | // on the target hardwarde and system load...
|
---|
[12753] | 404 |
|
---|
| 405 |
|
---|
| 406 | /// constructor
|
---|
| 407 | SystemQueueTimingTest() :
|
---|
| 408 | SystemQueueTest() /* super constructor */,
|
---|
[12757] | 409 | sysq( SystemQueue::instance() ),
|
---|
| 410 | calls(0)
|
---|
[12753] | 411 | {
|
---|
| 412 | }
|
---|
[12752] | 413 |
|
---|
[12753] | 414 | virtual void SetUp()
|
---|
| 415 | {
|
---|
| 416 | // start SystemQueue
|
---|
| 417 | sysq.run();
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 |
|
---|
| 421 | virtual void TearDown()
|
---|
| 422 | {
|
---|
| 423 | // stop SystemQueue
|
---|
| 424 | sysq.cancel();
|
---|
| 425 | }
|
---|
[12756] | 426 |
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * @param placed_in_queue The time (as ptime) when this event is put into the delay queue
|
---|
| 430 | * @param intended_sleep_time The time (in microseconds) the event is supposed to sleep in the queue
|
---|
| 431 | * @param margin The acceptable margin (in microseconds)
|
---|
| 432 | */
|
---|
| 433 | void TimeCheckingCall(ptime placed_in_queue, uint64_t intended_sleep_time, uint64_t margin)
|
---|
| 434 | {
|
---|
| 435 | ptime called_at = microsec_clock::local_time();
|
---|
| 436 |
|
---|
| 437 | // calculate actual sleep time and difference to intended sleep time
|
---|
| 438 | boost::posix_time::time_duration actual_sleep_time = called_at - placed_in_queue;
|
---|
| 439 | uint64_t diff = actual_sleep_time.total_microseconds() - intended_sleep_time;
|
---|
| 440 |
|
---|
| 441 | // info
|
---|
[12757] | 442 | cout << "### Timing difference: " << diff << " microseconds" << endl;
|
---|
[12756] | 443 |
|
---|
| 444 | EXPECT_LT( abs(diff), margin );
|
---|
| 445 |
|
---|
[12757] | 446 | calls++;
|
---|
[12756] | 447 | checkmark = true;
|
---|
| 448 | }
|
---|
[12753] | 449 |
|
---|
| 450 | SystemQueue& sysq;
|
---|
[12757] | 451 | int calls;
|
---|
[12751] | 452 | };
|
---|
| 453 |
|
---|
| 454 |
|
---|
[12752] | 455 | /**
|
---|
| 456 | * schedules a delayed call and tests whether it is called (more or less timely..)
|
---|
| 457 | */
|
---|
| 458 | TEST_F(SystemQueueTimingTest, DelayedCall)
|
---|
[12751] | 459 | {
|
---|
[12752] | 460 | // scheduleCall
|
---|
| 461 | sysq.scheduleCall(
|
---|
[12754] | 462 | boost::bind(&SystemQueueTimingTest::Check, this), DELAY_TIME
|
---|
[12752] | 463 | );
|
---|
| 464 |
|
---|
| 465 | // noting to do until the delay is up..
|
---|
[12764] | 466 | usleep(DELAY_TIME*1000 + DELAY_MARGIN*10);
|
---|
[12752] | 467 |
|
---|
| 468 | // wait for the event to happen
|
---|
| 469 | wait_for_checkmark(MAX_WAIT);
|
---|
| 470 |
|
---|
[12754] | 471 | EXPECT_TRUE( checkmark ) << "Delayed function was not called within delaytime ("
|
---|
[12752] | 472 | << DELAY_TIME << " ms) + " << (MAX_WAIT + DELAY_MARGIN) << " microseconds.";
|
---|
[12749] | 473 | }
|
---|
| 474 |
|
---|
[12754] | 475 |
|
---|
| 476 | /**
|
---|
[12755] | 477 | * tests whether the SystemQueue is considered non-empty, while an event is delayed-waiting
|
---|
| 478 | */
|
---|
| 479 | TEST_F(SystemQueueTimingTest, NotEmptyWhileWaiting)
|
---|
| 480 | {
|
---|
| 481 | // scheduleCall
|
---|
| 482 | sysq.scheduleCall(
|
---|
| 483 | boost::bind(&SystemQueueTimingTest::Check, this), DELAY_TIME
|
---|
| 484 | );
|
---|
| 485 |
|
---|
| 486 | // SystemQueue must not be empty as long as the event is not finished (and especially while stille queued)
|
---|
| 487 | if ( sysq.isEmpty() )
|
---|
| 488 | {
|
---|
| 489 | // assert that this test is actually meaningful
|
---|
| 490 | ASSERT_FALSE( checkmark )
|
---|
| 491 | << "NOTE: This is not necessarily a bug, maybe the timing just have to adjusted. Try to increase MAX_WAIT.";
|
---|
| 492 |
|
---|
| 493 | EXPECT_TRUE( ! sysq.isEmpty() || checkmark );
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 |
|
---|
| 499 | /**
|
---|
[12754] | 500 | * schedules a delayed call and tests whether it is called (more or less timely..)
|
---|
| 501 | */
|
---|
| 502 | TEST_F(SystemQueueTimingTest, MultipleCalls)
|
---|
| 503 | {
|
---|
| 504 | // schedule 4th call
|
---|
| 505 | sysq.scheduleCall(
|
---|
| 506 | boost::bind(&SystemQueueTest::OrderedCall, this, 4),
|
---|
| 507 | DELAY_TIME*3
|
---|
| 508 | );
|
---|
| 509 |
|
---|
| 510 | // schedule 2nd call
|
---|
| 511 | sysq.scheduleCall(
|
---|
| 512 | boost::bind(&SystemQueueTest::OrderedCall, this, 2),
|
---|
| 513 | DELAY_TIME*1
|
---|
| 514 | );
|
---|
| 515 |
|
---|
| 516 | // schedule 3rd call
|
---|
| 517 | sysq.scheduleCall(
|
---|
| 518 | boost::bind(&SystemQueueTest::OrderedCall, this, 3),
|
---|
| 519 | DELAY_TIME*2
|
---|
| 520 | );
|
---|
| 521 |
|
---|
| 522 | // schedule 1st call (without delay)
|
---|
| 523 | sysq.scheduleCall(
|
---|
| 524 | boost::bind(&SystemQueueTest::OrderedCall, this, 1)
|
---|
| 525 | );
|
---|
| 526 |
|
---|
| 527 |
|
---|
| 528 | // XXX the usual bug..
|
---|
| 529 | sysq.scheduleCall(
|
---|
[12756] | 530 | boost::bind(&SystemQueueTimingTest::Check, this),
|
---|
[12754] | 531 | DELAY_TIME*4
|
---|
| 532 | );
|
---|
| 533 |
|
---|
| 534 | // noting to do until the delay is up..
|
---|
[12764] | 535 | usleep(DELAY_TIME * 4000 + DELAY_MARGIN*10);
|
---|
[12754] | 536 |
|
---|
| 537 | // wait for the event to happen
|
---|
| 538 | wait_for_checkmark(MAX_WAIT);
|
---|
| 539 |
|
---|
| 540 | // evaluation
|
---|
| 541 | EXPECT_EQ( 4, last_ordered_call);
|
---|
| 542 | }
|
---|
[12756] | 543 |
|
---|
| 544 |
|
---|
| 545 | /**
|
---|
| 546 | * Schedules a delayed call and test whether the sleep time is acurate
|
---|
| 547 | */
|
---|
| 548 | TEST_F(SystemQueueTimingTest, TimingSingleCall)
|
---|
| 549 | {
|
---|
| 550 | ptime now = microsec_clock::local_time();
|
---|
| 551 |
|
---|
| 552 | sysq.scheduleCall(
|
---|
| 553 | boost::bind(&SystemQueueTimingTest::TimeCheckingCall, this,
|
---|
| 554 | now, DELAY_TIME*1000, DELAY_MARGIN),
|
---|
| 555 | DELAY_TIME
|
---|
| 556 | );
|
---|
| 557 |
|
---|
| 558 | // main thread is going to sleep..
|
---|
| 559 | usleep(DELAY_TIME * 1000 + DELAY_MARGIN * 2);
|
---|
| 560 | wait_for_checkmark(MAX_WAIT);
|
---|
| 561 |
|
---|
| 562 | // make sure the measurement function was called at all
|
---|
| 563 | EXPECT_TRUE(checkmark) << "Measurement function was NOT RUN AT ALL!";
|
---|
| 564 | }
|
---|
| 565 |
|
---|
[12757] | 566 |
|
---|
| 567 | /**
|
---|
| 568 | * Like TimingSingleCall but tests whether the timings change when multiple events are scheduled.
|
---|
| 569 | */
|
---|
| 570 | TEST_F(SystemQueueTimingTest, TimingMultipleCalls)
|
---|
| 571 | {
|
---|
| 572 | ptime now = microsec_clock::local_time();
|
---|
| 573 |
|
---|
| 574 | sysq.scheduleCall(
|
---|
| 575 | boost::bind(&SystemQueueTimingTest::TimeCheckingCall, this,
|
---|
| 576 | now, DELAY_TIME*1000 * 2, DELAY_MARGIN),
|
---|
| 577 | DELAY_TIME * 2
|
---|
| 578 | );
|
---|
| 579 |
|
---|
| 580 | sysq.scheduleCall(
|
---|
| 581 | boost::bind(&SystemQueueTimingTest::TimeCheckingCall, this,
|
---|
| 582 | now, DELAY_TIME*1000 * 3, DELAY_MARGIN),
|
---|
| 583 | DELAY_TIME * 3
|
---|
| 584 | );
|
---|
| 585 |
|
---|
| 586 | sysq.scheduleCall(
|
---|
| 587 | boost::bind(&SystemQueueTimingTest::TimeCheckingCall, this,
|
---|
| 588 | now, DELAY_TIME*1000, DELAY_MARGIN),
|
---|
| 589 | DELAY_TIME
|
---|
| 590 | );
|
---|
| 591 |
|
---|
| 592 | // main thread is going to sleep..
|
---|
| 593 | usleep(DELAY_TIME * 3000 + DELAY_MARGIN * 2);
|
---|
| 594 | wait_for_checkmark(MAX_WAIT); // XXX wait_until_empty
|
---|
| 595 |
|
---|
| 596 | // make sure the measurement function was called at all
|
---|
| 597 | EXPECT_EQ(3, calls) << "Not every event was performed..";
|
---|
| 598 | }
|
---|
[12758] | 599 |
|
---|
| 600 |
|
---|
| 601 | /*
|
---|
| 602 | * TODO
|
---|
| 603 | *
|
---|
[12764] | 604 | * maybe one more complicated testcase with timing and directly scheduled events
|
---|
[12758] | 605 | *
|
---|
| 606 | * but this probably only makes sense after a working SysQ implementation exists..
|
---|
| 607 | */
|
---|