1 //------------------------------------------------------------------------------
2 // BMessageRunnerTester.cpp
4 //------------------------------------------------------------------------------
6 // Standard Includes -----------------------------------------------------------
10 // System Includes -------------------------------------------------------------
11 #include <Application.h>
15 #include <MessageRunner.h>
16 #include <Messenger.h>
19 // Project Includes ------------------------------------------------------------
20 #include <TestShell.h>
21 #include <TestUtils.h>
22 #include <cppunit/TestAssert.h>
24 // Local Includes --------------------------------------------------------------
25 #include "BMessageRunnerTester.h"
26 #include "MessageRunnerTestHelpers.h"
28 // Local Defines ---------------------------------------------------------------
30 // Globals ---------------------------------------------------------------------
32 //------------------------------------------------------------------------------
38 static const char *kTesterSignature
39 = "application/x-vnd.obos-messagerunner-constructor-test";
41 static const bigtime_t kMinTimeInterval
= 50000;
43 // check_message_runner_info
46 check_message_runner_info(const BMessageRunner
&runner
, status_t error
,
47 bigtime_t interval
= 0, int32 count
= 0)
49 bigtime_t runnerInterval
= 0;
50 int32 runnerCount
= 0;
51 CHK(runner
.GetInfo(&runnerInterval
, &runnerCount
) == error
);
53 CHK(runnerInterval
== interval
);
54 CHK(runnerCount
== count
);
59 BMessageRunner(BMessenger target, const BMessage *message,
60 bigtime_t interval, int32 count)
61 @case 1 target is invalid, message is valid, interval > 0,
63 @results InitCheck() should return B_OK. The message runner turns
64 to unusable as soon as the first message had to be sent.
65 GetInfo() should return B_OK.
67 void TBMessageRunnerTester::BMessageRunnerA1()
69 MessageRunnerTestApp
app(kTesterSignature
);
71 BMessage
message(MSG_RUNNER_MESSAGE
);
72 bigtime_t interval
= 100000;
74 BMessageRunner
runner(target
, &message
, interval
, count
);
75 CHK(runner
.InitCheck() == B_OK
);
76 check_message_runner_info(runner
, B_OK
, interval
, count
);
77 snooze(interval
+ 10000);
78 check_message_runner_info(runner
, B_BAD_VALUE
);
79 CHK(app
.CountReplies() == 0);
83 BMessageRunner(BMessenger target, const BMessage *message,
84 bigtime_t interval, int32 count)
85 @case 2 target is valid, message is NULL, interval > 0, count > 0
86 @results InitCheck() should return B_BAD_VALUE.
87 GetInfo() should return B_BAD_VALUE.
89 void TBMessageRunnerTester::BMessageRunnerA2()
91 // R5: chrashes when passing a NULL message
93 MessageRunnerTestApp
app(kTesterSignature
);
94 MessageRunnerTestLooper
*looper
= app
.TestLooper();
95 BMessenger
target(looper
);
96 bigtime_t interval
= 100000;
98 BMessageRunner
runner(target
, NULL
, interval
, count
);
99 CHK(runner
.InitCheck() == B_BAD_VALUE
);
100 check_message_runner_info(runner
, B_BAD_VALUE
);
105 BMessageRunner(BMessenger target, const BMessage *message,
106 bigtime_t interval, int32 count)
107 @case 3 target is valid, message is valid, interval == 0, count > 0
108 @results R5: InitCheck() should return B_ERROR.
109 GetInfo() should return B_BAD_VALUE.
110 OBOS: InitCheck() should return B_OK.
111 GetInfo() should return B_OK.
112 A minimal time interval is used (50000).
114 void TBMessageRunnerTester::BMessageRunnerA3()
116 MessageRunnerTestApp
app(kTesterSignature
);
117 MessageRunnerTestLooper
*looper
= app
.TestLooper();
118 BMessenger
target(looper
);
119 BMessage
message(MSG_RUNNER_MESSAGE
);
120 bigtime_t interval
= 0;
122 BMessageRunner
runner(target
, &message
, interval
, count
);
124 CHK(runner
.InitCheck() == B_ERROR
);
125 check_message_runner_info(runner
, B_BAD_VALUE
);
127 bigtime_t startTime
= system_time();
128 CHK(runner
.InitCheck() == B_OK
);
129 interval
= max(interval
, kMinTimeInterval
);
130 check_message_runner_info(runner
, B_OK
, interval
, count
);
131 snooze((count
+ 1) * interval
+ 10000);
132 CHK(looper
->CheckMessages(startTime
, interval
, count
));
133 CHK(app
.CountReplies() == count
);
138 BMessageRunner(BMessenger target, const BMessage *message,
139 bigtime_t interval, int32 count)
140 @case 4 target is valid, message is valid, interval < 0, count > 0
141 @results InitCheck() should return B_OK.
142 GetInfo() should return B_OK.
143 A minimal time interval is used (50000).
145 void TBMessageRunnerTester::BMessageRunnerA4()
147 MessageRunnerTestApp
app(kTesterSignature
);
148 MessageRunnerTestLooper
*looper
= app
.TestLooper();
149 BMessenger
target(looper
);
150 BMessage
message(MSG_RUNNER_MESSAGE
);
151 bigtime_t interval
= -1;
153 BMessageRunner
runner(target
, &message
, interval
, count
);
154 bigtime_t startTime
= system_time();
155 CHK(runner
.InitCheck() == B_OK
);
156 interval
= max(interval
, kMinTimeInterval
);
157 check_message_runner_info(runner
, B_OK
, interval
, count
);
158 snooze((count
+ 1) * interval
+ 10000);
159 CHK(looper
->CheckMessages(startTime
, interval
, count
));
160 CHK(app
.CountReplies() == count
);
164 BMessageRunner(BMessenger target, const BMessage *message,
165 bigtime_t interval, int32 count)
166 @case 5 target is valid, message is valid,
167 interval == LONGLONG_MAX, count > 0
168 @results InitCheck() should return B_OK.
169 GetInfo() should return B_OK.
170 No message should be delivered.
172 void TBMessageRunnerTester::BMessageRunnerA5()
174 // R5: doesn't behave very well. In worst case registrar time loop gets
175 // locked up and system wide message runners don't get messages anymore.
177 MessageRunnerTestApp
app(kTesterSignature
);
178 MessageRunnerTestLooper
*looper
= app
.TestLooper();
179 BMessenger
target(looper
);
180 BMessage
message(MSG_RUNNER_MESSAGE
);
181 bigtime_t interval
= LONGLONG_MAX
;
183 BMessageRunner
runner(target
, &message
, interval
, count
);
184 bigtime_t startTime
= system_time();
185 CHK(runner
.InitCheck() == B_OK
);
186 interval
= max(interval
, kMinTimeInterval
);
187 check_message_runner_info(runner
, B_OK
, interval
, count
);
189 CHK(looper
->CheckMessages(startTime
, interval
, 0));
190 CHK(app
.CountReplies() == 0);
195 BMessageRunner(BMessenger target, const BMessage *message,
196 bigtime_t interval, int32 count)
197 @case 6 target is valid, message is valid, interval > 0, count == 0
198 @results InitCheck() should return B_ERROR.
199 GetInfo() should return B_BAD_VALUE.
201 void TBMessageRunnerTester::BMessageRunnerA6()
203 MessageRunnerTestApp
app(kTesterSignature
);
204 MessageRunnerTestLooper
*looper
= app
.TestLooper();
205 BMessenger
target(looper
);
206 BMessage
message(MSG_RUNNER_MESSAGE
);
207 bigtime_t interval
= 100000;
209 BMessageRunner
runner(target
, &message
, interval
, count
);
210 CHK(runner
.InitCheck() == B_ERROR
);
211 check_message_runner_info(runner
, B_BAD_VALUE
);
215 BMessageRunner(BMessenger target, const BMessage *message,
216 bigtime_t interval, int32 count)
217 @case 7 target is valid, message is valid, interval > 0, count < 0
218 @results InitCheck() should return B_OK.
219 GetInfo() should return B_OK.
220 Unlimited number of messages.
222 void TBMessageRunnerTester::BMessageRunnerA7()
224 MessageRunnerTestApp
app(kTesterSignature
);
225 MessageRunnerTestLooper
*looper
= app
.TestLooper();
226 BMessenger
target(looper
);
227 BMessage
message(MSG_RUNNER_MESSAGE
);
228 bigtime_t interval
= 100000;
230 BMessageRunner
runner(target
, &message
, interval
, count
);
231 bigtime_t startTime
= system_time();
232 CHK(runner
.InitCheck() == B_OK
);
233 interval
= max(interval
, kMinTimeInterval
);
234 check_message_runner_info(runner
, B_OK
, interval
, count
);
235 int32 checkCount
= 5;
236 snooze(checkCount
* interval
+ 10000);
237 CHK(looper
->CheckMessages(startTime
, interval
, checkCount
));
238 CHK(app
.CountReplies() == checkCount
);
242 BMessageRunner(BMessenger target, const BMessage *message,
243 bigtime_t interval, int32 count)
244 @case 8 target is valid, message is valid, interval > 0, count > 0
245 @results InitCheck() should return B_OK.
246 GetInfo() should return B_OK.
247 count messages are sent.
249 void TBMessageRunnerTester::BMessageRunnerA8()
251 MessageRunnerTestApp
app(kTesterSignature
);
252 MessageRunnerTestLooper
*looper
= app
.TestLooper();
253 BMessenger
target(looper
);
254 BMessage
message(MSG_RUNNER_MESSAGE
);
255 bigtime_t interval
= 100000;
257 BMessageRunner
runner(target
, &message
, interval
, count
);
258 bigtime_t startTime
= system_time();
259 CHK(runner
.InitCheck() == B_OK
);
260 interval
= max(interval
, kMinTimeInterval
);
261 check_message_runner_info(runner
, B_OK
, interval
, count
);
262 snooze((count
+ 1) * interval
+ 10000);
263 CHK(looper
->CheckMessages(startTime
, interval
, count
));
264 CHK(app
.CountReplies() == count
);
268 BMessageRunner(BMessenger target, const BMessage *message,
269 bigtime_t interval, int32 count, BMessenger replyTo)
270 @case 1 target is invalid, message is valid, interval > 0,
272 @results InitCheck() should return B_OK. The message runner turns
273 to unusable as soon as the first message had to be sent.
274 GetInfo() should return B_OK.
276 void TBMessageRunnerTester::BMessageRunnerB1()
278 MessageRunnerTestApp
app(kTesterSignature
);
280 BMessage
message(MSG_RUNNER_MESSAGE
);
281 bigtime_t interval
= 100000;
283 MessageRunnerTestHandler
*handler
= app
.TestHandler();
284 BMessenger
replyTo(handler
);
285 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
286 CHK(runner
.InitCheck() == B_OK
);
287 check_message_runner_info(runner
, B_OK
, interval
, count
);
288 snooze(interval
+ 10000);
289 check_message_runner_info(runner
, B_BAD_VALUE
);
290 CHK(app
.CountReplies() == 0);
291 CHK(handler
->CountReplies() == 0);
295 BMessageRunner(BMessenger target, const BMessage *message,
296 bigtime_t interval, int32 count, BMessenger replyTo)
297 @case 2 target is valid, message is NULL, interval > 0, count > 0
298 @results InitCheck() should return B_BAD_VALUE.
299 GetInfo() should return B_BAD_VALUE.
301 void TBMessageRunnerTester::BMessageRunnerB2()
303 // R5: chrashes when passing a NULL message
305 MessageRunnerTestApp
app(kTesterSignature
);
306 MessageRunnerTestLooper
*looper
= app
.TestLooper();
307 BMessenger
target(looper
);
308 bigtime_t interval
= 100000;
310 MessageRunnerTestHandler
*handler
= app
.TestHandler();
311 BMessenger
replyTo(handler
);
312 BMessageRunner
runner(target
, NULL
, interval
, count
, replyTo
);
313 CHK(runner
.InitCheck() == B_BAD_VALUE
);
314 check_message_runner_info(runner
, B_BAD_VALUE
);
319 BMessageRunner(BMessenger target, const BMessage *message,
320 bigtime_t interval, int32 count, BMessenger replyTo)
321 @case 3 target is valid, message is valid, interval == 0, count > 0
322 @results R5: InitCheck() should return B_ERROR.
323 GetInfo() should return B_BAD_VALUE.
324 OBOS: InitCheck() should return B_OK.
325 GetInfo() should return B_OK.
326 A minimal time interval is used (50000).
328 void TBMessageRunnerTester::BMessageRunnerB3()
330 MessageRunnerTestApp
app(kTesterSignature
);
331 MessageRunnerTestLooper
*looper
= app
.TestLooper();
332 BMessenger
target(looper
);
333 BMessage
message(MSG_RUNNER_MESSAGE
);
334 bigtime_t interval
= 0;
336 MessageRunnerTestHandler
*handler
= app
.TestHandler();
337 BMessenger
replyTo(handler
);
338 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
340 CHK(runner
.InitCheck() == B_ERROR
);
341 check_message_runner_info(runner
, B_BAD_VALUE
);
343 bigtime_t startTime
= system_time();
344 CHK(runner
.InitCheck() == B_OK
);
345 interval
= max(interval
, kMinTimeInterval
);
346 check_message_runner_info(runner
, B_OK
, interval
, count
);
347 snooze((count
+ 1) * interval
+ 10000);
348 CHK(looper
->CheckMessages(startTime
, interval
, count
));
349 CHK(app
.CountReplies() == 0);
350 CHK(handler
->CountReplies() == count
);
355 BMessageRunner(BMessenger target, const BMessage *message,
356 bigtime_t interval, int32 count, BMessenger replyTo)
357 @case 4 target is valid, message is valid, interval < 0, count > 0
358 @results InitCheck() should return B_OK.
359 GetInfo() should return B_OK.
360 A minimal time interval is used (50000).
362 void TBMessageRunnerTester::BMessageRunnerB4()
364 MessageRunnerTestApp
app(kTesterSignature
);
365 MessageRunnerTestLooper
*looper
= app
.TestLooper();
366 BMessenger
target(looper
);
367 BMessage
message(MSG_RUNNER_MESSAGE
);
368 bigtime_t interval
= -1;
370 MessageRunnerTestHandler
*handler
= app
.TestHandler();
371 BMessenger
replyTo(handler
);
372 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
373 bigtime_t startTime
= system_time();
374 CHK(runner
.InitCheck() == B_OK
);
375 interval
= max(interval
, kMinTimeInterval
);
376 check_message_runner_info(runner
, B_OK
, interval
, count
);
377 snooze((count
+ 1) * interval
+ 10000);
378 CHK(looper
->CheckMessages(startTime
, interval
, count
));
379 CHK(app
.CountReplies() == 0);
380 CHK(handler
->CountReplies() == count
);
384 BMessageRunner(BMessenger target, const BMessage *message,
385 bigtime_t interval, int32 count, BMessenger replyTo)
386 @case 5 target is valid, message is valid,
387 interval == LONGLONG_MAX, count > 0
388 @results InitCheck() should return B_OK.
389 GetInfo() should return B_OK.
390 No message should be delivered.
392 void TBMessageRunnerTester::BMessageRunnerB5()
394 // R5: doesn't behave very well. In worst case registrar time loop gets
395 // locked up and system wide message runners don't get messages anymore.
397 MessageRunnerTestApp
app(kTesterSignature
);
398 MessageRunnerTestLooper
*looper
= app
.TestLooper();
399 BMessenger
target(looper
);
400 BMessage
message(MSG_RUNNER_MESSAGE
);
401 bigtime_t interval
= LONGLONG_MAX
;
403 MessageRunnerTestHandler
*handler
= app
.TestHandler();
404 BMessenger
replyTo(handler
);
405 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
406 bigtime_t startTime
= system_time();
407 CHK(runner
.InitCheck() == B_OK
);
408 interval
= max(interval
, kMinTimeInterval
);
409 check_message_runner_info(runner
, B_OK
, interval
, count
);
411 CHK(looper
->CheckMessages(startTime
, interval
, 0));
412 CHK(app
.CountReplies() == 0);
413 CHK(handler
->CountReplies() == 0);
418 BMessageRunner(BMessenger target, const BMessage *message,
419 bigtime_t interval, int32 count, BMessenger replyTo)
420 @case 6 target is valid, message is valid, interval > 0, count == 0
421 @results InitCheck() should return B_ERROR.
422 GetInfo() should return B_BAD_VALUE.
424 void TBMessageRunnerTester::BMessageRunnerB6()
426 MessageRunnerTestApp
app(kTesterSignature
);
427 MessageRunnerTestLooper
*looper
= app
.TestLooper();
428 BMessenger
target(looper
);
429 BMessage
message(MSG_RUNNER_MESSAGE
);
430 bigtime_t interval
= 100000;
432 MessageRunnerTestHandler
*handler
= app
.TestHandler();
433 BMessenger
replyTo(handler
);
434 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
435 CHK(runner
.InitCheck() == B_ERROR
);
436 check_message_runner_info(runner
, B_BAD_VALUE
);
440 BMessageRunner(BMessenger target, const BMessage *message,
441 bigtime_t interval, int32 count, BMessenger replyTo)
442 @case 7 target is valid, message is valid, interval > 0, count < 0
443 @results InitCheck() should return B_OK.
444 GetInfo() should return B_OK.
445 Unlimited number of messages.
447 void TBMessageRunnerTester::BMessageRunnerB7()
449 MessageRunnerTestApp
app(kTesterSignature
);
450 MessageRunnerTestLooper
*looper
= app
.TestLooper();
451 BMessenger
target(looper
);
452 BMessage
message(MSG_RUNNER_MESSAGE
);
453 bigtime_t interval
= 100000;
455 MessageRunnerTestHandler
*handler
= app
.TestHandler();
456 BMessenger
replyTo(handler
);
457 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
458 bigtime_t startTime
= system_time();
459 CHK(runner
.InitCheck() == B_OK
);
460 interval
= max(interval
, kMinTimeInterval
);
461 check_message_runner_info(runner
, B_OK
, interval
, count
);
462 int32 checkCount
= 5;
463 snooze(checkCount
* interval
+ 10000);
464 CHK(looper
->CheckMessages(startTime
, interval
, checkCount
));
465 CHK(app
.CountReplies() == 0);
466 CHK(handler
->CountReplies() == checkCount
);
470 BMessageRunner(BMessenger target, const BMessage *message,
471 bigtime_t interval, int32 count, BMessenger replyTo)
472 @case 8 target is valid, message is valid, interval > 0, count > 0
473 @results InitCheck() should return B_OK.
474 GetInfo() should return B_OK.
475 count messages are sent.
477 void TBMessageRunnerTester::BMessageRunnerB8()
479 MessageRunnerTestApp
app(kTesterSignature
);
480 MessageRunnerTestLooper
*looper
= app
.TestLooper();
481 BMessenger
target(looper
);
482 BMessage
message(MSG_RUNNER_MESSAGE
);
483 bigtime_t interval
= 100000;
485 MessageRunnerTestHandler
*handler
= app
.TestHandler();
486 BMessenger
replyTo(handler
);
487 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
488 bigtime_t startTime
= system_time();
489 CHK(runner
.InitCheck() == B_OK
);
490 interval
= max(interval
, kMinTimeInterval
);
491 check_message_runner_info(runner
, B_OK
, interval
, count
);
492 snooze((count
+ 1) * interval
+ 10000);
493 CHK(looper
->CheckMessages(startTime
, interval
, count
));
494 CHK(app
.CountReplies() == 0);
495 CHK(handler
->CountReplies() == count
);
499 BMessageRunner(BMessenger target, const BMessage *message,
500 bigtime_t interval, int32 count, BMessenger replyTo)
501 @case 9 target is valid, message is valid, interval > 0, count > 0,
503 @results InitCheck() should return B_OK.
504 GetInfo() should return B_OK.
505 count messages are sent. The replies go to the registrar!
507 void TBMessageRunnerTester::BMessageRunnerB9()
509 MessageRunnerTestApp
app(kTesterSignature
);
510 MessageRunnerTestLooper
*looper
= app
.TestLooper();
511 BMessenger
target(looper
);
512 BMessage
message(MSG_RUNNER_MESSAGE
);
513 bigtime_t interval
= 100000;
515 MessageRunnerTestHandler
*handler
= app
.TestHandler();
516 BMessenger
replyTo(handler
);
517 BMessageRunner
runner(target
, &message
, interval
, count
, replyTo
);
518 bigtime_t startTime
= system_time();
519 CHK(runner
.InitCheck() == B_OK
);
520 interval
= max(interval
, kMinTimeInterval
);
521 check_message_runner_info(runner
, B_OK
, interval
, count
);
522 snooze((count
+ 1) * interval
+ 10000);
523 CHK(looper
->CheckMessages(startTime
, interval
, count
));
524 CHK(app
.CountReplies() == 0);
525 CHK(handler
->CountReplies() == count
);
529 Test
* TBMessageRunnerTester::Suite()
531 TestSuite
* SuiteOfTests
= new TestSuite
;
533 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
535 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
537 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
539 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
541 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
543 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
545 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
547 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
550 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
552 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
554 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
556 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
558 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
560 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
562 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
564 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,
566 ADD_TEST4(BMessageRunner
, SuiteOfTests
, TBMessageRunnerTester
,