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

use atomic variables correctly in FunctionSchedulerTest

Summary:
Most of the tests in FunctionSchedulerTest update a variable in one thread (the
FunctionScheduler thread) and then check its value in the main thread.  These
previously were not using any synchronization between the threads.  This fixes
them to use `std::atomic` to ensure proper synchronization.

Reviewed By: wez

Differential Revision: D6880254

fbshipit-source-id: facfbdf06312f731c64389fb4382b5cb078b76b9
parent 7c4b550e
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#endif #endif
using namespace folly; using namespace folly;
using std::atomic;
using std::chrono::milliseconds; using std::chrono::milliseconds;
namespace { namespace {
...@@ -71,7 +72,7 @@ TEST(FunctionScheduler, StartAndShutdown) { ...@@ -71,7 +72,7 @@ TEST(FunctionScheduler, StartAndShutdown) {
} }
TEST(FunctionScheduler, SimpleAdd) { TEST(FunctionScheduler, SimpleAdd) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
fs.start(); fs.start();
...@@ -83,7 +84,7 @@ TEST(FunctionScheduler, SimpleAdd) { ...@@ -83,7 +84,7 @@ TEST(FunctionScheduler, SimpleAdd) {
} }
TEST(FunctionScheduler, AddCancel) { TEST(FunctionScheduler, AddCancel) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
fs.start(); fs.start();
...@@ -104,7 +105,7 @@ TEST(FunctionScheduler, AddCancel) { ...@@ -104,7 +105,7 @@ TEST(FunctionScheduler, AddCancel) {
} }
TEST(FunctionScheduler, AddCancel2) { TEST(FunctionScheduler, AddCancel2) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
// Test adds and cancels while the scheduler is stopped // Test adds and cancels while the scheduler is stopped
...@@ -130,7 +131,7 @@ TEST(FunctionScheduler, AddCancel2) { ...@@ -130,7 +131,7 @@ TEST(FunctionScheduler, AddCancel2) {
EXPECT_TRUE(fs.cancelFunction("add3")); EXPECT_TRUE(fs.cancelFunction("add3"));
// Test a function that cancels itself // Test a function that cancels itself
int selfCancelCount = 0; atomic<int> selfCancelCount{0};
fs.addFunction( fs.addFunction(
[&] { [&] {
++selfCancelCount; ++selfCancelCount;
...@@ -144,7 +145,7 @@ TEST(FunctionScheduler, AddCancel2) { ...@@ -144,7 +145,7 @@ TEST(FunctionScheduler, AddCancel2) {
EXPECT_FALSE(fs.cancelFunction("selfCancel")); EXPECT_FALSE(fs.cancelFunction("selfCancel"));
// Test a function that schedules another function // Test a function that schedules another function
int adderCount = 0; atomic<int> adderCount{0};
int fn2Count = 0; int fn2Count = 0;
auto fn2 = [&] { ++fn2Count; }; auto fn2 = [&] { ++fn2Count; };
auto fnAdder = [&] { auto fnAdder = [&] {
...@@ -182,7 +183,7 @@ TEST(FunctionScheduler, AddCancel2) { ...@@ -182,7 +183,7 @@ TEST(FunctionScheduler, AddCancel2) {
} }
TEST(FunctionScheduler, AddMultiple) { TEST(FunctionScheduler, AddMultiple) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
fs.addFunction([&] { total += 3; }, testInterval(3), "add3"); fs.addFunction([&] { total += 3; }, testInterval(3), "add3");
...@@ -204,7 +205,7 @@ TEST(FunctionScheduler, AddMultiple) { ...@@ -204,7 +205,7 @@ TEST(FunctionScheduler, AddMultiple) {
} }
TEST(FunctionScheduler, AddAfterStart) { TEST(FunctionScheduler, AddAfterStart) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
fs.addFunction([&] { total += 3; }, testInterval(2), "add3"); fs.addFunction([&] { total += 3; }, testInterval(2), "add3");
...@@ -217,7 +218,7 @@ TEST(FunctionScheduler, AddAfterStart) { ...@@ -217,7 +218,7 @@ TEST(FunctionScheduler, AddAfterStart) {
} }
TEST(FunctionScheduler, ShutdownStart) { TEST(FunctionScheduler, ShutdownStart) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2"); fs.addFunction([&] { total += 2; }, testInterval(2), "add2");
fs.start(); fs.start();
...@@ -232,7 +233,7 @@ TEST(FunctionScheduler, ShutdownStart) { ...@@ -232,7 +233,7 @@ TEST(FunctionScheduler, ShutdownStart) {
} }
TEST(FunctionScheduler, ResetFunc) { TEST(FunctionScheduler, ResetFunc) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(3), "add2"); fs.addFunction([&] { total += 2; }, testInterval(3), "add2");
fs.addFunction([&] { total += 3; }, testInterval(3), "add3"); fs.addFunction([&] { total += 3; }, testInterval(3), "add3");
...@@ -253,7 +254,7 @@ TEST(FunctionScheduler, ResetFunc) { ...@@ -253,7 +254,7 @@ TEST(FunctionScheduler, ResetFunc) {
} }
TEST(FunctionScheduler, ResetFunc2) { TEST(FunctionScheduler, ResetFunc2) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunctionOnce([&] { total += 2; }, "add2", testInterval(1)); fs.addFunctionOnce([&] { total += 2; }, "add2", testInterval(1));
fs.addFunctionOnce([&] { total += 3; }, "add3", testInterval(1)); fs.addFunctionOnce([&] { total += 3; }, "add3", testInterval(1));
...@@ -316,7 +317,7 @@ TEST(FunctionScheduler, ResetFuncWhileRunning) { ...@@ -316,7 +317,7 @@ TEST(FunctionScheduler, ResetFuncWhileRunning) {
} }
TEST(FunctionScheduler, AddInvalid) { TEST(FunctionScheduler, AddInvalid) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
// interval may not be negative // interval may not be negative
EXPECT_THROW(fs.addFunction([&] { total += 2; }, testInterval(-1), "add2"), EXPECT_THROW(fs.addFunction([&] { total += 2; }, testInterval(-1), "add2"),
...@@ -334,7 +335,7 @@ TEST(FunctionScheduler, NoFunctions) { ...@@ -334,7 +335,7 @@ TEST(FunctionScheduler, NoFunctions) {
} }
TEST(FunctionScheduler, AddWhileRunning) { TEST(FunctionScheduler, AddWhileRunning) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.start(); fs.start();
delay(1); delay(1);
...@@ -348,7 +349,7 @@ TEST(FunctionScheduler, AddWhileRunning) { ...@@ -348,7 +349,7 @@ TEST(FunctionScheduler, AddWhileRunning) {
} }
TEST(FunctionScheduler, NoShutdown) { TEST(FunctionScheduler, NoShutdown) {
int total = 0; atomic<int> total{0};
{ {
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(1), "add2"); fs.addFunction([&] { total += 2; }, testInterval(1), "add2");
...@@ -364,7 +365,7 @@ TEST(FunctionScheduler, NoShutdown) { ...@@ -364,7 +365,7 @@ TEST(FunctionScheduler, NoShutdown) {
} }
TEST(FunctionScheduler, StartDelay) { TEST(FunctionScheduler, StartDelay) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction([&] { total += 2; }, testInterval(2), "add2", fs.addFunction([&] { total += 2; }, testInterval(2), "add2",
testInterval(2)); testInterval(2));
...@@ -435,7 +436,7 @@ TEST(FunctionScheduler, SteadyCatchup) { ...@@ -435,7 +436,7 @@ TEST(FunctionScheduler, SteadyCatchup) {
} }
TEST(FunctionScheduler, UniformDistribution) { TEST(FunctionScheduler, UniformDistribution) {
int total = 0; atomic<int> total{0};
const int kTicks = 2; const int kTicks = 2;
std::chrono::milliseconds minInterval = std::chrono::milliseconds minInterval =
testInterval(kTicks) - (timeFactor / 5); testInterval(kTicks) - (timeFactor / 5);
...@@ -460,16 +461,17 @@ TEST(FunctionScheduler, UniformDistribution) { ...@@ -460,16 +461,17 @@ TEST(FunctionScheduler, UniformDistribution) {
} }
TEST(FunctionScheduler, ExponentialBackoff) { TEST(FunctionScheduler, ExponentialBackoff) {
int total = 0; atomic<int> total{0};
int expectedInterval = 0; atomic<int> expectedInterval{0};
int nextInterval = 2; atomic<int> nextInterval{2};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunctionGenericDistribution( fs.addFunctionGenericDistribution(
[&] { total += 2; }, [&] { total += 2; },
[&expectedInterval, nextInterval]() mutable { [&expectedInterval, &nextInterval]() mutable {
expectedInterval = nextInterval; auto interval = nextInterval.load();
nextInterval *= nextInterval; expectedInterval = interval;
return testInterval(expectedInterval); nextInterval = interval * interval;
return testInterval(interval);
}, },
"ExponentialBackoff", "ExponentialBackoff",
"2^n * 100ms", "2^n * 100ms",
...@@ -487,8 +489,8 @@ TEST(FunctionScheduler, ExponentialBackoff) { ...@@ -487,8 +489,8 @@ TEST(FunctionScheduler, ExponentialBackoff) {
} }
TEST(FunctionScheduler, GammaIntervalDistribution) { TEST(FunctionScheduler, GammaIntervalDistribution) {
int total = 0; atomic<int> total{0};
int expectedInterval = 0; atomic<int> expectedInterval{0};
FunctionScheduler fs; FunctionScheduler fs;
std::default_random_engine generator(folly::Random::rand32()); std::default_random_engine generator(folly::Random::rand32());
// The alpha and beta arguments are selected, somewhat randomly, to be 2.0. // The alpha and beta arguments are selected, somewhat randomly, to be 2.0.
...@@ -518,7 +520,7 @@ TEST(FunctionScheduler, GammaIntervalDistribution) { ...@@ -518,7 +520,7 @@ TEST(FunctionScheduler, GammaIntervalDistribution) {
} }
TEST(FunctionScheduler, AddWithRunOnce) { TEST(FunctionScheduler, AddWithRunOnce) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunctionOnce([&] { total += 2; }, "add2"); fs.addFunctionOnce([&] { total += 2; }, "add2");
fs.start(); fs.start();
...@@ -537,7 +539,7 @@ TEST(FunctionScheduler, AddWithRunOnce) { ...@@ -537,7 +539,7 @@ TEST(FunctionScheduler, AddWithRunOnce) {
} }
TEST(FunctionScheduler, cancelFunctionAndWait) { TEST(FunctionScheduler, cancelFunctionAndWait) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction( fs.addFunction(
[&] { [&] {
...@@ -613,7 +615,7 @@ TEST(FunctionScheduler, StartThrows) { ...@@ -613,7 +615,7 @@ TEST(FunctionScheduler, StartThrows) {
#endif #endif
TEST(FunctionScheduler, cancelAllFunctionsAndWait) { TEST(FunctionScheduler, cancelAllFunctionsAndWait) {
int total = 0; atomic<int> total{0};
FunctionScheduler fs; FunctionScheduler fs;
fs.addFunction( fs.addFunction(
......
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