What will be the best way to write (google) test cases using a google mock object and expect the EXPECT_CALL() definitions being called from another thread controlled by the
Fraser's answer inspired me also. I used his suggestion, and it worked, but then I found another way to accomplish the same without the condition variable. You'll need to add a method to check some condition, and you'll need an infinite loop. This is also assuming that you have a separate thread that will update the condition.
TEST_F(BarTest, DoSomethingWhenFunc2Gt0)
{
EXPECT_CALL(fooInterfaceMock,func1()).Times(1);
EXPECT_CALL(fooInterfaceMock,func2()).Times(1).WillOnce(Return(1));
bar.start();
bar.triggerDoSomething();
// How long of a wait is too long?
auto now = chrono::system_clock::now();
auto tooLong = now + std::chrono::milliseconds(50);
/* Expect your thread to update this condition, so execution will continue
* as soon as the condition is updated and you won't have to sleep
* for the remainder of the time
*/
while (!bar.condition() && (now = chrono::system_clock::now()) < tooLong)
{
/* Not necessary in all cases, but some compilers may optimize out
* the while loop if there's no loop body.
*/
this_thread::sleep_for(chrono::milliseconds(1));
}
// If the assertion fails, then time ran out.
ASSERT_LT(now, tooLong);
bar.stop();
}