the Compartmented Robust Posix C++ Unit Test system

VERIFY_SCOPE_MIN_REALTIME_MS(ms)

Verify that the block of code immediately following the macro consumes at least 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 requires at least ms milliseconds of real-time to complete its execution.

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_MIN_REALTIME_MS(ms)

Example: The test program

     
     #include <crpcut.hpp>
     extern "C"
     {
     #  include <unistd.h>
     #  include <sys/times.h>
     }
     TEST(sleeps)
     {
       VERIFY_SCOPE_MIN_REALTIME_MS(5)
       {
         VERIFY_SCOPE_MAX_CPUTIME_MS(1)
         {
           for (int i = 0; i < 5; ++i)
           {
             usleep(1000); // would fail if implemented as busy wait
           }
         }
       }
     }
     
     TEST(too_short)
     {
       VERIFY_SCOPE_MIN_REALTIME_MS(5)
       {
         VERIFY_SCOPE_MAX_CPUTIME_MS(1)
         {
           for (int i = 0; i < 3; ++i)
           {
             usleep(1000); // would fail if implemented as busy wait
           }
         }
       }
     }
     
     TEST(busy_wait)
     {
       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_CPUTIME_MS(1)
       {
         VERIFY_SCOPE_MIN_REALTIME_MS(50)
         {
           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 two failed tests:


     FAILED!: busy_wait
     fail---------------------------------------------------------------------------
     samples/verify_scope_min_real.cpp:68
     VERIFY_SCOPE_MAX_CPUTIME_MS(1)
     Actual time used was 59ms
     -------------------------------------------------------------------------------
     phase="running"  --------------------------------------------------------------
     samples/verify_scope_min_real.cpp:62
     Earlier VERIFY failed
     -------------------------------------------------------------------------------
     ===============================================================================
     3 test cases selected
     
                    Sum   Critical   Non-critical
     PASSED   :       2          2              0
     FAILED   :       1          1              0