OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/post_task_and_reply_impl.h" | 5 #include "base/threading/post_task_and_reply_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 bool* const delete_flag_; | 51 bool* const delete_flag_; |
52 | 52 |
53 DISALLOW_COPY_AND_ASSIGN(ObjectToDelete); | 53 DISALLOW_COPY_AND_ASSIGN(ObjectToDelete); |
54 }; | 54 }; |
55 | 55 |
56 class MockObject { | 56 class MockObject { |
57 public: | 57 public: |
58 MockObject() = default; | 58 MockObject() = default; |
59 | 59 |
60 MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>)); | 60 MOCK_METHOD1(Task, void(scoped_refptr<ObjectToDelete>)); |
61 | 61 MOCK_METHOD0(Reply, void()); |
62 void Reply(bool* delete_flag) { | |
63 // Expect the task's deletion flag to be set before the reply runs. | |
64 EXPECT_TRUE(*delete_flag); | |
65 ReplyMock(); | |
66 } | |
67 | |
68 MOCK_METHOD0(ReplyMock, void()); | |
69 | 62 |
70 private: | 63 private: |
71 DISALLOW_COPY_AND_ASSIGN(MockObject); | 64 DISALLOW_COPY_AND_ASSIGN(MockObject); |
72 }; | 65 }; |
73 | 66 |
74 } // namespace | 67 } // namespace |
75 | 68 |
76 TEST(PostTaskAndReplyImplTest, PostTaskAndReply) { | 69 TEST(PostTaskAndReplyImplTest, PostTaskAndReply) { |
77 scoped_refptr<TestSimpleTaskRunner> post_runner(new TestSimpleTaskRunner); | 70 scoped_refptr<TestSimpleTaskRunner> post_runner(new TestSimpleTaskRunner); |
78 scoped_refptr<TestSimpleTaskRunner> reply_runner(new TestSimpleTaskRunner); | 71 scoped_refptr<TestSimpleTaskRunner> reply_runner(new TestSimpleTaskRunner); |
79 ThreadTaskRunnerHandle task_runner_handle(reply_runner); | 72 ThreadTaskRunnerHandle task_runner_handle(reply_runner); |
80 | 73 |
81 testing::StrictMock<MockObject> mock_object; | 74 testing::StrictMock<MockObject> mock_object; |
82 bool delete_flag = false; | 75 bool delete_flag = false; |
83 | 76 |
84 EXPECT_TRUE( | 77 EXPECT_TRUE( |
85 PostTaskAndReplyTaskRunner(post_runner.get()) | 78 PostTaskAndReplyTaskRunner(post_runner.get()) |
86 .PostTaskAndReply( | 79 .PostTaskAndReply( |
87 FROM_HERE, | 80 FROM_HERE, |
88 Bind(&MockObject::Task, Unretained(&mock_object), | 81 Bind(&MockObject::Task, Unretained(&mock_object), |
89 make_scoped_refptr(new ObjectToDelete(&delete_flag))), | 82 make_scoped_refptr(new ObjectToDelete(&delete_flag))), |
90 Bind(&MockObject::Reply, Unretained(&mock_object), | 83 Bind(&MockObject::Reply, Unretained(&mock_object)))); |
91 Unretained(&delete_flag)))); | |
92 | |
93 // Expect no reply in |reply_runner|. | |
94 EXPECT_FALSE(reply_runner->HasPendingTask()); | |
95 | 84 |
96 // Expect the task to be posted to |post_runner|. | 85 // Expect the task to be posted to |post_runner|. |
97 EXPECT_TRUE(post_runner->HasPendingTask()); | 86 EXPECT_TRUE(post_runner->HasPendingTask()); |
| 87 EXPECT_FALSE(reply_runner->HasPendingTask()); |
| 88 EXPECT_FALSE(delete_flag); |
| 89 |
98 EXPECT_CALL(mock_object, Task(_)); | 90 EXPECT_CALL(mock_object, Task(_)); |
99 post_runner->RunUntilIdle(); | 91 post_runner->RunUntilIdle(); |
100 testing::Mock::VerifyAndClear(&mock_object); | 92 testing::Mock::VerifyAndClear(&mock_object); |
101 | 93 |
102 // Expect the task's argument not to have been deleted yet. | 94 // |task| should have been deleted right after being run. |
103 EXPECT_FALSE(delete_flag); | 95 EXPECT_TRUE(delete_flag); |
104 | 96 |
105 // Expect the reply to be posted to |reply_runner|. | 97 // Expect the reply to be posted to |reply_runner|. |
106 EXPECT_FALSE(post_runner->HasPendingTask()); | 98 EXPECT_FALSE(post_runner->HasPendingTask()); |
107 EXPECT_TRUE(reply_runner->HasPendingTask()); | 99 EXPECT_TRUE(reply_runner->HasPendingTask()); |
108 EXPECT_CALL(mock_object, ReplyMock()); | 100 |
| 101 EXPECT_CALL(mock_object, Reply()); |
109 reply_runner->RunUntilIdle(); | 102 reply_runner->RunUntilIdle(); |
110 testing::Mock::VerifyAndClear(&mock_object); | 103 testing::Mock::VerifyAndClear(&mock_object); |
111 EXPECT_TRUE(delete_flag); | 104 EXPECT_TRUE(delete_flag); |
112 | 105 |
113 // Expect no pending task in |post_runner| and |reply_runner|. | 106 // Expect no pending task in |post_runner| and |reply_runner|. |
114 EXPECT_FALSE(post_runner->HasPendingTask()); | 107 EXPECT_FALSE(post_runner->HasPendingTask()); |
115 EXPECT_FALSE(reply_runner->HasPendingTask()); | 108 EXPECT_FALSE(reply_runner->HasPendingTask()); |
116 } | 109 } |
117 | 110 |
118 } // namespace internal | 111 } // namespace internal |
119 } // namespace base | 112 } // namespace base |
OLD | NEW |