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

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: Properly implement gn build Created 4 years, 10 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
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 "testing/gtest/include/gtest/gtest.h"
12
13 namespace metrics {
14
15 namespace {
16
17 using LeakReport = LeakDetector::LeakReport;
18
19 // Test class for LeakDetector that exposes protected methods.
20 class TestLeakDetector : public LeakDetector {
21 public:
22 using LeakDetector::NotifyObservers;
23
24 TestLeakDetector() {}
25
26 private:
27 DISALLOW_COPY_AND_ASSIGN(TestLeakDetector);
28 };
29
30 // Observer class that receives leak reports and stores them in |reports_|.
31 // Only one copy of each unique report will be stored.
32 class TestObserver : public LeakDetector::Observer {
33 public:
34 // Contains a comparator function used to compare LeakReports for uniqueness.
35 struct Comparator {
36 bool operator()(const LeakReport& a, const LeakReport& b) const {
37 if (a.alloc_size_bytes != b.alloc_size_bytes)
38 return a.alloc_size_bytes < b.alloc_size_bytes;
39
40 return a.call_stack < b.call_stack;
41 }
42 };
43
44 TestObserver() {}
45
46 void OnLeakFound(const LeakReport& report) override {
47 reports_.insert(report);
48 }
49
50 const std::set<LeakReport, Comparator>& reports() const { return reports_; }
51
52 private:
53 // Container for all leak reports received through OnLeakFound(). Stores only
54 // one copy of each unique report.
55 std::set<LeakReport, Comparator> reports_;
56
57 DISALLOW_COPY_AND_ASSIGN(TestObserver);
58 };
59
60 } // namespace
61
62 class LeakDetectorTest : public ::testing::Test {
63 public:
64 LeakDetectorTest() : detector_(new TestLeakDetector) {}
65
66 protected:
67 scoped_ptr<TestLeakDetector> detector_;
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest);
71 };
72
73 TEST_F(LeakDetectorTest, NotifyObservers) {
74 // Generate two sets of leak reports.
75 std::vector<LeakReport> reports1(3);
76 reports1[0].alloc_size_bytes = 8;
77 reports1[0].call_stack = {1, 2, 3, 4};
78 reports1[1].alloc_size_bytes = 16;
79 reports1[1].call_stack = {5, 6, 7, 8};
80 reports1[2].alloc_size_bytes = 24;
81 reports1[2].call_stack = {9, 10, 11, 12};
82
83 std::vector<LeakReport> reports2(3);
84 reports2[0].alloc_size_bytes = 32;
85 reports2[0].call_stack = {1, 2, 4, 8};
86 reports2[1].alloc_size_bytes = 40;
87 reports2[1].call_stack = {16, 32, 64, 128};
88 reports2[2].alloc_size_bytes = 48;
89 reports2[2].call_stack = {256, 512, 1024, 2048};
90
91 // Register three observers;
92 TestObserver obs1, obs2, obs3;
93 detector_->AddObserver(&obs1);
94 detector_->AddObserver(&obs2);
95 detector_->AddObserver(&obs3);
96
97 // Pass both sets of reports to the leak detector.
98 detector_->NotifyObservers(reports1);
99 detector_->NotifyObservers(reports2);
100
101 // Shut down the leak detector before checking the reports, so that the
102 // stored reports can be examined without new reports being generated.
103 detector_.reset();
104
105 // Check that all three observers got both sets of reports, passed in
106 // separately.
107 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) {
108 EXPECT_EQ(6U, obs->reports().size());
109 for (const auto& report : {reports1[0], reports1[1], reports1[2],
110 reports2[0], reports2[1], reports2[2]}) {
111 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end());
112 }
113 }
114 }
115
116 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698