the Compartmented Robust Posix C++ Unit Test system

VERIFY_SCOPE_MAX_REALTIME_MS(ms)

Verify 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 check 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 marked as failed, but continues execution with an error report. The report includes the parameter value ms and the actual time consumed.

[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 check 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: ASSERT_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)
     {
       VERIFY_SCOPE_MAX_REALTIME_MS(3)
       {
         for (int i = 0; i < 5; ++i)
           {
             usleep(1000); // would fail if implemented as busy wait
           }
       }
       INFO << "after violation";
     }
     
     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;
       VERIFY_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 followed by an INFO log entry:


     FAILED!: long_real_time
     fail---------------------------------------------------------------------------
     samples/verify_scope_max_real.cpp:36
     VERIFY_SCOPE_MAX_REALTIME_MS(3)
     Actual time used was 5ms
     -------------------------------------------------------------------------------
     info---------------------------------------------------------------------------
     samples/verify_scope_max_real.cpp:43
     after violation
     -------------------------------------------------------------------------------
     phase="running"  --------------------------------------------------------------
     samples/verify_scope_max_real.cpp:34
     Earlier VERIFY failed
     -------------------------------------------------------------------------------
     ===============================================================================
     2 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       1          1              0
     FAILED   :       1          1              0