Closed calder closed 5 years ago
@calder Thank you for this proposal. Could you please provide more details, potential use case, and most importantly how are you accomplishing this "today"? Thanks again
@gennadiycivil: Thanks! Here's an example use case:
Let's say we want to test the behavior of Foo::DoSomething()
.
Foo
takes a Bar
in its constructor.Foo::DoSomething()
calls Bar::DoSomethingElse()
asynchronously (via std::async
for example).If Bar::DoSomethingElse()
were called synchronously, we would just create a mock Bar
, pass it to Foo
's constructor, set up expectations on the Bar
mock, and call Foo::DoSomething()
.
Instead, because it's asynchronous, after calling Foo::DoSomething()
we have to either:
Bar::DoSomethingElse()
to probably have been called. This is undesirable because it slows down tests significantly, makes them brittle to timing changes, and introduces potential flakes.So far I have usually opted for "sleep synchronization" with all its associated problems.
@gennadiycivil: Does https://github.com/abseil/googletest/pull/1991 look like roughly the right direction?
@calder @gennadiycivil This would be a very useful feature for testing in ROS, where we have asynchronous callbacks (http://wiki.ros.org/roscpp/Overview/Publishers%20and%20Subscribers).
Actually, there is another workaround: You can wake up a sleeping thread when the mock function is called defining an appropriate action. https://github.com/PilzDE/pilz_robots/blob/5a969f06e6b7626e2ddb3c485f0a5ca645bbaff4/prbt_hardware_support/test/include/prbt_hardware_support/async_test.h
See #1991
Would it be reasonable to add the following helper to
gmock-spec-builders
for testing functions with asynchronous side effects?