blob: 029e36fdde34cb3b4c5fc73b1ea44dc930ede7cf [file] [log] [blame]
Jan Kundrátc381e632019-03-14 13:39:11 +01001#include <catch2/catch.hpp>
2#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}