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

Unified Diff: base/threading/post_task_and_reply_impl_unittest.cc

Issue 2180953002: Support PostTaskAndReply from a sequenced task. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self-review Created 4 years, 5 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
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
new file mode 100644
index 0000000000000000000000000000000000000000..f24ca82746f561203693e1a17cb9cd93c52d822d
--- /dev/null
+++ b/base/threading/post_task_and_reply_impl_unittest.cc
@@ -0,0 +1,99 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/threading/post_task_and_reply_impl.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/test/test_simple_task_runner.h"
+#include "base/threading/sequenced_task_runner_handle.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+
+namespace base {
+namespace internal {
+
+namespace {
+
+class ObjectToDelete : public RefCounted<ObjectToDelete> {
+ public:
+ // |delete_flag| is set to true when this object is deleted
+ ObjectToDelete(bool* delete_flag) : delete_flag_(delete_flag) {
robliao 2016/07/27 17:50:07 Explicit
fdoray 2016/07/28 13:40:48 Done.
+ EXPECT_FALSE(*delete_flag_);
robliao 2016/07/27 17:50:07 Should the test be allowed to proceed if this is f
fdoray 2016/07/28 13:40:48 Probably not. But ASSERTs are not supported in a c
robliao 2016/07/28 19:25:26 Ah, that's indeed because of the early return. Pro
+ }
robliao 2016/07/27 17:50:07 Add an ASSERT_TRUE(delete_flag)
fdoray 2016/07/28 13:40:48 Added EXPECT_TRUE. ASSERT_TRUE isn't supported in
+
+ private:
+ friend class RefCounted<ObjectToDelete>;
+ ~ObjectToDelete() { *delete_flag_ = true; }
+
+ bool* const delete_flag_;
+
+ DISALLOW_COPY_AND_ASSIGN(ObjectToDelete);
+};
+
+class MockObject {
+ public:
+ MockObject() = default;
+
+ MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>));
+
+ void Reply(bool* delete_flag) {
+ // Expect the task's deletion flag to be set before the reply runs.
+ EXPECT_TRUE(*delete_flag);
+ ReplyMock();
+ }
+
+ MOCK_METHOD0(ReplyMock, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockObject);
+};
+
+} // namespace
+
+TEST(PostTaskAndReplyImplTest, PostTaskAndReply) {
+ scoped_refptr<TestSimpleTaskRunner> task_runner(new TestSimpleTaskRunner);
+ scoped_refptr<TestSimpleTaskRunner> current_runner(new TestSimpleTaskRunner);
+ SequencedTaskRunnerHandle sequenced_task_runner_handle(current_runner);
+
+ testing::StrictMock<MockObject> mock_object;
+ bool delete_flag = false;
+
+ EXPECT_TRUE(internal::PostTaskAndReply(
+ FROM_HERE, Bind(&MockObject::Task, Unretained(&mock_object),
+ make_scoped_refptr(new ObjectToDelete(&delete_flag))),
+ Bind(&MockObject::Reply, Unretained(&mock_object),
+ Unretained(&delete_flag)),
+ Bind(&TestSimpleTaskRunner::PostTask, task_runner.get())));
+
+ // Expect no reply in |current_runner|.
+ EXPECT_FALSE(current_runner->HasPendingTask());
+
+ // Expect the task to be posted to |task_runner|.
+ EXPECT_TRUE(task_runner->HasPendingTask());
+ EXPECT_CALL(mock_object, Task(_));
+ task_runner->RunUntilIdle();
+ testing::Mock::VerifyAndClear(&mock_object);
+
+ // Expect the task's argument not to have been deleted yet.
+ EXPECT_FALSE(delete_flag);
robliao 2016/07/27 17:50:07 There should also be an EXPECT_TRUE for this after
fdoray 2016/07/28 13:40:48 Done.
+
+ // Expect the reply to be posted to |current_runner|.
+ EXPECT_FALSE(task_runner->HasPendingTask());
+ EXPECT_TRUE(current_runner->HasPendingTask());
+ EXPECT_CALL(mock_object, ReplyMock());
+ current_runner->RunUntilIdle();
+ testing::Mock::VerifyAndClear(&mock_object);
+
+ // Expect no pending task in |task_runner| and |current_runner|.
+ EXPECT_FALSE(task_runner->HasPendingTask());
+ EXPECT_FALSE(current_runner->HasPendingTask());
+}
+
+} // namespace internal
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698