1 // MessageRunnerTestHelpers.cpp
7 #include "MessageRunnerTestHelpers.h"
14 MessageRunnerTestHandler::MessageRunnerTestHandler()
15 : BHandler("message runner test handler"),
21 MessageRunnerTestHandler::~MessageRunnerTestHandler()
27 MessageRunnerTestHandler::MessageReceived(BMessage
*message
)
29 switch (message
->what
) {
37 ///////////////////////////
38 // MessageRunnerTestLooper
40 struct MessageRunnerTestLooper::MessageInfo
{
45 MessageRunnerTestLooper::MessageRunnerTestLooper()
52 MessageRunnerTestLooper::~MessageRunnerTestLooper()
54 for (int32 i
= 0; MessageInfo
*info
= MessageInfoAt(i
); i
++)
60 MessageRunnerTestLooper::MessageReceived(BMessage
*message
)
62 switch (message
->what
) {
63 case MSG_RUNNER_MESSAGE
:
65 MessageInfo
*info
= new MessageInfo
;
66 info
->time
= system_time();
67 fMessageInfos
.AddItem(info
);
68 message
->SendReply(MSG_REPLY
);
76 MessageRunnerTestLooper::CheckMessages(bigtime_t startTime
, bigtime_t interval
,
79 return CheckMessages(0, startTime
, interval
, count
);
84 MessageRunnerTestLooper::CheckMessages(int32 skip
, bigtime_t startTime
,
85 bigtime_t interval
, int32 count
)
87 BAutolock
_lock(this);
88 bool result
= (fMessageInfos
.CountItems() == count
+ skip
);
90 printf("message counts don't match: %ld vs. %ld\n", fMessageInfos
.CountItems(),
93 for (int32 i
= 0; result
&& i
< count
; i
++) {
94 MessageInfo
*info
= MessageInfoAt(i
+ skip
);
95 bigtime_t expectedTime
= startTime
+ (i
+ 1) * interval
;
96 result
= (expectedTime
- JITTER
< info
->time
97 && info
->time
< expectedTime
+ JITTER
);
99 printf("message out of time: %lld vs. %lld\n", info
->time
, expectedTime
);
105 MessageRunnerTestLooper::MessageInfo
*
106 MessageRunnerTestLooper::MessageInfoAt(int32 index
) const
108 return static_cast<MessageInfo
*>(fMessageInfos
.ItemAt(index
));
112 ////////////////////////
113 // MessageRunnerTestApp
116 MessageRunnerTestApp::MessageRunnerTestApp(const char *signature
)
117 : BApplication(signature
),
124 fLooper
= new MessageRunnerTestLooper
;
127 fHandler
= new MessageRunnerTestHandler
;
128 AddHandler(fHandler
);
129 // start out message loop in a different thread
131 fThread
= spawn_thread(_ThreadEntry
, "message runner app thread",
132 B_NORMAL_PRIORITY
, this);
133 resume_thread(fThread
);
137 MessageRunnerTestApp::~MessageRunnerTestApp()
142 // shut down our message loop
144 PostMessage(B_QUIT_REQUESTED
);
146 wait_for_thread(fThread
, &dummy
);
147 // delete the handler
149 RemoveHandler(fHandler
);
155 MessageRunnerTestApp::MessageReceived(BMessage
*message
)
157 switch (message
->what
) {
166 MessageRunnerTestApp::QuitRequested()
173 MessageRunnerTestApp::_ThreadEntry(void *data
)
175 MessageRunnerTestApp
*app
= static_cast<MessageRunnerTestApp
*>(data
);