Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Unified Diff: base/threading/post_task_and_reply_impl_unittest.cc

Issue 2657603004: Clear PostTaskAndReply task on the destination thread (3) (Closed)
Patch Set: -#include Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/threading/post_task_and_reply_impl.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/post_task_and_reply_impl_unittest.cc
diff --git a/base/threading/post_task_and_reply_impl_unittest.cc b/base/threading/post_task_and_reply_impl_unittest.cc
index 2e8fc44291b0eab156d05bf20268bafb4f1eae83..7335dd3664ad147f38c8dec33fc28f16205b64df 100644
--- a/base/threading/post_task_and_reply_impl_unittest.cc
+++ b/base/threading/post_task_and_reply_impl_unittest.cc
@@ -10,6 +10,7 @@
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
+#include "base/single_thread_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -71,16 +72,67 @@ class MockObject {
DISALLOW_COPY_AND_ASSIGN(MockObject);
};
+class WrappedTaskRunner : public SingleThreadTaskRunner {
gab 2017/03/29 18:21:35 Hmmm sorry I wasn't clear, I didn't mean to make t
tzik 2017/03/30 09:01:18 Ah, sorry for confusion. Updated the test.
+ public:
+ explicit WrappedTaskRunner(
+ scoped_refptr<SingleThreadTaskRunner> underlying_task_runner)
+ : underlying_task_runner_(std::move(underlying_task_runner)) {}
+
+ bool PostDelayedTask(const tracked_objects::Location& from_here,
+ Closure task,
+ base::TimeDelta delay) override {
+ if (post_task_hook_)
+ std::move(post_task_hook_).Run();
+ return underlying_task_runner_->PostDelayedTask(from_here, std::move(task),
+ delay);
+ }
+
+ bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
+ Closure task,
+ base::TimeDelta delay) override {
+ if (post_task_hook_)
+ std::move(post_task_hook_).Run();
+ return underlying_task_runner_->PostNonNestableDelayedTask(
+ from_here, std::move(task), delay);
+ }
+
+ void set_post_task_hook(OnceClosure hook) {
+ post_task_hook_ = std::move(hook);
+ }
+
+ bool has_post_task_hook() const { return !post_task_hook_.is_null(); }
+
+ bool RunsTasksOnCurrentThread() const override {
+ return underlying_task_runner_->RunsTasksOnCurrentThread();
+ }
+
+ private:
+ ~WrappedTaskRunner() override {}
+
+ scoped_refptr<SingleThreadTaskRunner> underlying_task_runner_;
+ OnceClosure post_task_hook_;
+
+ DISALLOW_COPY_AND_ASSIGN(WrappedTaskRunner);
+};
+
} // namespace
TEST(PostTaskAndReplyImplTest, PostTaskAndReply) {
scoped_refptr<TestSimpleTaskRunner> post_runner(new TestSimpleTaskRunner);
scoped_refptr<TestSimpleTaskRunner> reply_runner(new TestSimpleTaskRunner);
- ThreadTaskRunnerHandle task_runner_handle(reply_runner);
+ scoped_refptr<WrappedTaskRunner> wrapped_reply_runner(
+ new WrappedTaskRunner(reply_runner));
+ ThreadTaskRunnerHandle task_runner_handle(wrapped_reply_runner);
testing::StrictMock<MockObject> mock_object;
bool delete_flag = false;
+ auto expect_true = [](bool* delete_flag) { EXPECT_TRUE(*delete_flag); };
+
+ // Expect the posted callback is destroyed before the reply task is scheduled.
+ wrapped_reply_runner->set_post_task_hook(
+ base::BindOnce(expect_true, &delete_flag));
+
EXPECT_TRUE(
PostTaskAndReplyTaskRunner(post_runner.get())
.PostTaskAndReply(
@@ -99,8 +151,9 @@ TEST(PostTaskAndReplyImplTest, PostTaskAndReply) {
post_runner->RunUntilIdle();
testing::Mock::VerifyAndClear(&mock_object);
- // Expect the task's argument not to have been deleted yet.
- EXPECT_FALSE(delete_flag);
+ // Expect the task's argument to have been deleted.
+ EXPECT_FALSE(wrapped_reply_runner->has_post_task_hook());
+ EXPECT_TRUE(delete_flag);
// Expect the reply to be posted to |reply_runner|.
EXPECT_FALSE(post_runner->HasPendingTask());
« no previous file with comments | « base/threading/post_task_and_reply_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698