blob: 8ec4f4b2b7be48c673c262042de5ba33dc87f5a6 [file] [log] [blame]
Jan Kundráta33cf082019-03-28 11:55:57 +01001#include <doctest/doctest.h>
Jan Kundrátc381e632019-03-14 13:39:11 +01002#include <chrono>
3#include <thread>
4#include <trompeloeil.hpp>
5
6/** @short Wait until a given sequence of expectation is matched, and then a bit more to ensure that there's silence afterwards */
7void waitForCompletionAndBitMore(const trompeloeil::sequence& seq)
8{
9 using namespace std::literals;
10 using clock = std::chrono::steady_clock;
11
12 // We're busy-waiting a bit
13 const auto waitingStep = 30ms;
14 // Timeout after this much
15 const auto completionTimeout = 5000ms;
16 // When checking for silence afterwards, wait at least this long.
17 // We'll also wait as long as it originally took to process everything.
18 const auto minExtraWait = 100ms;
19
20 auto start = clock::now();
21 while (!seq.is_completed()) {
22 std::this_thread::sleep_for(waitingStep);
23 if (clock::now() - start > completionTimeout) {
24 break;
25 }
26 }
27 REQUIRE(seq.is_completed());
28 auto duration = std::chrono::duration<double>(clock::now() - start);
29 std::this_thread::sleep_for(std::max(duration, decltype(duration)(minExtraWait)));
30}