Commit 1172173d authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Make FiberManager.batonTimedWaitTimeout less flaky

Reviewed By: yfeldblum

Differential Revision: D6957008

fbshipit-source-id: 848932827efaf8529513026b45446da2c1697253
parent 7ee12309
...@@ -32,6 +32,10 @@ class SimpleLoopController : public LoopController { ...@@ -32,6 +32,10 @@ class SimpleLoopController : public LoopController {
scheduled_ = false; scheduled_ = false;
} }
void setTimeFunc(Function<TimePoint()> timeFunc) {
timeFunc_ = std::move(timeFunc);
}
/** /**
* Run FiberManager loop; if no ready task are present, * Run FiberManager loop; if no ready task are present,
* run provided function. Stops after both stop() has been called * run provided function. Stops after both stop() has been called
...@@ -45,7 +49,7 @@ class SimpleLoopController : public LoopController { ...@@ -45,7 +49,7 @@ class SimpleLoopController : public LoopController {
while (LIKELY(waiting || !stopRequested_)) { while (LIKELY(waiting || !stopRequested_)) {
func(); func();
auto time = Clock::now(); auto time = timeFunc_();
for (size_t i = 0; i < scheduledFuncs_.size(); ++i) { for (size_t i = 0; i < scheduledFuncs_.size(); ++i) {
if (scheduledFuncs_[i].first <= time) { if (scheduledFuncs_[i].first <= time) {
...@@ -104,6 +108,7 @@ class SimpleLoopController : public LoopController { ...@@ -104,6 +108,7 @@ class SimpleLoopController : public LoopController {
std::atomic<int> remoteScheduleCalled_{0}; std::atomic<int> remoteScheduleCalled_{0};
int remoteLoopRun_{0}; int remoteLoopRun_{0};
std::vector<std::pair<TimePoint, std::function<void()>>> scheduledFuncs_; std::vector<std::pair<TimePoint, std::function<void()>>> scheduledFuncs_;
Function<TimePoint()> timeFunc_{[] { return Clock::now(); }};
/* LoopController interface */ /* LoopController interface */
......
...@@ -48,6 +48,9 @@ TEST(FiberManager, batonTimedWaitTimeout) { ...@@ -48,6 +48,9 @@ TEST(FiberManager, batonTimedWaitTimeout) {
auto& loopController = auto& loopController =
dynamic_cast<SimpleLoopController&>(manager.loopController()); dynamic_cast<SimpleLoopController&>(manager.loopController());
auto now = SimpleLoopController::Clock::now();
loopController.setTimeFunc([&] { return now; });
auto loopFunc = [&]() { auto loopFunc = [&]() {
if (!taskAdded) { if (!taskAdded) {
manager.addTask([&]() { manager.addTask([&]() {
...@@ -72,7 +75,7 @@ TEST(FiberManager, batonTimedWaitTimeout) { ...@@ -72,7 +75,7 @@ TEST(FiberManager, batonTimedWaitTimeout) {
}); });
taskAdded = true; taskAdded = true;
} else { } else {
std::this_thread::sleep_for(std::chrono::milliseconds(50)); now += std::chrono::milliseconds(50);
iterations++; iterations++;
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment