1 //------------------------------------------------------------------------------ 2 // BMessageRunnerTester.cpp 3 // 4 //------------------------------------------------------------------------------ 5 6 // Standard Includes ----------------------------------------------------------- 7 #include <stdio.h> 8 9 // System Includes ------------------------------------------------------------- 10 #include <Application.h> 11 #include <Handler.h> 12 #include <Looper.h> 13 #include <Message.h> 14 #include <MessageRunner.h> 15 #include <Messenger.h> 16 #include <OS.h> 17 18 // Project Includes ------------------------------------------------------------ 19 #include <TestShell.h> 20 #include <TestUtils.h> 21 #include <cppunit/TestAssert.h> 22 23 // Local Includes -------------------------------------------------------------- 24 #include "BMessageRunnerTester.h" 25 #include "MessageRunnerTestHelpers.h" 26 27 // Local Defines --------------------------------------------------------------- 28 29 // Globals --------------------------------------------------------------------- 30 31 //------------------------------------------------------------------------------ 32 33 static const char *kTesterSignature 34 = "application/x-vnd.obos-messagerunner-constructor-test"; 35 36 static const bigtime_t kMinTimeInterval = 50000; 37 38 // check_message_runner_info 39 static 40 void 41 check_message_runner_info(const BMessageRunner &runner, status_t error, 42 bigtime_t interval = 0, int32 count = 0) 43 { 44 bigtime_t runnerInterval = 0; 45 int32 runnerCount = 0; 46 CHK(runner.GetInfo(&runnerInterval, &runnerCount) == error); 47 if (error == B_OK) { 48 CHK(runnerInterval == interval); 49 CHK(runnerCount == count); 50 } 51 } 52 53 /* 54 BMessageRunner(BMessenger target, const BMessage *message, 55 bigtime_t interval, int32 count) 56 @case 1 target is invalid, message is valid, interval > 0, 57 count > 0 58 @results InitCheck() should return B_OK. The message runner turns 59 to unusable as soon as the first message had to be sent. 60 GetInfo() should return B_OK. 61 */ 62 void TBMessageRunnerTester::BMessageRunnerA1() 63 { 64 MessageRunnerTestApp app(kTesterSignature); 65 BMessenger target; 66 BMessage message(MSG_RUNNER_MESSAGE); 67 bigtime_t interval = 100000; 68 int32 count = 5; 69 BMessageRunner runner(target, &message, interval, count); 70 CHK(runner.InitCheck() == B_OK); 71 check_message_runner_info(runner, B_OK, interval, count); 72 snooze(interval + 10000); 73 check_message_runner_info(runner, B_BAD_VALUE); 74 CHK(app.CountReplies() == 0); 75 } 76 77 /* 78 BMessageRunner(BMessenger target, const BMessage *message, 79 bigtime_t interval, int32 count) 80 @case 2 target is valid, message is NULL, interval > 0, count > 0 81 @results InitCheck() should return B_BAD_VALUE. 82 GetInfo() should return B_BAD_VALUE. 83 */ 84 void TBMessageRunnerTester::BMessageRunnerA2() 85 { 86 // R5: chrashes when passing a NULL message 87 #ifndef TEST_R5 88 MessageRunnerTestApp app(kTesterSignature); 89 MessageRunnerTestLooper *looper = app.TestLooper(); 90 BMessenger target(looper); 91 bigtime_t interval = 100000; 92 int32 count = 5; 93 BMessageRunner runner(target, NULL, interval, count); 94 CHK(runner.InitCheck() == B_BAD_VALUE); 95 check_message_runner_info(runner, B_BAD_VALUE); 96 #endif 97 } 98 99 /* 100 BMessageRunner(BMessenger target, const BMessage *message, 101 bigtime_t interval, int32 count) 102 @case 3 target is valid, message is valid, interval == 0, count > 0 103 @results R5: InitCheck() should return B_ERROR. 104 GetInfo() should return B_BAD_VALUE. 105 OBOS: InitCheck() should return B_OK. 106 GetInfo() should return B_OK. 107 A minimal time interval is used (50000). 108 */ 109 void TBMessageRunnerTester::BMessageRunnerA3() 110 { 111 MessageRunnerTestApp app(kTesterSignature); 112 MessageRunnerTestLooper *looper = app.TestLooper(); 113 BMessenger target(looper); 114 BMessage message(MSG_RUNNER_MESSAGE); 115 bigtime_t interval = 0; 116 int32 count = 5; 117 BMessageRunner runner(target, &message, interval, count); 118 #ifdef TEST_R5 119 CHK(runner.InitCheck() == B_ERROR); 120 check_message_runner_info(runner, B_BAD_VALUE); 121 #else 122 bigtime_t startTime = system_time(); 123 CHK(runner.InitCheck() == B_OK); 124 interval = max(interval, kMinTimeInterval); 125 check_message_runner_info(runner, B_OK, interval, count); 126 snooze((count + 1) * interval + 10000); 127 CHK(looper->CheckMessages(startTime, interval, count)); 128 CHK(app.CountReplies() == count); 129 #endif 130 } 131 132 /* 133 BMessageRunner(BMessenger target, const BMessage *message, 134 bigtime_t interval, int32 count) 135 @case 4 target is valid, message is valid, interval < 0, count > 0 136 @results InitCheck() should return B_OK. 137 GetInfo() should return B_OK. 138 A minimal time interval is used (50000). 139 */ 140 void TBMessageRunnerTester::BMessageRunnerA4() 141 { 142 MessageRunnerTestApp app(kTesterSignature); 143 MessageRunnerTestLooper *looper = app.TestLooper(); 144 BMessenger target(looper); 145 BMessage message(MSG_RUNNER_MESSAGE); 146 bigtime_t interval = -1; 147 int32 count = 5; 148 BMessageRunner runner(target, &message, interval, count); 149 bigtime_t startTime = system_time(); 150 CHK(runner.InitCheck() == B_OK); 151 interval = max(interval, kMinTimeInterval); 152 check_message_runner_info(runner, B_OK, interval, count); 153 snooze((count + 1) * interval + 10000); 154 CHK(looper->CheckMessages(startTime, interval, count)); 155 CHK(app.CountReplies() == count); 156 } 157 158 /* 159 BMessageRunner(BMessenger target, const BMessage *message, 160 bigtime_t interval, int32 count) 161 @case 5 target is valid, message is valid, 162 interval == LONGLONG_MAX, count > 0 163 @results InitCheck() should return B_OK. 164 GetInfo() should return B_OK. 165 No message should be delivered. 166 */ 167 void TBMessageRunnerTester::BMessageRunnerA5() 168 { 169 // R5: doesn't behave very well. In worst case registrar time loop gets 170 // locked up and system wide message runners don't get messages anymore. 171 #ifndef TEST_R5 172 MessageRunnerTestApp app(kTesterSignature); 173 MessageRunnerTestLooper *looper = app.TestLooper(); 174 BMessenger target(looper); 175 BMessage message(MSG_RUNNER_MESSAGE); 176 bigtime_t interval = LONGLONG_MAX; 177 int32 count = 5; 178 BMessageRunner runner(target, &message, interval, count); 179 bigtime_t startTime = system_time(); 180 CHK(runner.InitCheck() == B_OK); 181 interval = max(interval, kMinTimeInterval); 182 check_message_runner_info(runner, B_OK, interval, count); 183 snooze(10000); 184 CHK(looper->CheckMessages(startTime, interval, 0)); 185 CHK(app.CountReplies() == 0); 186 #endif 187 } 188 189 /* 190 BMessageRunner(BMessenger target, const BMessage *message, 191 bigtime_t interval, int32 count) 192 @case 6 target is valid, message is valid, interval > 0, count == 0 193 @results InitCheck() should return B_ERROR. 194 GetInfo() should return B_BAD_VALUE. 195 */ 196 void TBMessageRunnerTester::BMessageRunnerA6() 197 { 198 MessageRunnerTestApp app(kTesterSignature); 199 MessageRunnerTestLooper *looper = app.TestLooper(); 200 BMessenger target(looper); 201 BMessage message(MSG_RUNNER_MESSAGE); 202 bigtime_t interval = 100000; 203 int32 count = 0; 204 BMessageRunner runner(target, &message, interval, count); 205 CHK(runner.InitCheck() == B_ERROR); 206 check_message_runner_info(runner, B_BAD_VALUE); 207 } 208 209 /* 210 BMessageRunner(BMessenger target, const BMessage *message, 211 bigtime_t interval, int32 count) 212 @case 7 target is valid, message is valid, interval > 0, count < 0 213 @results InitCheck() should return B_OK. 214 GetInfo() should return B_OK. 215 Unlimited number of messages. 216 */ 217 void TBMessageRunnerTester::BMessageRunnerA7() 218 { 219 MessageRunnerTestApp app(kTesterSignature); 220 MessageRunnerTestLooper *looper = app.TestLooper(); 221 BMessenger target(looper); 222 BMessage message(MSG_RUNNER_MESSAGE); 223 bigtime_t interval = 100000; 224 int32 count = 5; 225 BMessageRunner runner(target, &message, interval, count); 226 bigtime_t startTime = system_time(); 227 CHK(runner.InitCheck() == B_OK); 228 interval = max(interval, kMinTimeInterval); 229 check_message_runner_info(runner, B_OK, interval, count); 230 int32 checkCount = 5; 231 snooze(checkCount * interval + 10000); 232 CHK(looper->CheckMessages(startTime, interval, checkCount)); 233 CHK(app.CountReplies() == checkCount); 234 } 235 236 /* 237 BMessageRunner(BMessenger target, const BMessage *message, 238 bigtime_t interval, int32 count) 239 @case 8 target is valid, message is valid, interval > 0, count > 0 240 @results InitCheck() should return B_OK. 241 GetInfo() should return B_OK. 242 count messages are sent. 243 */ 244 void TBMessageRunnerTester::BMessageRunnerA8() 245 { 246 MessageRunnerTestApp app(kTesterSignature); 247 MessageRunnerTestLooper *looper = app.TestLooper(); 248 BMessenger target(looper); 249 BMessage message(MSG_RUNNER_MESSAGE); 250 bigtime_t interval = 100000; 251 int32 count = 5; 252 BMessageRunner runner(target, &message, interval, count); 253 bigtime_t startTime = system_time(); 254 CHK(runner.InitCheck() == B_OK); 255 interval = max(interval, kMinTimeInterval); 256 check_message_runner_info(runner, B_OK, interval, count); 257 snooze((count + 1) * interval + 10000); 258 CHK(looper->CheckMessages(startTime, interval, count)); 259 CHK(app.CountReplies() == count); 260 } 261 262 /* 263 BMessageRunner(BMessenger target, const BMessage *message, 264 bigtime_t interval, int32 count, BMessenger replyTo) 265 @case 1 target is invalid, message is valid, interval > 0, 266 count > 0 267 @results InitCheck() should return B_OK. The message runner turns 268 to unusable as soon as the first message had to be sent. 269 GetInfo() should return B_OK. 270 */ 271 void TBMessageRunnerTester::BMessageRunnerB1() 272 { 273 MessageRunnerTestApp app(kTesterSignature); 274 BMessenger target; 275 BMessage message(MSG_RUNNER_MESSAGE); 276 bigtime_t interval = 100000; 277 int32 count = 5; 278 MessageRunnerTestHandler *handler = app.TestHandler(); 279 BMessenger replyTo(handler); 280 BMessageRunner runner(target, &message, interval, count, replyTo); 281 CHK(runner.InitCheck() == B_OK); 282 check_message_runner_info(runner, B_OK, interval, count); 283 snooze(interval + 10000); 284 check_message_runner_info(runner, B_BAD_VALUE); 285 CHK(app.CountReplies() == 0); 286 CHK(handler->CountReplies() == 0); 287 } 288 289 /* 290 BMessageRunner(BMessenger target, const BMessage *message, 291 bigtime_t interval, int32 count, BMessenger replyTo) 292 @case 2 target is valid, message is NULL, interval > 0, count > 0 293 @results InitCheck() should return B_BAD_VALUE. 294 GetInfo() should return B_BAD_VALUE. 295 */ 296 void TBMessageRunnerTester::BMessageRunnerB2() 297 { 298 // R5: chrashes when passing a NULL message 299 #ifndef TEST_R5 300 MessageRunnerTestApp app(kTesterSignature); 301 MessageRunnerTestLooper *looper = app.TestLooper(); 302 BMessenger target(looper); 303 bigtime_t interval = 100000; 304 int32 count = 5; 305 MessageRunnerTestHandler *handler = app.TestHandler(); 306 BMessenger replyTo(handler); 307 BMessageRunner runner(target, NULL, interval, count, replyTo); 308 CHK(runner.InitCheck() == B_BAD_VALUE); 309 check_message_runner_info(runner, B_BAD_VALUE); 310 #endif 311 } 312 313 /* 314 BMessageRunner(BMessenger target, const BMessage *message, 315 bigtime_t interval, int32 count, BMessenger replyTo) 316 @case 3 target is valid, message is valid, interval == 0, count > 0 317 @results R5: InitCheck() should return B_ERROR. 318 GetInfo() should return B_BAD_VALUE. 319 OBOS: InitCheck() should return B_OK. 320 GetInfo() should return B_OK. 321 A minimal time interval is used (50000). 322 */ 323 void TBMessageRunnerTester::BMessageRunnerB3() 324 { 325 MessageRunnerTestApp app(kTesterSignature); 326 MessageRunnerTestLooper *looper = app.TestLooper(); 327 BMessenger target(looper); 328 BMessage message(MSG_RUNNER_MESSAGE); 329 bigtime_t interval = 0; 330 int32 count = 5; 331 MessageRunnerTestHandler *handler = app.TestHandler(); 332 BMessenger replyTo(handler); 333 BMessageRunner runner(target, &message, interval, count, replyTo); 334 #ifdef TEST_R5 335 CHK(runner.InitCheck() == B_ERROR); 336 check_message_runner_info(runner, B_BAD_VALUE); 337 #else 338 bigtime_t startTime = system_time(); 339 CHK(runner.InitCheck() == B_OK); 340 interval = max(interval, kMinTimeInterval); 341 check_message_runner_info(runner, B_OK, interval, count); 342 snooze((count + 1) * interval + 10000); 343 CHK(looper->CheckMessages(startTime, interval, count)); 344 CHK(app.CountReplies() == 0); 345 CHK(handler->CountReplies() == count); 346 #endif 347 } 348 349 /* 350 BMessageRunner(BMessenger target, const BMessage *message, 351 bigtime_t interval, int32 count, BMessenger replyTo) 352 @case 4 target is valid, message is valid, interval < 0, count > 0 353 @results InitCheck() should return B_OK. 354 GetInfo() should return B_OK. 355 A minimal time interval is used (50000). 356 */ 357 void TBMessageRunnerTester::BMessageRunnerB4() 358 { 359 MessageRunnerTestApp app(kTesterSignature); 360 MessageRunnerTestLooper *looper = app.TestLooper(); 361 BMessenger target(looper); 362 BMessage message(MSG_RUNNER_MESSAGE); 363 bigtime_t interval = -1; 364 int32 count = 5; 365 MessageRunnerTestHandler *handler = app.TestHandler(); 366 BMessenger replyTo(handler); 367 BMessageRunner runner(target, &message, interval, count, replyTo); 368 bigtime_t startTime = system_time(); 369 CHK(runner.InitCheck() == B_OK); 370 interval = max(interval, kMinTimeInterval); 371 check_message_runner_info(runner, B_OK, interval, count); 372 snooze((count + 1) * interval + 10000); 373 CHK(looper->CheckMessages(startTime, interval, count)); 374 CHK(app.CountReplies() == 0); 375 CHK(handler->CountReplies() == count); 376 } 377 378 /* 379 BMessageRunner(BMessenger target, const BMessage *message, 380 bigtime_t interval, int32 count, BMessenger replyTo) 381 @case 5 target is valid, message is valid, 382 interval == LONGLONG_MAX, count > 0 383 @results InitCheck() should return B_OK. 384 GetInfo() should return B_OK. 385 No message should be delivered. 386 */ 387 void TBMessageRunnerTester::BMessageRunnerB5() 388 { 389 // R5: doesn't behave very well. In worst case registrar time loop gets 390 // locked up and system wide message runners don't get messages anymore. 391 #ifndef TEST_R5 392 MessageRunnerTestApp app(kTesterSignature); 393 MessageRunnerTestLooper *looper = app.TestLooper(); 394 BMessenger target(looper); 395 BMessage message(MSG_RUNNER_MESSAGE); 396 bigtime_t interval = LONGLONG_MAX; 397 int32 count = 5; 398 MessageRunnerTestHandler *handler = app.TestHandler(); 399 BMessenger replyTo(handler); 400 BMessageRunner runner(target, &message, interval, count, replyTo); 401 bigtime_t startTime = system_time(); 402 CHK(runner.InitCheck() == B_OK); 403 interval = max(interval, kMinTimeInterval); 404 check_message_runner_info(runner, B_OK, interval, count); 405 snooze(10000); 406 CHK(looper->CheckMessages(startTime, interval, 0)); 407 CHK(app.CountReplies() == 0); 408 CHK(handler->CountReplies() == 0); 409 #endif 410 } 411 412 /* 413 BMessageRunner(BMessenger target, const BMessage *message, 414 bigtime_t interval, int32 count, BMessenger replyTo) 415 @case 6 target is valid, message is valid, interval > 0, count == 0 416 @results InitCheck() should return B_ERROR. 417 GetInfo() should return B_BAD_VALUE. 418 */ 419 void TBMessageRunnerTester::BMessageRunnerB6() 420 { 421 MessageRunnerTestApp app(kTesterSignature); 422 MessageRunnerTestLooper *looper = app.TestLooper(); 423 BMessenger target(looper); 424 BMessage message(MSG_RUNNER_MESSAGE); 425 bigtime_t interval = 100000; 426 int32 count = 0; 427 MessageRunnerTestHandler *handler = app.TestHandler(); 428 BMessenger replyTo(handler); 429 BMessageRunner runner(target, &message, interval, count, replyTo); 430 CHK(runner.InitCheck() == B_ERROR); 431 check_message_runner_info(runner, B_BAD_VALUE); 432 } 433 434 /* 435 BMessageRunner(BMessenger target, const BMessage *message, 436 bigtime_t interval, int32 count, BMessenger replyTo) 437 @case 7 target is valid, message is valid, interval > 0, count < 0 438 @results InitCheck() should return B_OK. 439 GetInfo() should return B_OK. 440 Unlimited number of messages. 441 */ 442 void TBMessageRunnerTester::BMessageRunnerB7() 443 { 444 MessageRunnerTestApp app(kTesterSignature); 445 MessageRunnerTestLooper *looper = app.TestLooper(); 446 BMessenger target(looper); 447 BMessage message(MSG_RUNNER_MESSAGE); 448 bigtime_t interval = 100000; 449 int32 count = 5; 450 MessageRunnerTestHandler *handler = app.TestHandler(); 451 BMessenger replyTo(handler); 452 BMessageRunner runner(target, &message, interval, count, replyTo); 453 bigtime_t startTime = system_time(); 454 CHK(runner.InitCheck() == B_OK); 455 interval = max(interval, kMinTimeInterval); 456 check_message_runner_info(runner, B_OK, interval, count); 457 int32 checkCount = 5; 458 snooze(checkCount * interval + 10000); 459 CHK(looper->CheckMessages(startTime, interval, checkCount)); 460 CHK(app.CountReplies() == 0); 461 CHK(handler->CountReplies() == checkCount); 462 } 463 464 /* 465 BMessageRunner(BMessenger target, const BMessage *message, 466 bigtime_t interval, int32 count, BMessenger replyTo) 467 @case 8 target is valid, message is valid, interval > 0, count > 0 468 @results InitCheck() should return B_OK. 469 GetInfo() should return B_OK. 470 count messages are sent. 471 */ 472 void TBMessageRunnerTester::BMessageRunnerB8() 473 { 474 MessageRunnerTestApp app(kTesterSignature); 475 MessageRunnerTestLooper *looper = app.TestLooper(); 476 BMessenger target(looper); 477 BMessage message(MSG_RUNNER_MESSAGE); 478 bigtime_t interval = 100000; 479 int32 count = 5; 480 MessageRunnerTestHandler *handler = app.TestHandler(); 481 BMessenger replyTo(handler); 482 BMessageRunner runner(target, &message, interval, count, replyTo); 483 bigtime_t startTime = system_time(); 484 CHK(runner.InitCheck() == B_OK); 485 interval = max(interval, kMinTimeInterval); 486 check_message_runner_info(runner, B_OK, interval, count); 487 snooze((count + 1) * interval + 10000); 488 CHK(looper->CheckMessages(startTime, interval, count)); 489 CHK(app.CountReplies() == 0); 490 CHK(handler->CountReplies() == count); 491 } 492 493 /* 494 BMessageRunner(BMessenger target, const BMessage *message, 495 bigtime_t interval, int32 count, BMessenger replyTo) 496 @case 9 target is valid, message is valid, interval > 0, count > 0, 497 replyTo is invalid 498 @results InitCheck() should return B_OK. 499 GetInfo() should return B_OK. 500 count messages are sent. The replies go to the registrar! 501 */ 502 void TBMessageRunnerTester::BMessageRunnerB9() 503 { 504 MessageRunnerTestApp app(kTesterSignature); 505 MessageRunnerTestLooper *looper = app.TestLooper(); 506 BMessenger target(looper); 507 BMessage message(MSG_RUNNER_MESSAGE); 508 bigtime_t interval = 100000; 509 int32 count = 5; 510 MessageRunnerTestHandler *handler = app.TestHandler(); 511 BMessenger replyTo(handler); 512 BMessageRunner runner(target, &message, interval, count, replyTo); 513 bigtime_t startTime = system_time(); 514 CHK(runner.InitCheck() == B_OK); 515 interval = max(interval, kMinTimeInterval); 516 check_message_runner_info(runner, B_OK, interval, count); 517 snooze((count + 1) * interval + 10000); 518 CHK(looper->CheckMessages(startTime, interval, count)); 519 CHK(app.CountReplies() == 0); 520 CHK(handler->CountReplies() == count); 521 } 522 523 524 Test* TBMessageRunnerTester::Suite() 525 { 526 TestSuite* SuiteOfTests = new TestSuite; 527 528 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 529 BMessageRunnerA1); 530 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 531 BMessageRunnerA2); 532 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 533 BMessageRunnerA3); 534 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 535 BMessageRunnerA4); 536 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 537 BMessageRunnerA5); 538 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 539 BMessageRunnerA6); 540 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 541 BMessageRunnerA7); 542 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 543 BMessageRunnerA8); 544 545 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 546 BMessageRunnerB1); 547 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 548 BMessageRunnerB2); 549 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 550 BMessageRunnerB3); 551 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 552 BMessageRunnerB4); 553 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 554 BMessageRunnerB5); 555 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 556 BMessageRunnerB6); 557 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 558 BMessageRunnerB7); 559 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 560 BMessageRunnerB8); 561 ADD_TEST4(BMessageRunner, SuiteOfTests, TBMessageRunnerTester, 562 BMessageRunnerB9); 563 564 return SuiteOfTests; 565 } 566 567