Commit c7ad653c authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

FunctionSchedulerTest: honor timeFactor correctly

Summary:
Most of the tests in FunctionSchedulerTest sleep for lengths of time based on
the timeFactor constant set at the top of the file.  However a couple of places
that need to sleep for non-integral timeFactor durations used manual `usleep()`
calls instead.  This made the tests break if you change the `timeFactor` value.

This changes `delay()` to accept floating point numbers, and updates the tests
that were using hard-coded `usleep()` calls to use `delay()` instead.

Reviewed By: yfeldblum

Differential Revision: D6880258

fbshipit-source-id: 0cfdf84d3ba27aa2acd1f5210becbc7306b857bf
parent 4b34bc38
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
using namespace folly; using namespace folly;
using std::atomic; using std::atomic;
using std::chrono::duration_cast;
using std::chrono::microseconds;
using std::chrono::milliseconds; using std::chrono::milliseconds;
namespace { namespace {
...@@ -51,8 +53,9 @@ int getTicksWithinRange(int n, int min, int max) { ...@@ -51,8 +53,9 @@ int getTicksWithinRange(int n, int min, int max) {
n = std::min(max, n); n = std::min(max, n);
return n; return n;
} }
void delay(int n) { void delay(float n) {
std::chrono::microseconds usec(n * timeFactor); microseconds usec(static_cast<microseconds::rep>(
duration_cast<microseconds>(timeFactor).count() * n));
usleep(usec.count()); usleep(usec.count());
} }
...@@ -245,7 +248,7 @@ TEST(FunctionScheduler, ResetFunc) { ...@@ -245,7 +248,7 @@ TEST(FunctionScheduler, ResetFunc) {
delay(1); delay(1);
// t2: after the reset, add2 should have been invoked immediately // t2: after the reset, add2 should have been invoked immediately
EXPECT_EQ(7, total); EXPECT_EQ(7, total);
usleep(150000); delay(1.5);
// t3.5: add3 should have been invoked. add2 should not // t3.5: add3 should have been invoked. add2 should not
EXPECT_EQ(10, total); EXPECT_EQ(10, total);
delay(1); delay(1);
...@@ -342,10 +345,12 @@ TEST(FunctionScheduler, AddWhileRunning) { ...@@ -342,10 +345,12 @@ TEST(FunctionScheduler, AddWhileRunning) {
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
// The function should be invoked nearly immediately when we add it // The function should be invoked nearly immediately when we add it
// and the FunctionScheduler is already running // and the FunctionScheduler is already running
usleep(50000); delay(0.5);
EXPECT_EQ(2, total); auto t = total.load();
EXPECT_EQ(2, t);
delay(2); delay(2);
EXPECT_EQ(4, total); t = total.load();
EXPECT_EQ(4, t);
} }
TEST(FunctionScheduler, NoShutdown) { TEST(FunctionScheduler, NoShutdown) {
...@@ -354,7 +359,7 @@ TEST(FunctionScheduler, NoShutdown) { ...@@ -354,7 +359,7 @@ TEST(FunctionScheduler, NoShutdown) {
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(1), "add2"); fs.addFunction([&] { total += 2; }, testInterval(1), "add2");
fs.start(); fs.start();
usleep(50000); delay(0.5);
EXPECT_EQ(2, total); EXPECT_EQ(2, total);
} }
// Destroyed the FunctionScheduler without calling shutdown. // Destroyed the FunctionScheduler without calling shutdown.
......
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