使用谷歌模拟对象编写(谷歌)测试用例并期望从测试中的类控制的另一个线程调用 EXPECT_CALL() 定义的最佳方法是什么?在触发调用序列后简单地调用 sleep() 或类似方法并不合适,因为它可能会减慢不必要的测试并且可能不会真正达到计时条件。但是以某种方式完成测试用例必须等到模拟方法被调用。任何人的想法?
下面是一些代码来说明这种情况:
Bar.hpp(被测类)
class Bar
{
public:
Bar(IFooInterface* argFooInterface);
virtual ~Bar();
void triggerDoSomething();
void start();
void stop();
private:
void* barThreadMethod(void* userArgs);
void endThread();
void doSomething();
ClassMethodThread<Bar> thread; // A simple class method thread implementation using boost::thread
IFooInterface* fooInterface;
boost::interprocess::interprocess_semaphore semActionTrigger;
boost::interprocess::interprocess_semaphore semEndThread;
bool stopped;
bool endThreadRequested;
};
Bar.cpp(摘录):
void Bar::triggerDoSomething()
{
semActionTrigger.post();
}
void* Bar::barThreadMethod(void* userArgs)
{
(void)userArgs;
stopped = false;
do
{
semActionTrigger.wait();
if(!endThreadRequested && !semActionTrigger.try_wait())
{
doSomething();
}
} while(!endThreadRequested && !semEndThread.try_wait());
stopped = true;
return NULL;
}
void Bar::doSomething()
{
if(fooInterface)
{
fooInterface->func1();
if(fooInterface->func2() > 0)
{
return;
}
fooInterface->func3(5);
}
}
测试代码(摘录,到目前为止 FooInterfaceMock 的定义没有什么特别之处):
class BarTest : public ::testing::Test
{
public:
BarTest()
: fooInterfaceMock()
, bar(&fooInterfaceMock)
{
}
protected:
FooInterfaceMock fooInterfaceMock;
Bar bar;
};
TEST_F(BarTest, DoSomethingWhenFunc2Gt0)
{
EXPECT_CALL(fooInterfaceMock,func1())
.Times(1);
EXPECT_CALL(fooInterfaceMock,func2())
.Times(1)
.WillOnce(Return(1));
bar.start();
bar.triggerDoSomething();
//sleep(1);
bar.stop();
}
没有 sleep() 的测试结果:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.DoSomethingWhenFunc2Gt0
../test/BarTest.cpp:39: Failure
Actual function call count doesn't match EXPECT_CALL(fooInterfaceMock, func2())...
Expected: to be called once
Actual: never called - unsatisfied and active
../test/BarTest.cpp:37: Failure
Actual function call count doesn't match EXPECT_CALL(fooInterfaceMock, func1())...
Expected: to be called once
Actual: never called - unsatisfied and active
[ FAILED ] BarTest.DoSomethingWhenFunc2Gt0 (1 ms)
[----------] 1 test from BarTest (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] BarTest.DoSomethingWhenFunc2Gt0
1 FAILED TEST
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
Aborted
启用 sleep() 的测试结果:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from BarTest
[ RUN ] BarTest.DoSomethingWhenFunc2Gt0
[ OK ] BarTest.DoSomethingWhenFunc2Gt0 (1000 ms)
[----------] 1 test from BarTest (1000 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1000 ms total)
[ PASSED ] 1 test.
我想避免 sleep(),最好的情况是根本不需要更改 Bar 类。