rollbear / trompeloeil

Header only C++14 mocking framework
Boost Software License 1.0
802 stars 85 forks source link

Feature Request: Would like a built-in way to prevent expiring async mocks #303

Open Mskeffington opened 1 year ago

Mskeffington commented 1 year ago

considering the following code:

SECTION("test async function") {
        std::condition_variable cv;
        std::mutex cvMutex;

        REQUIRE_CALL(myMock, call_async())
            .LR_SIDE_EFFECT(cv.notify_all());

        // wrapper contains an instance of the mock which is called on the other side of an RPC
        wrapper.call_async();

        // In order to prevent timing out the REQUIRE_CALL, I need to wait for the call to end before the block exits. 
        std::unique_lock<std::mutex> lock(cvMutex);
        // specify a timeout of 1s.  This normally takes less than 1s during a successful test
        REQUIRE(cv.wait_for(lock, 1s) == std::cv_status::no_timeout);
}

I would love a way to wrap this very verbose functionality. Maybe like this:

SECTION("test async function") {
        REQUIRE_CALL(myMock, call_async())
            .ASYNC_TIMEOUT(1s);

        wrapper.call_async();
        // could also wrap the call in a macro if neccecary...
}