transm/test/assistant.h

58 lines
1.6 KiB
C++

#ifndef ASSISTANT_H
#define ASSISTANT_H
#include <fstream>
#include <random>
#include <string>
#include <functional>
#ifdef USE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
bool random_file(const std::string& file, size_t size);
/**
* @brief 等待变量的值达到预期值
*
* @tparam T 变量类型
* @tparam Compare 比较器类型
* @param value 变量的引用
* @param expected 预期值
* @param comparator 比较函数 (默认为 std::equal_to)
* @param timeout_ms 超时时间(毫秒),0表示无限等待
* @param interval_ms 检查间隔(毫秒)
* @return true 变量值符合预期
* @return false 超时或条件不满足
*/
template <typename T, typename Compare = std::equal_to<T>>
bool value_wait(const std::function<T()>& value_ref, const T& expected, Compare comparator = Compare(),
unsigned long timeout_ms = 0, unsigned long interval_ms = 100)
{
auto start = std::chrono::steady_clock::now();
while (true) {
T value = value_ref();
if (comparator(value, expected)) {
return true;
}
if (timeout_ms > 0) {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
if (elapsed >= timeout_ms) {
return false;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
}
}
#endif // ASSISTANT_H