TimePoint now() override { return now_; }
+ /// Flush the function queue. Destroys all stored functions without
+ /// executing them. Returns number of removed functions.
+ std::size_t clear() {
+ std::queue<Func> funcs;
+ std::priority_queue<ScheduledFunc> scheduled_funcs;
+
+ {
+ std::lock_guard<std::mutex> lock(lock_);
+ funcs_.swap(funcs);
+ scheduledFuncs_.swap(scheduled_funcs);
+ }
+
+ return funcs.size() + scheduled_funcs.size();
+ }
+
private:
std::mutex lock_;
std::queue<Func> funcs_;
t.join();
}
+TEST(ManualExecutor, clear) {
+ ManualExecutor x;
+ size_t count = 0;
+ x.add([&] { ++count; });
+ x.scheduleAt([&] { ++count; }, x.now() + std::chrono::milliseconds(10));
+ EXPECT_EQ(0, count);
+
+ x.clear();
+ x.advance(std::chrono::milliseconds(10));
+ x.run();
+ EXPECT_EQ(0, count);
+}
+
TEST(Executor, InlineExecutor) {
InlineExecutor x;
size_t counter = 0;