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

Unified 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: Responded to wfh's comments 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 side-by-side diff with in-line comments
Download patch
Index: components/metrics/leak_detector/leak_detector_unittest.cc
diff --git a/components/metrics/leak_detector/leak_detector_unittest.cc b/components/metrics/leak_detector/leak_detector_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a682d0d40c4c636d15860a4ba4dbd8823514bb09
--- /dev/null
+++ b/components/metrics/leak_detector/leak_detector_unittest.cc
@@ -0,0 +1,120 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/metrics/leak_detector/leak_detector.h"
+
+#include <set>
+
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace metrics {
+
+namespace {
+
+using LeakReport = LeakDetector::LeakReport;
+
+// Test class for LeakDetector that exposes protected methods.
+class TestLeakDetector : public LeakDetector {
+ public:
+ using LeakDetector::NotifyObservers;
+
+ TestLeakDetector() {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestLeakDetector);
+};
+
+// Observer class that receives leak reports and stores them in |reports_|.
+// Only one copy of each unique report will be stored.
+class TestObserver : public LeakDetector::Observer {
+ public:
+ // Contains a comparator function used to compare LeakReports for uniqueness.
+ struct Comparator {
+ bool operator()(const LeakReport& a, const LeakReport& b) const {
+ if (a.alloc_size_bytes != b.alloc_size_bytes)
+ return a.alloc_size_bytes < b.alloc_size_bytes;
+
+ return a.call_stack < b.call_stack;
+ }
+ };
+
+ TestObserver() {}
+
+ void OnLeakFound(const LeakReport& report) override {
+ reports_.insert(report);
+ }
+
+ const std::set<LeakReport, Comparator>& reports() const { return reports_; }
+
+ private:
+ // Container for all leak reports received through OnLeakFound(). Stores only
+ // one copy of each unique report.
+ std::set<LeakReport, Comparator> reports_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestObserver);
+};
+
+} // namespace
+
+class LeakDetectorTest : public ::testing::Test {
+ public:
+ LeakDetectorTest() : detector_(new TestLeakDetector) {}
+
+ protected:
+ scoped_ptr<TestLeakDetector> detector_;
+
+ private:
+ // For supporting content::BrowserThread operations.
+ content::TestBrowserThreadBundle thread_bundle_;
+
+ DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest);
+};
+
+TEST_F(LeakDetectorTest, NotifyObservers) {
+ // Generate two sets of leak reports.
+ std::vector<LeakReport> reports1(3);
+ reports1[0].alloc_size_bytes = 8;
+ reports1[0].call_stack = {1, 2, 3, 4};
+ reports1[1].alloc_size_bytes = 16;
+ reports1[1].call_stack = {5, 6, 7, 8};
+ reports1[2].alloc_size_bytes = 24;
+ reports1[2].call_stack = {9, 10, 11, 12};
+
+ std::vector<LeakReport> reports2(3);
+ reports2[0].alloc_size_bytes = 32;
+ reports2[0].call_stack = {1, 2, 4, 8};
+ reports2[1].alloc_size_bytes = 40;
+ reports2[1].call_stack = {16, 32, 64, 128};
+ reports2[2].alloc_size_bytes = 48;
+ reports2[2].call_stack = {256, 512, 1024, 2048};
+
+ // Register three observers;
+ TestObserver obs1, obs2, obs3;
+ detector_->AddObserver(&obs1);
+ detector_->AddObserver(&obs2);
+ detector_->AddObserver(&obs3);
+
+ // Pass both sets of reports to the leak detector.
+ detector_->NotifyObservers(reports1);
+ detector_->NotifyObservers(reports2);
+
+ // Shut down the leak detector before checking the reports, so that the
+ // stored reports can be examined without new reports being generated.
+ detector_.reset();
+
+ // Check that all three observers got both sets of reports, passed in
+ // separately.
+ for (const TestObserver* obs : {&obs1, &obs2, &obs3}) {
+ EXPECT_EQ(6U, obs->reports().size());
+ for (const auto& report : {reports1[0], reports1[1], reports1[2],
+ reports2[0], reports2[1], reports2[2]}) {
+ EXPECT_TRUE(obs->reports().find(report) != obs->reports().end());
+ }
+ }
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698