From 46930a2aef23cb94dbedb76477dc04733c724ad5 Mon Sep 17 00:00:00 2001 From: Mirek Klimos Date: Fri, 7 Oct 2016 11:18:57 -0700 Subject: [PATCH] FunctionScheduler - return whether shutdown was successful Summary: We want to fail fast if we attempt to stop a scheduler that was not running Reviewed By: yfeldblum Differential Revision: D3976919 fbshipit-source-id: d8aa8b35aa9e22758e6a7ef64f48a7dd6d990b1c --- folly/experimental/FunctionScheduler.cpp | 5 +++-- folly/experimental/FunctionScheduler.h | 3 ++- folly/experimental/test/FunctionSchedulerTest.cpp | 14 +++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/folly/experimental/FunctionScheduler.cpp b/folly/experimental/FunctionScheduler.cpp index 6727ca35..2e0ee6eb 100644 --- a/folly/experimental/FunctionScheduler.cpp +++ b/folly/experimental/FunctionScheduler.cpp @@ -277,17 +277,18 @@ bool FunctionScheduler::start() { return true; } -void FunctionScheduler::shutdown() { +bool FunctionScheduler::shutdown() { { std::lock_guard g(mutex_); if (!running_) { - return; + return false; } running_ = false; runningCondvar_.notify_one(); } thread_.join(); + return true; } void FunctionScheduler::run() { diff --git a/folly/experimental/FunctionScheduler.h b/folly/experimental/FunctionScheduler.h index e5514596..1668c501 100644 --- a/folly/experimental/FunctionScheduler.h +++ b/folly/experimental/FunctionScheduler.h @@ -177,8 +177,9 @@ class FunctionScheduler { * Stops the FunctionScheduler. * * It may be restarted later by calling start() again. + * Returns false if the scheduler was not running. */ - void shutdown(); + bool shutdown(); /** * Set the name of the worker thread. diff --git a/folly/experimental/test/FunctionSchedulerTest.cpp b/folly/experimental/test/FunctionSchedulerTest.cpp index e54d1b4a..eb122d6f 100644 --- a/folly/experimental/test/FunctionSchedulerTest.cpp +++ b/folly/experimental/test/FunctionSchedulerTest.cpp @@ -50,6 +50,19 @@ void delay(int n) { } // unnamed namespace +TEST(FunctionScheduler, StartAndShutdown) { + FunctionScheduler fs; + EXPECT_TRUE(fs.start()); + EXPECT_FALSE(fs.start()); + EXPECT_TRUE(fs.shutdown()); + EXPECT_FALSE(fs.shutdown()); + // start again + EXPECT_TRUE(fs.start()); + EXPECT_FALSE(fs.start()); + EXPECT_TRUE(fs.shutdown()); + EXPECT_FALSE(fs.shutdown()); +} + TEST(FunctionScheduler, SimpleAdd) { int total = 0; FunctionScheduler fs; @@ -76,7 +89,6 @@ TEST(FunctionScheduler, AddCancel) { delay(2); EXPECT_EQ(4, total); fs.addFunction([&] { total += 1; }, testInterval(2), "add2"); - EXPECT_FALSE(fs.start()); // already running delay(1); EXPECT_EQ(5, total); delay(2); -- 2.34.1