the Compartmented Robust Posix C++ Unit Test system

ASSERT_SCOPE_MAX_REALTIME_MS(ms)

Assert that the block of code immediately following the macro does not consume more than ms milliseconds real time.

Used in: A test function body, the constructor or destructor of a fixture, or a function called from them. See TEST(name, ...).

Requirement: ms is an unsigned integer value.

Requirement: A code block follows immediately after.

The assertion succeeds if, and only if, the code block following completes its execution in at most ms milliseconds of real-time.

On success the test function continues without side effects.

On failure, the test is terminated with an error report. The report includes the parameter value ms and the actual time consumed.

[Note]Note
It is possible to temporarily disable the assertion with the -t / --disable-timeouts command line flag, in which case the assertion will always succeed, regardless of the amount of CPU-time consumed by the following code block.
[Note]Note
It is also 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 possible to temporarily disable the assertion with the -t / --disable-timeouts command line flag, in which case the assertion will always succeed, regardless of the amount of time consumed by the following code block. This can be useful when running with time consuming tools like valgrind.
[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 assertion with the -t / --disable-timeouts command line flag, in which case the assertion will always succeed, regardless of the amount of time consumed by the following code block.

See also: VERIFY_SCOPE_MAX_REALTIME_MS(ms).

Example: The test program

     
     #include <crpcut.hpp>
     extern "C"
     {
     #  include <unistd.h>
     #  include <sys/times.h>
     }
     TEST(long_real_time)
     {
       ASSERT_SCOPE_MAX_REALTIME_MS(3)
       {
         for (int i = 0; i < 5; ++i)
           {
             usleep(1000); // would fail if implemented as busy wait
           }
       }
     }
     
     TEST(short_real_time)
     {
       const clock_t clocks_per_tick = sysconf(_SC_CLK_TCK);
       tms t;
       times(&t);
       clock_t deadline = t.tms_utime + t.tms_stime + clocks_per_tick/20;
       ASSERT_SCOPE_MAX_REALTIME_MS(100)
       {
         for (;;)
           {
             for (volatile int n = 0; n < 100000; ++n)
               ;
             times(&t);
             if (t.tms_utime + t.tms_stime > deadline) break;
           }
       }
     }
     
     int main(int argc, char *argv[])
     {
       return crpcut::run(argc, argv);
     }

    

reports one failed test:


     FAILED!: long_real_time
     phase="running"  --------------------------------------------------------------
     samples/assert_scope_max_real.cpp:36
     ASSERT_SCOPE_MAX_REALTIME_MS(3)
     Actual time used was 5ms
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0