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

Unified Diff: base/pending_task_unittest.cc

Issue 1044413002: Record async "task backtraces" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: c++11 is hard; let's go shopping. Created 3 years, 10 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
« no previous file with comments | « base/pending_task.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/pending_task_unittest.cc
diff --git a/base/pending_task_unittest.cc b/base/pending_task_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a8269452aba9949eca7739f4b02b0476c1f26ac8
--- /dev/null
+++ b/base/pending_task_unittest.cc
@@ -0,0 +1,166 @@
+// Copyright (c) 2017 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/pending_task.h"
+
+#include <vector>
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "base/threading/thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+class PendingTaskTest : public ::testing::Test {
+ public:
+ PendingTaskTest() {}
+
+ ~PendingTaskTest() override {}
+
+ protected:
+ typedef std::vector<const void*> ExpectedTrace;
+
+ static void VerifyTraceAndPost(
+ const scoped_refptr<TaskRunner>& task_runner,
+ const tracked_objects::Location& posted_from,
+ const tracked_objects::Location& next_from_here,
+ const std::vector<const void*>& expected_trace,
+ Closure task) {
+ SCOPED_TRACE(StringPrintf("Callback Depth: %zu", expected_trace.size()));
+
+ // Beyond depth + 1, the trace is nonsensical because there haven't been
+ // enough nested tasks called.
+ size_t window = std::min(base::MessageLoop::current()
+ ->current_pending_task_->task_backtrace.size(),
danakj 2017/02/09 20:34:42 nit: this would probably wrap a lot nicer if u put
awong 2017/02/09 20:56:06 Done.
+ expected_trace.size());
+
+ EXPECT_EQ(posted_from,
+ base::MessageLoop::current()->current_pending_task_->posted_from);
+ for (size_t i = 0; i < window; i++) {
+ SCOPED_TRACE(StringPrintf("Trace frame: %zu", i));
+ EXPECT_EQ(expected_trace[i],
+ base::MessageLoop::current()
+ ->current_pending_task_->task_backtrace[i]);
+ }
+ task_runner->PostTask(next_from_here, std::move(task));
+ }
+
+ static void RunTwo(Closure c1, Closure c2) {
+ c1.Run();
+ c2.Run();
+ }
+};
+
+// Ensure the task backtrace populates correctly.
+TEST_F(PendingTaskTest, SingleThreadedSimple) {
+ base::MessageLoop loop;
+ const tracked_objects::Location& location0 = FROM_HERE;
+ const tracked_objects::Location& location1 = FROM_HERE;
+ const tracked_objects::Location& location2 = FROM_HERE;
+ const tracked_objects::Location& location3 = FROM_HERE;
+ const tracked_objects::Location& location4 = FROM_HERE;
+ const tracked_objects::Location& location5 = FROM_HERE;
+
+ Closure task5 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location4,
+ location5,
+ ExpectedTrace({location3.program_counter(), location2.program_counter(),
+ location1.program_counter(), location0.program_counter()}),
+ base::Bind(&DoNothing));
+ Closure task4 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location3,
+ location4,
+ ExpectedTrace({location2.program_counter(), location1.program_counter(),
+ location0.program_counter(), nullptr}),
+ task5);
+ Closure task3 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location2,
+ location3, ExpectedTrace({location1.program_counter(),
+ location0.program_counter(), nullptr, nullptr}),
+ task4);
+ Closure task2 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location1,
+ location2, ExpectedTrace({location0.program_counter()}), task3);
+ Closure task1 =
+ base::Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
+ location0, location1, ExpectedTrace({}), task2);
+
+ loop.task_runner()->PostTask(location0, task1);
+
+ base::RunLoop().RunUntilIdle();
+}
+
+// Post a task onto another thread. Ensure on the other thread, it has the
+// right stack trace.
+TEST_F(PendingTaskTest, MultipleThreads) {
+ base::MessageLoop loop; // Implicitly "thread a"
+ base::Thread thread_b("pt_test_b");
+ base::Thread thread_c("pt_test_c");
+ thread_b.StartAndWaitForTesting();
+ thread_c.StartAndWaitForTesting();
+
+ const tracked_objects::Location& location_a0 = FROM_HERE;
+ const tracked_objects::Location& location_a1 = FROM_HERE;
+ const tracked_objects::Location& location_a2 = FROM_HERE;
+
+ const tracked_objects::Location& location_b0 = FROM_HERE;
+ const tracked_objects::Location& location_b1 = FROM_HERE;
+
+ const tracked_objects::Location& location_c0 = FROM_HERE;
+
+ // Push one frame onto the stack in thread c and then pass back to thread a.
+ Closure task_a2 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location_c0,
+ location_a2,
+ ExpectedTrace(
+ {location_b1.program_counter(), location_b0.program_counter(),
+ location_a1.program_counter(), location_a0.program_counter()}),
+ base::Bind(&DoNothing));
+ Closure task_c0 = base::Bind(&PendingTaskTest::VerifyTraceAndPost,
+ loop.task_runner(), location_b1, location_c0,
+ ExpectedTrace({location_b0.program_counter(),
+ location_a1.program_counter(),
+ location_a0.program_counter()}),
+ task_a2);
+
+ // Push one frame onto the stack in thread b then spawn two tasks, one in
+ // thread b and one in thread c using location_b1.
+ Closure task_b0_fork = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost,
+ thread_c.message_loop()->task_runner(), location_b0, location_b1,
+ ExpectedTrace({location_a1.program_counter(),
+ location_a0.program_counter(), nullptr}),
+ task_c0);
+ Closure task_b0_local = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost,
+ thread_c.message_loop()->task_runner(), location_b0, location_b1,
+ ExpectedTrace({location_a1.program_counter(),
+ location_a0.program_counter(), nullptr}),
+ base::Bind(&DoNothing));
+
+ // Push one frame onto the stack in thread a then pass to thread b.
+ Closure task_a1 = base::Bind(
+ &PendingTaskTest::VerifyTraceAndPost,
+ thread_c.message_loop()->task_runner(), location_a1, location_b0,
+ ExpectedTrace({location_a0.program_counter(), nullptr}),
+ base::Bind(&PendingTaskTest::RunTwo, task_b0_local, task_b0_fork));
+ Closure task_a0 =
+ base::Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
+ location_a0, location_a1, ExpectedTrace({nullptr}), task_a1);
+
+ loop.task_runner()->PostTask(location_a0, task_a0);
+
+ base::RunLoop().RunUntilIdle();
+
+ thread_b.FlushForTesting();
+ thread_b.Stop();
+
+ thread_c.FlushForTesting();
+ thread_c.Stop();
+}
+
+} // namespace base
« no previous file with comments | « base/pending_task.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698