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

Side by Side 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, 4 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/threading/post_task_and_reply_impl.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/macros.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "base/threading/sequenced_task_runner_handle.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using testing::_;
17
18 namespace base {
19 namespace internal {
20
21 namespace {
22
23 class ObjectToDelete : public RefCounted<ObjectToDelete> {
24 public:
25 // |delete_flag| is set to true when this object is deleted
26 ObjectToDelete(bool* delete_flag) : delete_flag_(delete_flag) {
robliao 2016/07/27 17:50:07 Explicit
fdoray 2016/07/28 13:40:48 Done.
27 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
28 }
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
29
30 private:
31 friend class RefCounted<ObjectToDelete>;
32 ~ObjectToDelete() { *delete_flag_ = true; }
33
34 bool* const delete_flag_;
35
36 DISALLOW_COPY_AND_ASSIGN(ObjectToDelete);
37 };
38
39 class MockObject {
40 public:
41 MockObject() = default;
42
43 MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>));
44
45 void Reply(bool* delete_flag) {
46 // Expect the task's deletion flag to be set before the reply runs.
47 EXPECT_TRUE(*delete_flag);
48 ReplyMock();
49 }
50
51 MOCK_METHOD0(ReplyMock, void());
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(MockObject);
55 };
56
57 } // namespace
58
59 TEST(PostTaskAndReplyImplTest, PostTaskAndReply) {
60 scoped_refptr<TestSimpleTaskRunner> task_runner(new TestSimpleTaskRunner);
61 scoped_refptr<TestSimpleTaskRunner> current_runner(new TestSimpleTaskRunner);
62 SequencedTaskRunnerHandle sequenced_task_runner_handle(current_runner);
63
64 testing::StrictMock<MockObject> mock_object;
65 bool delete_flag = false;
66
67 EXPECT_TRUE(internal::PostTaskAndReply(
68 FROM_HERE, Bind(&MockObject::Task, Unretained(&mock_object),
69 make_scoped_refptr(new ObjectToDelete(&delete_flag))),
70 Bind(&MockObject::Reply, Unretained(&mock_object),
71 Unretained(&delete_flag)),
72 Bind(&TestSimpleTaskRunner::PostTask, task_runner.get())));
73
74 // Expect no reply in |current_runner|.
75 EXPECT_FALSE(current_runner->HasPendingTask());
76
77 // Expect the task to be posted to |task_runner|.
78 EXPECT_TRUE(task_runner->HasPendingTask());
79 EXPECT_CALL(mock_object, Task(_));
80 task_runner->RunUntilIdle();
81 testing::Mock::VerifyAndClear(&mock_object);
82
83 // Expect the task's argument not to have been deleted yet.
84 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.
85
86 // Expect the reply to be posted to |current_runner|.
87 EXPECT_FALSE(task_runner->HasPendingTask());
88 EXPECT_TRUE(current_runner->HasPendingTask());
89 EXPECT_CALL(mock_object, ReplyMock());
90 current_runner->RunUntilIdle();
91 testing::Mock::VerifyAndClear(&mock_object);
92
93 // Expect no pending task in |task_runner| and |current_runner|.
94 EXPECT_FALSE(task_runner->HasPendingTask());
95 EXPECT_FALSE(current_runner->HasPendingTask());
96 }
97
98 } // namespace internal
99 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698