2020-02-20 16:15:13 +00:00
|
|
|
#include "webfuse/utils/timeout_watcher.hpp"
|
2020-02-19 21:44:56 +00:00
|
|
|
#include <stdexcept>
|
2020-06-13 21:47:52 +00:00
|
|
|
#include <thread>
|
2020-02-19 21:44:56 +00:00
|
|
|
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using std::chrono::duration_cast;
|
|
|
|
using std::chrono::steady_clock;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
milliseconds now()
|
|
|
|
{
|
|
|
|
return duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace webfuse_test
|
|
|
|
{
|
|
|
|
|
|
|
|
TimeoutWatcher::TimeoutWatcher(milliseconds timeout)
|
|
|
|
: startedAt(now())
|
|
|
|
, timeout_(timeout)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeoutWatcher::~TimeoutWatcher()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TimeoutWatcher::isTimeout()
|
|
|
|
{
|
|
|
|
return (now() - startedAt) > timeout_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimeoutWatcher::check()
|
|
|
|
{
|
|
|
|
if (isTimeout())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("timeout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 21:47:52 +00:00
|
|
|
bool TimeoutWatcher::waitUntil(std::function<bool()> predicate)
|
|
|
|
{
|
|
|
|
bool result = predicate();
|
|
|
|
while ((!result) && (!isTimeout()))
|
|
|
|
{
|
|
|
|
std::this_thread::yield();
|
|
|
|
result = predicate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-19 21:44:56 +00:00
|
|
|
}
|