assertEventually

Checks a probe until the timeout expires. The assert error is produced if the probe fails to return 'true' before the timeout.

The parameter timeout determines the maximum timeout to wait before asserting a failure (default is 500ms).

The parameter delay determines how often the predicate will be checked (default is 10ms).

This kind of assertion is very useful to check on code that runs in another thread. For instance, the thread that listens to a socket.

  1. void assertEventually(T probe, Duration timeout, Duration delay, string msg, string file, size_t line)
  2. void assertEventually(bool delegate() probe, Duration timeout, Duration delay, string msg, string file, size_t line)
    void
    assertEventually
    (
    bool delegate
    ()
    probe
    ,
    Duration timeout = 500.msecs
    ,
    Duration delay = 10.msecs
    ,
    lazy string msg = null
    ,
    string file = __FILE__
    ,
    size_t line = __LINE__
    )
  3. void assertEventually(bool function() probe, Duration timeout, Duration delay, string msg, string file, size_t line)

Throws

AssertException when the probe fails to become true before timeout

Examples

// this should complete right after the first probe
assertEventually({ return true; });
// test using delegate implementation
assertEventually(delegate() { return true; });

assertEventually(
{ static count = 0; return ++count > 23; }, // make sure every slow/heavy-loaded computers/CI do this unittest
		// e.g.: Travis-CI due to high number of parallel jobs, can't do it in
		// time.
		1000.msecs,
		1.msecs
);

// this should never complete and eventually timeout.
auto exception = expectThrows!AssertException(assertEventually({ return false; }));

assertEquals("timed out", exception.msg);

Meta