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

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

Issue 1681263003: metrics: Add leak detector controller in Chrome OS metrics system (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add content/public/browser to GN deps 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/metrics/leak_detector/leak_detector.h"
6
7 #include <set>
8
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace metrics {
15
16 namespace {
17
18 // Default values for LeakDetector params. See header file for the meaning of
19 // each parameter.
20 const float kDefaultSamplingRate = 1.0f;
21 const size_t kDefaultMaxCallStackUnwindDepth = 4;
22 const uint64_t kDefaultAnalysisIntervalBytes = 32 * 1024 * 1024; // 32 MiB.
23 const uint32_t kDefaultSizeSuspicionThreshold = 4;
24 const uint32_t kDefaultCallStackSuspicionThreshold = 4;
25
26 using LeakReport = LeakDetector::LeakReport;
27
28 // Observer class that receives leak reports and stores them in |reports_|.
29 // Only one copy of each unique report will be stored.
30 class TestObserver : public LeakDetector::Observer {
31 public:
32 // Contains a comparator function used to compare LeakReports for uniqueness.
33 struct Comparator {
34 bool operator()(const LeakReport& a, const LeakReport& b) const {
35 if (a.alloc_size_bytes != b.alloc_size_bytes)
36 return a.alloc_size_bytes < b.alloc_size_bytes;
37
38 return a.call_stack < b.call_stack;
39 }
40 };
41
42 TestObserver() {}
43
44 void OnLeakFound(const LeakReport& report) override {
45 reports_.insert(report);
46 }
47
48 const std::set<LeakReport, Comparator>& reports() const { return reports_; }
49
50 private:
51 // Container for all leak reports received through OnLeakFound(). Stores only
52 // one copy of each unique report.
53 std::set<LeakReport, Comparator> reports_;
54
55 DISALLOW_COPY_AND_ASSIGN(TestObserver);
56 };
57
58 } // namespace
59
60 class LeakDetectorTest : public ::testing::Test {
61 public:
62 LeakDetectorTest()
63 : detector_(new LeakDetector(kDefaultSamplingRate,
64 kDefaultMaxCallStackUnwindDepth,
65 kDefaultAnalysisIntervalBytes,
66 kDefaultSizeSuspicionThreshold,
67 kDefaultCallStackSuspicionThreshold)) {}
68
69 protected:
70 // Use a scoped_ptr to hold the test object so it can be destroyed before the
71 // test is over.
72 scoped_ptr<LeakDetector> detector_;
73
74 private:
75 // For supporting content::BrowserThread operations.
76 content::TestBrowserThreadBundle thread_bundle_;
77
78 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest);
79 };
80
81 TEST_F(LeakDetectorTest, NotifyObservers) {
82 // Generate two sets of leak reports.
83 std::vector<LeakReport> reports1(3);
84 reports1[0].alloc_size_bytes = 8;
85 reports1[0].call_stack = {1, 2, 3, 4};
86 reports1[1].alloc_size_bytes = 16;
87 reports1[1].call_stack = {5, 6, 7, 8};
88 reports1[2].alloc_size_bytes = 24;
89 reports1[2].call_stack = {9, 10, 11, 12};
90
91 std::vector<LeakReport> reports2(3);
92 reports2[0].alloc_size_bytes = 32;
93 reports2[0].call_stack = {1, 2, 4, 8};
94 reports2[1].alloc_size_bytes = 40;
95 reports2[1].call_stack = {16, 32, 64, 128};
96 reports2[2].alloc_size_bytes = 48;
97 reports2[2].call_stack = {256, 512, 1024, 2048};
98
99 // Register three observers;
100 TestObserver obs1, obs2, obs3;
101 detector_->AddObserver(&obs1);
102 detector_->AddObserver(&obs2);
103 detector_->AddObserver(&obs3);
104
105 // Pass both sets of reports to the leak detector.
106 detector_->NotifyObservers(reports1);
107 detector_->NotifyObservers(reports2);
108
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
114 // separately.
115 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) {
116 EXPECT_EQ(6U, obs->reports().size());
117 for (const auto& report : {reports1[0], reports1[1], reports1[2],
118 reports2[0], reports2[1], reports2[2]}) {
119 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end());
120 }
121 }
122 }
123
124 } // 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