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

Side by Side Diff: base/trace_event/blame_context_unittest.cc

Issue 1921973003: Proof of concept about BlameContext races on dtor. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <unistd.h>
8
7 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
8 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
9 #include "base/run_loop.h" 11 #include "base/run_loop.h"
10 #include "base/test/trace_event_analyzer.h" 12 #include "base/test/trace_event_analyzer.h"
11 #include "base/trace_event/trace_buffer.h" 13 #include "base/trace_event/trace_buffer.h"
12 #include "base/trace_event/trace_event_argument.h" 14 #include "base/trace_event/trace_event_argument.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 namespace base { 17 namespace base {
16 namespace trace_event { 18 namespace trace_event {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 "\"id_ref\":\"0x5678\"," 206 "\"id_ref\":\"0x5678\","
205 "\"scope\":\"TestBlameContextScope\"" 207 "\"scope\":\"TestBlameContextScope\""
206 "}" 208 "}"
207 "}"; 209 "}";
208 210
209 std::string snapshot_json; 211 std::string snapshot_json;
210 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json); 212 JSONWriter::Write(*events[2]->GetKnownArgAsValue("snapshot"), &snapshot_json);
211 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json); 213 EXPECT_EQ(kExpectedSnapshotJson, snapshot_json);
212 } 214 }
213 215
216 class TestBlameContextWithSleep : public BlameContext {
217 public:
218 TestBlameContextWithSleep()
219 : BlameContext(kTestBlameContextCategory,
220 kTestBlameContextName,
221 kTestBlameContextType,
222 kTestBlameContextScope,
223 0,
224 nullptr), destroyed_(false) {}
225
226 ~TestBlameContextWithSleep() override {
227 destroyed_ = true;
228 usleep(200000); //anything longer than the PostDelayedTask below.
229 }
230
231 protected:
232 void AsValueInto(trace_event::TracedValue* state) override {
233 CHECK(!destroyed_) << "KABOOM, calling the derived AsValueInto after its "
234 "dtor. Weird things will happen now.";
235 }
236
237 bool destroyed_;
238 };
239
240 TEST_F(BlameContextTest, DTOR) {
241 Thread thread("test thread");
242 thread.Start();
243 thread.task_runner()->PostDelayedTask(
244 FROM_HERE,
245 base::Bind(&BlameContextTest::StartTracing, base::Unretained(this)),
246 TimeDelta::FromMicroseconds(100000));
247 std::unique_ptr<TestBlameContextWithSleep> derived(new TestBlameContextWithSle ep());
248 derived->Initialize();
249 derived.reset();
250 }
251
214 } // namepace 252 } // namepace
215 } // namespace trace_event 253 } // namespace trace_event
216 } // namespace base 254 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698