9#include <boost/test/unit_test.hpp>
21 std::lock_guard<std::mutex> lock(mutex);
24 auto noTime = std::chrono::steady_clock::time_point::min();
46 int counter[10] = { 0 };
52 auto start = std::chrono::steady_clock::now();
54 std::chrono::steady_clock::time_point
first,
last;
58 for (
int i = 0; i < 100; ++i) {
59 auto t = now + std::chrono::microseconds(
randomMsec(rng));
75 for (
int i = 0; i < 5; i++)
79 now = std::chrono::steady_clock::now();
82 for (
int i = 0; i < 5; i++)
84 for (
int i = 0; i < 100; i++) {
85 auto t = now + std::chrono::microseconds(
randomMsec(rng));
98 if (thread.joinable()) thread.join();
102 for (
int i = 0; i < 10; i++) {
111 std::condition_variable
condvar;
115 const auto no_wait = [&](
const std::chrono::seconds& d) {
116 return condvar.wait_until(lock, std::chrono::steady_clock::now() - d);
139 std::vector<std::thread>
threads;
141 for (
int i = 0; i < 5; ++i) {
152 for (
int i = 0; i < 100; ++i) {
167 if (thread.joinable()) thread.join();
188 std::chrono::steady_clock::time_point
first,
last;
198 scheduler.
scheduleFromNow([&scheduler] { scheduler.
stop(); }, std::chrono::milliseconds{1});
209 auto now = std::chrono::steady_clock::now();
210 int delta = std::chrono::duration_cast<std::chrono::seconds>(
first - now).count();
Simple class for background tasks that should be run periodically or once "after a while".
void MockForward(std::chrono::seconds delta_seconds) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Mock the scheduler to fast forward in time.
void serviceQueue() EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Services the queue 'forever'.
size_t getQueueInfo(std::chrono::steady_clock::time_point &first, std::chrono::steady_clock::time_point &last) const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Returns number of tasks waiting to be serviced, and first and last task times.
std::function< void()> Function
void StopWhenDrained() EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Tell any threads running serviceQueue to stop when there is no work left to be done.
void stop() EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Tell any threads running serviceQueue to stop as soon as the current task is done.
void scheduleFromNow(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Call f once after the delta has passed.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(manythreads)
static void microTask(CScheduler &s, std::mutex &mutex, int &counter, int delta, std::chrono::steady_clock::time_point rescheduleTime)
#define WAIT_LOCK(cs, name)
void UninterruptibleSleep(const std::chrono::microseconds &n)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.