Class AsyncTest
Defined in File async_test.h
Class Documentation
-
class AsyncTest
Test class that allows the handling of asynchronous test objects.
The class provides the two basic functions AsyncTest::barricade and AsyncTest::triggerClearEvent. During the test setup gates between the steps with one or more clear events. Allow passing on by calling triggerClearEvent after a test.
Usage: Suppose you want to test a function that calls another function asynchronously, like the following example:
void asyncCall(std::function<void()> fun) { std::thread t(fun); t.detach(); }
You expect that fun gets called, so your test thread has to wait for the completion, else it would fail. This can be achieved via:
class MyTest : public testing::Test, public testing::AsyncTest { public: MOCK_METHOD0(myMethod, void()); }; TEST_F(MyTest, testCase) { EXPECT_CALL(*this, myMethod()).Times(1).WillOnce(ACTION_OPEN_BARRIER_VOID("myMethod")); const int timeout_ms {100}; asyncCall([this]{ myMethod(); }); BARRIER("myMethod", timeout_ms) << "Timed-out waiting for myMethod call."; }
Public Functions
-
inline void triggerClearEvent(const std::string &event)
Triggers a clear event. If a call to barricade is currently pending it will unblock as soon as all clear events are triggered. Else the event is put on the waitlist. This waitlist is emptied upon a call to barricade.
- Parameters:
event – The event that is triggered
-
inline bool barricade(const std::string &clear_event, const int timeout_ms = -1)
Will block until the event given by clear_event is triggered or a timeout is reached. Unblocks immediately, if the event was on the waitlist.
- Parameters:
clear_event – Event that allows the test to pass on
timeout_ms – Timeout [ms] (optional).
- Returns:
True if the event was triggered, false otherwise.
-
inline bool barricade(std::initializer_list<std::string> clear_events, const int timeout_ms = -1)
Will block until all events given by clear_events are triggered or a timeout is reached. Events on the waitlist are taken into account, too.
- Parameters:
clear_events – List of events that allow the test to pass on
timeout_ms – Timeout [ms] (optional).
- Returns:
True if all events were triggered, false otherwise.
-
inline void triggerClearEvent(const std::string &event)