Testing Delayed Actions with GTest
Testing Delayed Actions with GTest
I have scenario template like :
// this function is called as timer callback
void funcToBeCalled ();
Timer t;
void
push_data ()
if (t.isRunning ())
return;
t.start (oneshot = true);
//TestCase
EXPECT_CALL(mockObj,funcToBeCalled).Times(1);
push_data ();
push_data ();
push_data ();
Purpose is when push_data() is called n times in time period t it will be bound to timer callback so only once funcToBeCalled()
is called all push_data request after t time is ignored.
The above case is a prototype version and tests are succesfull.You can assume that mock setup is correct.
funcToBeCalled()
But I want to have a test case like :
#define TIMER_TIMEOUT 10
bool funcToBeCalled ();
Timer t;
void
push_data ()
if (t.isRunning ())
return;
t.start (TIMER_TIMEOUT, oneshot = true);
// TestCase
SharedSignal msgnal;
EXPECT_CALL (mockObj,
funcToBeCalled).Times (2).
WillOnce (Return (true)).WillOnce (SetSignal (msgnal));
push_data ();
push_data ();
push_data ();
Sleep (TIMER_TIMEOUT);
push_data ();
push_data ();
push_data ();
msgnal.wait (5 * TIMER_TIMEOUT);
Sleep ensures that first time window is expired second call to funcToBeCalled()
should be made.
funcToBeCalled()
I do not want to use verify Mock::VerifyAndClearExpectations
it does not simulate one long time window,instead it simulates two time windows from scratch.
Mock::VerifyAndClearExpectations
How can I achieve this test case with Google Test Framework?
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.