Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // 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 | |
| 16 namespace base { | |
| 17 | |
|
danakj
2017/02/08 18:06:23
can you wrap everything in a nested anon namespace
awong
2017/02/09 20:56:05
can't since we're friended by MessageLoop. Added n
| |
| 18 class PendingTaskTest : public ::testing::Test { | |
| 19 public: | |
| 20 PendingTaskTest() {} | |
|
danakj
2017/02/08 18:06:23
= default
awong
2017/02/09 20:56:05
Done.
| |
| 21 | |
| 22 ~PendingTaskTest() override {} | |
|
danakj
2017/02/08 18:06:23
= default
awong
2017/02/09 20:56:05
Done.
| |
| 23 | |
| 24 protected: | |
| 25 typedef std::vector<const void*> ExpectedTrace; | |
|
danakj
2017/02/08 18:06:23
using instead of typedef
awong
2017/02/09 20:56:05
done.
I don't recognize this language anymore. :-
| |
| 26 | |
| 27 static void VerifyTraceAndPost(const scoped_refptr<TaskRunner>& task_runner, | |
|
danakj
2017/02/08 18:06:23
just TaskRunner*, this doesn't need to use scoped_
awong
2017/02/09 20:56:05
So I sorta in general prefer to use const scoped_r
danakj
2017/02/09 21:04:14
In that case I agree, but I think it would just re
awong
2017/02/09 21:16:10
For class methods, there is a special casing of ar
| |
| 28 const tracked_objects::Location& from_here, | |
| 29 const std::vector<const void*>& expected_trace, | |
|
danakj
2017/02/08 18:06:22
you could take this by value, it's always an rvalu
awong
2017/02/09 20:56:05
Is there a benefit to this? It seems like it's jus
danakj
2017/02/09 21:04:14
Oh that's a good point about Bind, I was reading i
| |
| 30 Closure task) { | |
| 31 SCOPED_TRACE(StringPrintf("Callback Depth: %zu", expected_trace.size())); | |
| 32 | |
| 33 // Beyond depth + 1, the trace is nonsensical because there haven't been | |
| 34 // enough nested tasks called. | |
| 35 size_t window = | |
| 36 std::min(arraysize(PendingTask::task_backtrace), expected_trace.size()); | |
| 37 | |
| 38 for (size_t i = 0; i < window; i++) { | |
| 39 SCOPED_TRACE(StringPrintf("Trace frame: %zu", i)); | |
| 40 EXPECT_EQ(expected_trace[i], | |
| 41 base::MessageLoop::current() | |
| 42 ->current_pending_task_->task_backtrace[i]); | |
| 43 } | |
| 44 task_runner->PostTask(from_here, std::move(task)); | |
| 45 } | |
| 46 | |
| 47 static void RunTwo(Closure c1, Closure c2) { | |
| 48 c1.Run(); | |
| 49 c2.Run(); | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 // Ensure the task backtrace populates correctly. | |
| 54 TEST_F(PendingTaskTest, SingleThreadedSimple) { | |
| 55 base::MessageLoop loop; | |
| 56 const tracked_objects::Location& location0 = FROM_HERE; | |
| 57 const tracked_objects::Location& location1 = FROM_HERE; | |
| 58 const tracked_objects::Location& location2 = FROM_HERE; | |
| 59 const tracked_objects::Location& location3 = FROM_HERE; | |
| 60 const tracked_objects::Location& location4 = FROM_HERE; | |
| 61 const tracked_objects::Location& location5 = FROM_HERE; | |
| 62 | |
| 63 Closure task5 = base::Bind( | |
| 64 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location5, | |
| 65 ExpectedTrace({location4.program_counter(), location3.program_counter(), | |
|
danakj
2017/02/08 18:06:23
you could just pass the init list directly, why th
awong
2017/02/09 20:56:05
nope! base::Bind() doesn't understand initializer
danakj
2017/02/09 21:04:14
Ah! Right, me forgetting the Bind again here.
| |
| 66 location2.program_counter(), location1.program_counter()}), | |
| 67 base::Bind(&DoNothing)); | |
|
danakj
2017/02/08 18:06:23
don't need base:: inside base
awong
2017/02/09 21:16:10
Done.
| |
| 68 Closure task4 = base::Bind( | |
| 69 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location4, | |
| 70 ExpectedTrace({location3.program_counter(), location2.program_counter(), | |
| 71 location1.program_counter(), location0.program_counter()}), | |
| 72 task5); | |
| 73 Closure task3 = base::Bind( | |
| 74 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location3, | |
| 75 ExpectedTrace({location2.program_counter(), location1.program_counter(), | |
| 76 location0.program_counter(), nullptr}), | |
| 77 task4); | |
| 78 Closure task2 = base::Bind( | |
| 79 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location2, | |
| 80 ExpectedTrace( | |
| 81 {location1.program_counter(), location0.program_counter(), nullptr}), | |
| 82 task3); | |
| 83 Closure task1 = base::Bind( | |
| 84 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location1, | |
| 85 ExpectedTrace({location0.program_counter(), nullptr}), task2); | |
| 86 | |
| 87 loop.task_runner()->PostTask(location0, task1); | |
| 88 | |
| 89 base::RunLoop().RunUntilIdle(); | |
| 90 } | |
| 91 | |
| 92 // Post a task onto another thread. Ensure on the other thread, it has the | |
| 93 // right stack trace. | |
| 94 TEST_F(PendingTaskTest, MultipleThreads) { | |
| 95 base::MessageLoop loop; // Implicitly "thread a" | |
|
danakj
2017/02/08 18:06:22
comment needs .
| |
| 96 base::Thread thread_b("pt_test_b"); | |
| 97 base::Thread thread_c("pt_test_c"); | |
| 98 thread_b.StartAndWaitForTesting(); | |
| 99 thread_c.StartAndWaitForTesting(); | |
| 100 | |
| 101 const tracked_objects::Location& location_a0 = FROM_HERE; | |
| 102 const tracked_objects::Location& location_a1 = FROM_HERE; | |
| 103 const tracked_objects::Location& location_a2 = FROM_HERE; | |
| 104 | |
| 105 const tracked_objects::Location& location_b0 = FROM_HERE; | |
| 106 const tracked_objects::Location& location_b1 = FROM_HERE; | |
| 107 | |
| 108 const tracked_objects::Location& location_c0 = FROM_HERE; | |
| 109 | |
| 110 // Push one frame onto the stack in thread c and then pass back to thread a. | |
| 111 Closure task_a2 = base::Bind( | |
| 112 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location_a2, | |
| 113 ExpectedTrace( | |
| 114 {location_c0.program_counter(), location_b1.program_counter(), | |
| 115 location_b0.program_counter(), location_a1.program_counter()}), | |
| 116 base::Bind(&DoNothing)); | |
| 117 Closure task_c0 = base::Bind( | |
| 118 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location_c0, | |
| 119 ExpectedTrace( | |
| 120 {location_b1.program_counter(), location_b0.program_counter(), | |
| 121 location_a1.program_counter(), location_a0.program_counter()}), | |
| 122 task_a2); | |
| 123 | |
| 124 // Push one frame onto the stack in thread b then spawn two tasks, one in | |
| 125 // thread b and one in thread c using location_b1. | |
| 126 Closure task_b0_fork = | |
| 127 base::Bind(&PendingTaskTest::VerifyTraceAndPost, | |
| 128 thread_c.message_loop()->task_runner(), location_b1, | |
|
danakj
2017/02/08 18:06:23
prefer thread_c.task_runner() over thread_c.messag
| |
| 129 ExpectedTrace({location_b0.program_counter(), | |
| 130 location_a1.program_counter(), | |
| 131 location_a0.program_counter(), nullptr}), | |
| 132 task_c0); | |
| 133 Closure task_b0_local = | |
| 134 base::Bind(&PendingTaskTest::VerifyTraceAndPost, | |
| 135 thread_c.message_loop()->task_runner(), location_b1, | |
|
danakj
2017/02/08 18:06:22
should this be thread_b?
| |
| 136 ExpectedTrace({location_b0.program_counter(), | |
| 137 location_a1.program_counter(), | |
| 138 location_a0.program_counter(), nullptr}), | |
| 139 base::Bind(&DoNothing)); | |
| 140 | |
| 141 // Push one frame onto the stack in thread a then pass to thread b. | |
|
danakj
2017/02/08 18:06:22
This puts 2 frames on thread a, right? Cuz the fir
| |
| 142 Closure task_a1 = base::Bind( | |
| 143 &PendingTaskTest::VerifyTraceAndPost, | |
| 144 thread_c.message_loop()->task_runner(), location_b0, | |
|
danakj
2017/02/08 18:06:22
the comment says b but this variable says c?
| |
| 145 ExpectedTrace({location_a1.program_counter(), | |
| 146 location_a0.program_counter(), nullptr}), | |
| 147 base::Bind(&PendingTaskTest::RunTwo, task_b0_local, task_b0_fork)); | |
| 148 Closure task_a0 = base::Bind( | |
| 149 &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location_a1, | |
| 150 ExpectedTrace({location_a0.program_counter(), nullptr}), task_a1); | |
| 151 | |
| 152 loop.task_runner()->PostTask(location_a0, task_a0); | |
| 153 | |
| 154 base::RunLoop().RunUntilIdle(); | |
| 155 | |
| 156 thread_b.FlushForTesting(); | |
| 157 thread_b.Stop(); | |
| 158 | |
| 159 thread_c.FlushForTesting(); | |
| 160 thread_c.Stop(); | |
| 161 } | |
| 162 | |
| 163 } // namespace base | |
| OLD | NEW |