Commit a72a920c authored by Chad Austin's avatar Chad Austin Committed by Facebook Github Bot

Drain ManualExecutor on destruction

Summary:
For use in tests where the executor has a nontrivial lifetime but any
queued jobs should be completed, have ManualExecutor drain itself
before it's destroyed.

Reviewed By: yfeldblum

Differential Revision: D8845457

fbshipit-source-id: 7c2aa65aa27a7850ff73a93cfbe34c2248b62d26
parent 37c011d8
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
namespace folly { namespace folly {
ManualExecutor::~ManualExecutor() {
drain();
}
void ManualExecutor::add(Func callback) { void ManualExecutor::add(Func callback) {
std::lock_guard<std::mutex> lock(lock_); std::lock_guard<std::mutex> lock(lock_);
funcs_.emplace(std::move(callback)); funcs_.emplace(std::move(callback));
......
...@@ -39,6 +39,8 @@ class ManualExecutor : public DrivableExecutor, ...@@ -39,6 +39,8 @@ class ManualExecutor : public DrivableExecutor,
public ScheduledExecutor, public ScheduledExecutor,
public SequencedExecutor { public SequencedExecutor {
public: public:
~ManualExecutor();
void add(Func) override; void add(Func) override;
/// Do work. Returns the number of functions that were executed (maybe 0). /// Do work. Returns the number of functions that were executed (maybe 0).
......
...@@ -37,6 +37,10 @@ TEST(ManualExecutor, runIsStable) { ...@@ -37,6 +37,10 @@ TEST(ManualExecutor, runIsStable) {
x.add(f2); x.add(f2);
x.run(); x.run();
EXPECT_EQ(count, 0); EXPECT_EQ(count, 0);
// ManualExecutor's destructor drains, so explicitly clear the two added by
// f2.
EXPECT_EQ(2, x.clear());
} }
TEST(ManualExecutor, drainIsNotStable) { TEST(ManualExecutor, drainIsNotStable) {
...@@ -197,6 +201,15 @@ TEST(ManualExecutor, clear) { ...@@ -197,6 +201,15 @@ TEST(ManualExecutor, clear) {
EXPECT_EQ(0, count); EXPECT_EQ(0, count);
} }
TEST(ManualExecutor, drainsOnDestruction) {
size_t count = 0;
{
ManualExecutor x;
x.add([&] { ++count; });
}
EXPECT_EQ(1, count);
}
TEST(Executor, InlineExecutor) { TEST(Executor, InlineExecutor) {
InlineExecutor x; InlineExecutor x;
size_t counter = 0; size_t counter = 0;
......
...@@ -540,6 +540,7 @@ TEST(Via, viaExecutorDiscardsTaskFutureSetValueFirst) { ...@@ -540,6 +540,7 @@ TEST(Via, viaExecutorDiscardsTaskFutureSetValueFirst) {
ManualExecutor x; ManualExecutor x;
future = makeFuture().via(&x).then( future = makeFuture().via(&x).then(
[c = std::move(captured_promise)] { return 42; }); [c = std::move(captured_promise)] { return 42; });
x.clear();
} }
EXPECT_THROW(std::move(*future).get(std::chrono::seconds(5)), BrokenPromise); EXPECT_THROW(std::move(*future).get(std::chrono::seconds(5)), BrokenPromise);
...@@ -564,6 +565,7 @@ TEST(Via, viaExecutorDiscardsTaskFutureSetCallbackFirst) { ...@@ -564,6 +565,7 @@ TEST(Via, viaExecutorDiscardsTaskFutureSetCallbackFirst) {
future = trigger.getFuture().via(&x).then( future = trigger.getFuture().via(&x).then(
[c = std::move(captured_promise)] { return 42; }); [c = std::move(captured_promise)] { return 42; });
trigger.setValue(); trigger.setValue();
x.clear();
} }
EXPECT_THROW(std::move(*future).get(std::chrono::seconds(5)), BrokenPromise); EXPECT_THROW(std::move(*future).get(std::chrono::seconds(5)), BrokenPromise);
......
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