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

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

Powered by Google App Engine
This is Rietveld 408576698