Main Content

matlab.unittest.constraints.Eventually class

Package: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.Constraint

Poll for value to asynchronously satisfy constraint

Construction

outConstObj = Eventually(constObj) creates a constraint, outConstObj, that polls for an actual value returned from a function handle to asynchronously satisfy the constObj constraint. It is not satisfied if evaluation of the function handle does not produce a value that satisfies the constraint within 20 seconds. The testing framework invokes the drawnow function while the Eventually constraint waits for specified function to satisfy the constraint.

outConstObj = Eventually(constObj,'WithTimeoutOf',timeOutVal) creates a constraint that polls for the constraint to be satisfied within the timer period specified in timeOutVal.

Input Arguments

constObj

Constraint instance

timeOutVal

Maximum time to attempt to produce passing behavior, specified in seconds

Default: 20 seconds

Properties

FinalReturnValue

Output produced when the test framework invokes the supplied function handle. This property is read only and is set when the test framework invokes the function handle.

Timeout

Maximum time to attempt to produce passing behavior, specified by the timeOutVal input argument

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
import matlab.unittest.constraints.IsLessThan

testCase = TestCase.forInteractiveUse;

Verify that, within the timeout period, a call to toc results in a value greater than 10 (seconds). The Eventually constraint repeatedly calls toc until either the constraint is satisfied or the elapsed time exceeds the timeout period. Repeated calls to toc result in the elapsed time since the last call to tic.

tic
testCase.verifyThat(@toc, Eventually(IsGreaterThan(10)))
Interactive verification passed.

The verification may take as long as 10 seconds for toc to reach a passing value. If you issue the call to tic and wait more than 10 seconds before issuing the verifyThat command, the verification returns immediately since toc already returns a value greater than 10.

Verify that, within the timeout period, toc does not return a negative value.

testCase.verifyThat(@toc, Eventually(IsLessThan(0)))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Eventually failed.
--> The constraint never passed with a timeout of 20 second(s).
--> IsLessThan failed.
    --> The value must be less than the maximum value.
    
    Actual Value:
      36.532254706346720
    Maximum Value (Exclusive):
         0

Evaluated Function:
  function_handle with value:

    @toc

This failure is expected since elapsed time is not going to be less than zero. However, Eventually polls toc for the duration of the timeout period.

Adjust the timeout period so Eventually polls for 5 seconds.

tic
testCase.verifyThat(@toc, Eventually(IsGreaterThan(10), ...
    'WithTimeoutOf', 5))
Interactive verification failed.

---------------------
Framework Diagnostic:
---------------------
Eventually failed.
--> The constraint never passed with a timeout of 5 second(s).
--> IsGreaterThan failed.
    --> The value must be greater than the minimum value.
    
    Actual Value:
       5.143138452046230
    Minimum Value (Exclusive):
        10

Evaluated Function:
  function_handle with value:

    @toc

If you didn’t wait more than 5 seconds between calls to tic and verifyThat, the test fails because the elapsed time is not greater than 10 seconds within the modified timeout period.