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

Side by Side Diff: components/metrics/leak_detector/leak_detector_unittest.cc

Issue 1665553002: metrics: Connect leak detector to allocator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: primiano's feedback; Register observer sooner Created 4 years, 9 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 | « components/metrics/leak_detector/leak_detector.cc ('k') | 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 "components/metrics/leak_detector/leak_detector.h" 5 #include "components/metrics/leak_detector/leak_detector.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/allocator/allocator_extension.h"
9 #include "base/macros.h" 10 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/test/test_browser_thread_bundle.h" 11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace metrics { 14 namespace metrics {
15 15
16 namespace { 16 namespace {
17 17
18 // Default values for LeakDetector params. See header file for the meaning of 18 // Default values for LeakDetector params. See header file for the meaning of
19 // each parameter. 19 // each parameter.
20 const float kDefaultSamplingRate = 1.0f; 20 const float kDefaultSamplingRate = 1.0f;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // one copy of each unique report. 52 // one copy of each unique report.
53 std::set<LeakReport, Comparator> reports_; 53 std::set<LeakReport, Comparator> reports_;
54 54
55 DISALLOW_COPY_AND_ASSIGN(TestObserver); 55 DISALLOW_COPY_AND_ASSIGN(TestObserver);
56 }; 56 };
57 57
58 } // namespace 58 } // namespace
59 59
60 class LeakDetectorTest : public ::testing::Test { 60 class LeakDetectorTest : public ::testing::Test {
61 public: 61 public:
62 LeakDetectorTest() 62 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) {
63 : detector_(new LeakDetector(kDefaultSamplingRate, 63 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth,
64 kDefaultMaxCallStackUnwindDepth, 64 kDefaultAnalysisIntervalBytes,
65 kDefaultAnalysisIntervalBytes, 65 kDefaultSizeSuspicionThreshold,
66 kDefaultSizeSuspicionThreshold, 66 kDefaultCallStackSuspicionThreshold);
67 kDefaultCallStackSuspicionThreshold)) {} 67 }
68 68
69 protected: 69 protected:
70 // Use a scoped_ptr to hold the test object so it can be destroyed before the 70 // Points to the instance of LeakDetector returned by GetInstance().
71 // test is over. 71 LeakDetector* detector_;
72 scoped_ptr<LeakDetector> detector_;
73 72
74 private: 73 private:
75 // For supporting content::BrowserThread operations. 74 // For supporting content::BrowserThread operations.
76 content::TestBrowserThreadBundle thread_bundle_; 75 content::TestBrowserThreadBundle thread_bundle_;
77 76
78 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); 77 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest);
79 }; 78 };
80 79
81 TEST_F(LeakDetectorTest, NotifyObservers) { 80 TEST_F(LeakDetectorTest, NotifyObservers) {
82 // Generate two sets of leak reports. 81 // Generate two sets of leak reports.
(...skipping 16 matching lines...) Expand all
99 // Register three observers; 98 // Register three observers;
100 TestObserver obs1, obs2, obs3; 99 TestObserver obs1, obs2, obs3;
101 detector_->AddObserver(&obs1); 100 detector_->AddObserver(&obs1);
102 detector_->AddObserver(&obs2); 101 detector_->AddObserver(&obs2);
103 detector_->AddObserver(&obs3); 102 detector_->AddObserver(&obs3);
104 103
105 // Pass both sets of reports to the leak detector. 104 // Pass both sets of reports to the leak detector.
106 detector_->NotifyObservers(reports1); 105 detector_->NotifyObservers(reports1);
107 detector_->NotifyObservers(reports2); 106 detector_->NotifyObservers(reports2);
108 107
109 // Shut down the leak detector before checking the reports, so that the
110 // stored reports can be examined without new reports being generated.
111 detector_.reset();
112
113 // Check that all three observers got both sets of reports, passed in 108 // Check that all three observers got both sets of reports, passed in
114 // separately. 109 // separately.
115 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { 110 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) {
116 EXPECT_EQ(6U, obs->reports().size()); 111 EXPECT_EQ(6U, obs->reports().size());
117 for (const auto& report : {reports1[0], reports1[1], reports1[2], 112 for (const auto& report : {reports1[0], reports1[1], reports1[2],
118 reports2[0], reports2[1], reports2[2]}) { 113 reports2[0], reports2[1], reports2[2]}) {
119 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); 114 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end());
120 } 115 }
121 } 116 }
122 } 117 }
123 118
124 } // namespace metrics 119 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/leak_detector.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698