Jan Kundrát | a33cf08 | 2019-03-28 11:55:57 +0100 | [diff] [blame] | 1 | #include <doctest/doctest.h> |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 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 */ |
| 7 | void 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 | } |