You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
925 B
58 lines
925 B
4 years ago
|
#include "webfuse_provider/utils/timeout_watcher.hpp"
|
||
5 years ago
|
#include <stdexcept>
|
||
4 years ago
|
#include <thread>
|
||
5 years ago
|
|
||
|
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");
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
bool TimeoutWatcher::waitUntil(std::function<bool()> predicate)
|
||
|
{
|
||
|
bool result = predicate();
|
||
|
while ((!result) && (!isTimeout()))
|
||
|
{
|
||
|
std::this_thread::yield();
|
||
|
result = predicate();
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
5 years ago
|
}
|