Tomáš Pecka | 0a2e890 | 2020-06-09 21:11:20 +0200 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <doctest/doctest.h> |
| 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 (true) { |
| 22 | { |
| 23 | auto lock = trompeloeil::get_lock(); |
| 24 | if (seq.is_completed()) { |
| 25 | break; |
| 26 | } |
| 27 | } |
| 28 | std::this_thread::sleep_for(waitingStep); |
| 29 | if (clock::now() - start > completionTimeout) { |
| 30 | break; |
| 31 | } |
| 32 | } |
| 33 | { |
| 34 | auto lock = trompeloeil::get_lock(); |
| 35 | REQUIRE(seq.is_completed()); |
| 36 | } |
| 37 | auto duration = std::chrono::duration<double>(clock::now() - start); |
| 38 | std::this_thread::sleep_for(std::max(duration, decltype(duration)(minExtraWait))); |
| 39 | } |