| 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/trace_event/blame_context.h" | 5 #include "base/trace_event/blame_context.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 nullptr) {} | 34 nullptr) {} |
| 35 | 35 |
| 36 TestBlameContext(int id, const TestBlameContext& parent) | 36 TestBlameContext(int id, const TestBlameContext& parent) |
| 37 : BlameContext(kTestBlameContextCategory, | 37 : BlameContext(kTestBlameContextCategory, |
| 38 kTestBlameContextName, | 38 kTestBlameContextName, |
| 39 kTestBlameContextType, | 39 kTestBlameContextType, |
| 40 kTestBlameContextScope, | 40 kTestBlameContextScope, |
| 41 id, | 41 id, |
| 42 &parent) {} | 42 &parent) {} |
| 43 | 43 |
| 44 ~TestBlameContext() override { UnregisterFromTraceLog(); } |
| 45 |
| 44 protected: | 46 protected: |
| 45 void AsValueInto(trace_event::TracedValue* state) override { | 47 void AsValueInto(trace_event::TracedValue* state) override { |
| 46 BlameContext::AsValueInto(state); | 48 BlameContext::AsValueInto(state); |
| 47 state->SetBoolean("crossStreams", false); | 49 state->SetBoolean("crossStreams", false); |
| 48 } | 50 } |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 class DisabledTestBlameContext : public BlameContext { | 53 class DisabledTestBlameContext : public BlameContext { |
| 52 public: | 54 public: |
| 53 explicit DisabledTestBlameContext(int id) | 55 explicit DisabledTestBlameContext(int id) |
| 54 : BlameContext(kDisabledTestBlameContextCategory, | 56 : BlameContext(kDisabledTestBlameContextCategory, |
| 55 kTestBlameContextName, | 57 kTestBlameContextName, |
| 56 kTestBlameContextType, | 58 kTestBlameContextType, |
| 57 kTestBlameContextScope, | 59 kTestBlameContextScope, |
| 58 id, | 60 id, |
| 59 nullptr) {} | 61 nullptr) {} |
| 62 |
| 63 ~DisabledTestBlameContext() override { UnregisterFromTraceLog(); } |
| 60 }; | 64 }; |
| 61 | 65 |
| 62 void OnTraceDataCollected(Closure quit_closure, | 66 void OnTraceDataCollected(Closure quit_closure, |
| 63 trace_event::TraceResultBuffer* buffer, | 67 trace_event::TraceResultBuffer* buffer, |
| 64 const scoped_refptr<RefCountedString>& json, | 68 const scoped_refptr<RefCountedString>& json, |
| 65 bool has_more_events) { | 69 bool has_more_events) { |
| 66 buffer->AddFragment(json->data()); | 70 buffer->AddFragment(json->data()); |
| 67 if (!has_more_events) | 71 if (!has_more_events) |
| 68 quit_closure.Run(); | 72 quit_closure.Run(); |
| 69 } | 73 } |
| 70 | 74 |
| 71 class BlameContextTest : public testing::Test { | 75 class BlameContextTest : public testing::Test { |
| 72 public: | 76 public: |
| 73 void StartTracing(); | 77 void StartTracing(); |
| 74 void StopTracing(); | 78 void StopTracing(); |
| 75 std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer(); | 79 std::unique_ptr<trace_analyzer::TraceAnalyzer> CreateTraceAnalyzer(); |
| 80 protected: |
| 81 MessageLoop loop_; |
| 76 }; | 82 }; |
| 77 | 83 |
| 78 void BlameContextTest::StartTracing() { | 84 void BlameContextTest::StartTracing() { |
| 79 trace_event::TraceLog::GetInstance()->SetEnabled( | 85 trace_event::TraceLog::GetInstance()->SetEnabled( |
| 80 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE); | 86 trace_event::TraceConfig("*"), trace_event::TraceLog::RECORDING_MODE); |
| 81 } | 87 } |
| 82 | 88 |
| 83 void BlameContextTest::StopTracing() { | 89 void BlameContextTest::StopTracing() { |
| 84 trace_event::TraceLog::GetInstance()->SetDisabled(); | 90 trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 85 } | 91 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 "\"id_ref\":\"0x5678\"," | 214 "\"id_ref\":\"0x5678\"," |
| 209 "\"scope\":\"TestBlameContextScope\"" | 215 "\"scope\":\"TestBlameContextScope\"" |
| 210 "}" | 216 "}" |
| 211 "}"; | 217 "}"; |
| 212 | 218 |
| 213 std::string snapshot_json; | 219 std::string snapshot_json; |
| 214 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json); | 220 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json); |
| 215 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); | 221 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); |
| 216 } | 222 } |
| 217 | 223 |
| 224 class TestBlameContextWithSleep : public BlameContext { |
| 225 public: |
| 226 TestBlameContextWithSleep() |
| 227 : BlameContext(kTestBlameContextCategory, |
| 228 kTestBlameContextName, |
| 229 kTestBlameContextType, |
| 230 kTestBlameContextScope, |
| 231 0, |
| 232 nullptr), |
| 233 callback_running_(false) {} |
| 234 |
| 235 ~TestBlameContextWithSleep() override { |
| 236 UnregisterFromTraceLog(); |
| 237 CHECK(!callback_running_); |
| 238 } |
| 239 |
| 240 void OnTraceLogEnabled() override { |
| 241 callback_running_ = true; |
| 242 |
| 243 // Sleep for longer time than the Sleep() in RaceWithDTOR. |
| 244 PlatformThread::Sleep(TimeDelta::FromMilliseconds(200)); |
| 245 |
| 246 // Now the instance is already deleted, so it would crash inside. |
| 247 BlameContext::OnTraceLogEnabled(); |
| 248 callback_running_ = false; |
| 249 } |
| 250 |
| 251 private: |
| 252 bool callback_running_; |
| 253 }; |
| 254 |
| 255 TEST_F(BlameContextTest, RaceWithDTOR) { |
| 256 Thread thread("test thread"); |
| 257 CHECK(thread.Start()); |
| 258 CHECK(thread.message_loop()); |
| 259 |
| 260 std::unique_ptr<TestBlameContextWithSleep> derived; |
| 261 derived.reset(new TestBlameContextWithSleep()); |
| 262 derived->Initialize(); |
| 263 thread.task_runner()->PostTask( |
| 264 FROM_HERE, |
| 265 base::Bind(&BlameContextTest::StartTracing, base::Unretained(this))); |
| 266 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100)); |
| 267 derived.reset(); |
| 268 } |
| 269 |
| 218 } // namepace | 270 } // namepace |
| 219 } // namespace trace_event | 271 } // namespace trace_event |
| 220 } // namespace base | 272 } // namespace base |
| OLD | NEW |