How is unit test callback function? (C ++ Boost Unit test)

I want a unit test function client / server. The client calls the server, the server calls the callback function. Sort of:

void CallBack()
{
    BOOST_SUCCESS(); // test is successful if this is called
}

BOOST_AUTO_TEST_CASE( ConnectionTest_ClientCallback )
{
    CallServer(); // server will do work and call CallBack()
    sleep(20);
    BOOST_FAIL("Server hasn't called CallBack() within specified time limit.");
}

But the above will not work, because CallBack () can be called during any of the tests. Is there a better way to do this?

+3
source share
1 answer

The variable is set in the callback function:

void CallBack()
{
    callBackCalled = true;
}

And check that in the test:

BOOST_AUTO_TEST_CASE( ConnectionTest_ClientCallback )
{
    callbackCalled = false;
    CallServer(); // server will do work and call CallBack()
    sleep(20);
    if (callbackCalled)
        BOOST_SUCCESS();
    else
        BOOST_FAIL("Server hasn't called CallBack() within specified time limit.");
}

Edit: the best solution offered by kizzx2:

BOOST_AUTO_TEST_CASE( ConnectionTest_ClientCallback )
{
    callbackCalled = false;
    CallServer(); // server will do work and call CallBack()
    sleep(20);
    BOOST_CHECK_MESSAGE(callbackCalled, "Server hasn't called CallBack() within specified time limit.");
}
+2
source

Source: https://habr.com/ru/post/1778036/


All Articles