the Compartmented Robust Posix C++ Unit Test system


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

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

The CPU-time consumption is enforced using setrlimit() with RLIMIT_CPU. If set it to something else inside the test, or if the SIGXCPU signal is handled by the test, crpcut may be unable to enforce the limit. crpcut will still be able to measure the consumed CPU-time, however, and report violations.
The time measured is the running of the test function only. Construction and destruction time are excluded (see TEST(name, ...) for information about fixtures.)

Example: The test program

     #include <crpcut.hpp>
     extern "C"
     #include "unistd.h"
     TEST(test_killed, DEADLINE_CPU_MS(2))
       for (;;)
     TEST(test_succeeds_long_wait, DEADLINE_CPU_MS(2))
       usleep(50000); // realtime passes, not cpu-time since process is inactive
     int main(int argc, char *argv[])
       return crpcut::run(argc, argv);

reports one failed test:

     FAILED!: test_killed
     /tmp/crpcutIZLgpU/test_killed is not empty!
     phase="running"  --------------------------------------------------------------
     Died with core dump
     Files remain under /tmp/crpcutIZLgpU
     2 test cases selected
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0

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

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.
It is also possible to temporarily disable the deadline with the -t / --disable-timeouts command line flag.