the Compartmented Robust Posix C++ Unit Test system

DEADLINE_REALTIME_MS(n)

A test modifier to set the maximum real-time (in milliseconds) a test is allowed before it is considered failed. If the real-time consumed is vastly longer, the test process is killed.

Used in: The modifier list of a test definition. See TEST(name, ...)

[Note]Note
The time measured is the running of the test function only. For deadlines on construction and destruction time for fixtures, see FIXTURE_CONSTRUCTION_DEADLINE_REALTIME_MS(ms) and FIXTURE_DESTRUCTION_DEADLINE_REALTIME_MS(ms). See also TEST(name, ...) for information about fixtures.
[Note]Note
DEADLINE_REALTIME_MS(ms) cannot be used together with EXPECT_REALTIME_TIMEOUT_MS(ms).

By default, all tests have a read-time deadline of 2 seconds, to catch accidental infinite loops. Should your test require more, you can set it higher.

Example: The test program

     
     #include <crpcut.hpp>
     extern "C"
     {
     #include "unistd.h"
     }
     
     TEST(test_killed, DEADLINE_REALTIME_MS(2))
     {
       for (;;)
         ;
     }
     
     TEST(slight_overdraw, DEADLINE_REALTIME_MS(2))
     {
       usleep(5000);
     }
     
     TEST(quick_function, DEADLINE_CPU_MS(2))
     {
       usleep(1000);
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

reports two failed test:


     FAILED!: test_killed
     phase="running"  --------------------------------------------------------------
     samples/deadline_realtime.cpp:34
     Timed out - killed
     Expected normal exit
     -------------------------------------------------------------------------------
     ===============================================================================
     FAILED!: slight_overdraw
     phase="destroying"  -----------------------------------------------------------
     samples/deadline_realtime.cpp:40
     Realtime timeout 2ms exceeded.
       Actual time to completion was 5ms
     -------------------------------------------------------------------------------
     ===============================================================================
     3 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       2          2              0

[Note]Note
When the time consumed is longer than allowed, but not long long enough to get the test process killed, the reported phase is destroying.

See also DEADLINE_CPU_MS(n) for a deadline in CPU-time rather than real-time. Both can be combined in the modifier list for the same TEST(name, ...).

[Note]Note
It is possible to extend the timeout limit using the command line parameter --timeout-multiplier=factor, which can be useful when running the test program with time consuming tools like valgrind.
[Note]Note
It is also possible to temporarily disable the deadline with the -t / --disable-timeouts command line flag.