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

Unified Diff: base/trace_event/blame_context_unittest.cc

Issue 1916843003: Ensure Origin Thread Affinity for BlameContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix windows bot compile error Created 4 years, 8 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/trace_event/blame_context.cc ('k') | components/scheduler/base/task_queue_manager_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/blame_context_unittest.cc
diff --git a/base/trace_event/blame_context_unittest.cc b/base/trace_event/blame_context_unittest.cc
index 3ecf3e3fbd20a7af65bf49ff2b69fbaa79b99f45..d1da9138c8dea99616dcd5295d9032204f86f584 100644
--- a/base/trace_event/blame_context_unittest.cc
+++ b/base/trace_event/blame_context_unittest.cc
@@ -41,6 +41,8 @@ class TestBlameContext : public BlameContext {
id,
&parent) {}
+ ~TestBlameContext() override { UnregisterFromTraceLog(); }
+
protected:
void AsValueInto(trace_event::TracedValue* state) override {
BlameContext::AsValueInto(state);
@@ -57,6 +59,8 @@ class DisabledTestBlameContext : public BlameContext {
kTestBlameContextScope,
id,
nullptr) {}
+
+ ~DisabledTestBlameContext() override { UnregisterFromTraceLog(); }
};
void OnTraceDataCollected(Closure quit_closure,
@@ -73,6 +77,8 @@ class BlameContextTest : public testing::Test {
void StartTracing();
void StopTracing();
std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer();
+ protected:
+ MessageLoop loop_;
};
void BlameContextTest::StartTracing() {
@@ -215,6 +221,52 @@ TEST_F(BlameContextTest, TakeSnapshot) {
EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
}
+class TestBlameContextWithSleep : public BlameContext {
+ public:
+ TestBlameContextWithSleep()
+ : BlameContext(kTestBlameContextCategory,
+ kTestBlameContextName,
+ kTestBlameContextType,
+ kTestBlameContextScope,
+ 0,
+ nullptr),
+ callback_running_(false) {}
+
+ ~TestBlameContextWithSleep() override {
+ UnregisterFromTraceLog();
+ CHECK(!callback_running_);
+ }
+
+ void OnTraceLogEnabled() override {
+ callback_running_ = true;
+
+ // Sleep for longer time than the Sleep() in RaceWithDTOR.
+ PlatformThread::Sleep(TimeDelta::FromMilliseconds(200));
+
+ // Now the instance is already deleted, so it would crash inside.
+ BlameContext::OnTraceLogEnabled();
+ callback_running_ = false;
+ }
+
+ private:
+ bool callback_running_;
+};
+
+TEST_F(BlameContextTest, RaceWithDTOR) {
+ Thread thread("test thread");
+ CHECK(thread.Start());
+ CHECK(thread.message_loop());
+
+ std::unique_ptr<TestBlameContextWithSleep> derived;
+ derived.reset(new TestBlameContextWithSleep());
+ derived->Initialize();
+ thread.task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&BlameContextTest::StartTracing, base::Unretained(this)));
+ PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
+ derived.reset();
+}
+
} // namepace
} // namespace trace_event
} // namespace base
« no previous file with comments | « base/trace_event/blame_context.cc ('k') | components/scheduler/base/task_queue_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698