return true;
}
-void FunctionScheduler::shutdown() {
+bool FunctionScheduler::shutdown() {
{
std::lock_guard<std::mutex> g(mutex_);
if (!running_) {
- return;
+ return false;
}
running_ = false;
runningCondvar_.notify_one();
}
thread_.join();
+ return true;
}
void FunctionScheduler::run() {
* 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.
} // 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;
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);