blob: a29f3a25c0494e14bcba8a84a52287106e43f83d [file] [log] [blame]
Jan Kundrátc381e632019-03-14 13:39:11 +01001#include <chrono>
Václav Kubernát90de9502019-11-20 17:19:44 +01002#include <doctest/doctest.h>
Jan Kundrátc381e632019-03-14 13:39:11 +01003#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}