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

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

Powered by Google App Engine
This is Rietveld 408576698