blob: 5c9a122059c7cf5ed5fec64b4fbb650600b5ce1d [file] [log] [blame]
ajwong4f13f742017-02-09 23:52:401// Copyright (c) 2017 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/pending_task.h"
6
7#include <vector>
8
9#include "base/bind.h"
10#include "base/message_loop/message_loop.h"
11#include "base/run_loop.h"
12#include "base/strings/stringprintf.h"
13#include "base/threading/thread.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace base {
17
18class PendingTaskTest : public ::testing::Test {
19 public:
20 PendingTaskTest() = default;
21
22 ~PendingTaskTest() override = default;
23
24 protected:
25 using ExpectedTrace = std::vector<const void*>;
26
Brett Wilson8e88b312017-09-12 05:22:1627 static void VerifyTraceAndPost(const scoped_refptr<TaskRunner>& task_runner,
28 const Location& posted_from,
29 const Location& next_from_here,
30 const std::vector<const void*>& expected_trace,
31 Closure task) {
ajwong4f13f742017-02-09 23:52:4032 SCOPED_TRACE(StringPrintf("Callback Depth: %zu", expected_trace.size()));
33
34 // Beyond depth + 1, the trace is nonsensical because there haven't been
35 // enough nested tasks called.
36 const PendingTask* current_pending_task =
37 MessageLoop::current()->current_pending_task_;
38 size_t window = std::min(current_pending_task->task_backtrace.size(),
39 expected_trace.size());
40
41 EXPECT_EQ(posted_from,
42 MessageLoop::current()->current_pending_task_->posted_from);
43 for (size_t i = 0; i < window; i++) {
44 SCOPED_TRACE(StringPrintf("Trace frame: %zu", i));
45 EXPECT_EQ(expected_trace[i], current_pending_task->task_backtrace[i]);
46 }
47 task_runner->PostTask(next_from_here, std::move(task));
48 }
49
50 static void RunTwo(Closure c1, Closure c2) {
51 c1.Run();
52 c2.Run();
53 }
54};
55
56// Ensure the task backtrace populates correctly.
57TEST_F(PendingTaskTest, SingleThreadedSimple) {
58 MessageLoop loop;
Brett Wilson8e88b312017-09-12 05:22:1659 const Location& location0 = FROM_HERE;
60 const Location& location1 = FROM_HERE;
61 const Location& location2 = FROM_HERE;
62 const Location& location3 = FROM_HERE;
63 const Location& location4 = FROM_HERE;
64 const Location& location5 = FROM_HERE;
ajwong4f13f742017-02-09 23:52:4065
66 Closure task5 = Bind(
67 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location4,
68 location5,
69 ExpectedTrace({location3.program_counter(), location2.program_counter(),
70 location1.program_counter(), location0.program_counter()}),
71 Bind(&DoNothing));
72 Closure task4 = Bind(
73 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location3,
74 location4,
75 ExpectedTrace({location2.program_counter(), location1.program_counter(),
76 location0.program_counter(), nullptr}),
77 task5);
78 Closure task3 = Bind(
79 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location2,
80 location3, ExpectedTrace({location1.program_counter(),
81 location0.program_counter(), nullptr, nullptr}),
82 task4);
83 Closure task2 =
84 Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location1,
85 location2, ExpectedTrace({location0.program_counter()}), task3);
86 Closure task1 = Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
87 location0, location1, ExpectedTrace({}), task2);
88
89 loop.task_runner()->PostTask(location0, task1);
90
91 RunLoop().RunUntilIdle();
92}
93
94// Post a task onto another thread. Ensure on the other thread, it has the
95// right stack trace.
96TEST_F(PendingTaskTest, MultipleThreads) {
97 MessageLoop loop; // Implicitly "thread a."
98 Thread thread_b("pt_test_b");
99 Thread thread_c("pt_test_c");
100 thread_b.StartAndWaitForTesting();
101 thread_c.StartAndWaitForTesting();
102
Brett Wilson8e88b312017-09-12 05:22:16103 const Location& location_a0 = FROM_HERE;
104 const Location& location_a1 = FROM_HERE;
105 const Location& location_a2 = FROM_HERE;
106 const Location& location_a3 = FROM_HERE;
ajwong4f13f742017-02-09 23:52:40107
Brett Wilson8e88b312017-09-12 05:22:16108 const Location& location_b0 = FROM_HERE;
109 const Location& location_b1 = FROM_HERE;
ajwong4f13f742017-02-09 23:52:40110
Brett Wilson8e88b312017-09-12 05:22:16111 const Location& location_c0 = FROM_HERE;
ajwong4f13f742017-02-09 23:52:40112
113 // On thread c, post a task back to thread a that verifies its trace
114 // and terminates after one more self-post.
115 Closure task_a2 =
116 Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
117 location_a2, location_a3,
118 ExpectedTrace(
119 {location_c0.program_counter(), location_b0.program_counter(),
120 location_a1.program_counter(), location_a0.program_counter()}),
121 Bind(&DoNothing));
122 Closure task_c0 = Bind(&PendingTaskTest::VerifyTraceAndPost,
123 loop.task_runner(), location_c0, location_a2,
124 ExpectedTrace({location_b0.program_counter(),
125 location_a1.program_counter(),
126 location_a0.program_counter()}),
127 task_a2);
128
129 // On thread b run two tasks that conceptually come from the same location
130 // (managed via RunTwo().) One will post back to thread b and another will
131 // post to thread c to test spawning multiple tasks on different message
132 // loops. The task posted to thread c will not get location b1 whereas the
133 // one posted back to thread b will.
134 Closure task_b0_fork =
135 Bind(&PendingTaskTest::VerifyTraceAndPost,
136 thread_c.message_loop()->task_runner(), location_b0, location_c0,
137 ExpectedTrace({location_a1.program_counter(),
138 location_a0.program_counter(), nullptr}),
139 task_c0);
140 Closure task_b0_local =
141 Bind(&PendingTaskTest::VerifyTraceAndPost,
142 thread_b.message_loop()->task_runner(), location_b0, location_b1,
143 ExpectedTrace({location_a1.program_counter(),
144 location_a0.program_counter(), nullptr}),
145 Bind(&DoNothing));
146
147 // Push one frame onto the stack in thread a then pass to thread b.
148 Closure task_a1 =
149 Bind(&PendingTaskTest::VerifyTraceAndPost,
150 thread_b.message_loop()->task_runner(), location_a1, location_b0,
151 ExpectedTrace({location_a0.program_counter(), nullptr}),
152 Bind(&PendingTaskTest::RunTwo, task_b0_local, task_b0_fork));
153 Closure task_a0 =
154 Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
155 location_a0, location_a1, ExpectedTrace({nullptr}), task_a1);
156
157 loop.task_runner()->PostTask(location_a0, task_a0);
158
159 RunLoop().RunUntilIdle();
160
161 thread_b.FlushForTesting();
162 thread_b.Stop();
163
164 thread_c.FlushForTesting();
165 thread_c.Stop();
166}
167
168} // namespace base