Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/leak_detector/leak_detector.h" | 5 #include "components/metrics/leak_detector/leak_detector.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/allocator/allocator_extension.h" | |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace metrics { | 15 namespace metrics { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Default values for LeakDetector params. See header file for the meaning of | 19 // Default values for LeakDetector params. See header file for the meaning of |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 // one copy of each unique report. | 53 // one copy of each unique report. |
| 53 std::set<LeakReport, Comparator> reports_; | 54 std::set<LeakReport, Comparator> reports_; |
| 54 | 55 |
| 55 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 56 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 } // namespace | 59 } // namespace |
| 59 | 60 |
| 60 class LeakDetectorTest : public ::testing::Test { | 61 class LeakDetectorTest : public ::testing::Test { |
| 61 public: | 62 public: |
| 62 LeakDetectorTest() | 63 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) { |
| 63 : detector_(new LeakDetector(kDefaultSamplingRate, | 64 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth, |
| 64 kDefaultMaxCallStackUnwindDepth, | 65 kDefaultAnalysisIntervalBytes, |
| 65 kDefaultAnalysisIntervalBytes, | 66 kDefaultSizeSuspicionThreshold, |
| 66 kDefaultSizeSuspicionThreshold, | 67 kDefaultCallStackSuspicionThreshold); |
| 67 kDefaultCallStackSuspicionThreshold)) {} | 68 } |
| 68 | 69 |
| 69 protected: | 70 protected: |
| 70 // Use a scoped_ptr to hold the test object so it can be destroyed before the | 71 // Use a scoped_ptr to hold the test object so it can be destroyed before the |
|
Will Harris
2016/03/12 01:44:22
nit:update comment
Simon Que
2016/03/14 19:17:01
Done.
| |
| 71 // test is over. | 72 // test is over. |
| 72 scoped_ptr<LeakDetector> detector_; | 73 LeakDetector* detector_; |
| 73 | 74 |
| 74 private: | 75 private: |
| 75 // For supporting content::BrowserThread operations. | 76 // For supporting content::BrowserThread operations. |
| 76 content::TestBrowserThreadBundle thread_bundle_; | 77 content::TestBrowserThreadBundle thread_bundle_; |
| 77 | 78 |
| 78 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); | 79 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 TEST_F(LeakDetectorTest, NotifyObservers) { | 82 TEST_F(LeakDetectorTest, NotifyObservers) { |
| 82 // Generate two sets of leak reports. | 83 // Generate two sets of leak reports. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 99 // Register three observers; | 100 // Register three observers; |
| 100 TestObserver obs1, obs2, obs3; | 101 TestObserver obs1, obs2, obs3; |
| 101 detector_->AddObserver(&obs1); | 102 detector_->AddObserver(&obs1); |
| 102 detector_->AddObserver(&obs2); | 103 detector_->AddObserver(&obs2); |
| 103 detector_->AddObserver(&obs3); | 104 detector_->AddObserver(&obs3); |
| 104 | 105 |
| 105 // Pass both sets of reports to the leak detector. | 106 // Pass both sets of reports to the leak detector. |
| 106 detector_->NotifyObservers(reports1); | 107 detector_->NotifyObservers(reports1); |
| 107 detector_->NotifyObservers(reports2); | 108 detector_->NotifyObservers(reports2); |
| 108 | 109 |
| 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 | 110 // Check that all three observers got both sets of reports, passed in |
| 114 // separately. | 111 // separately. |
| 115 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { | 112 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { |
| 116 EXPECT_EQ(6U, obs->reports().size()); | 113 EXPECT_EQ(6U, obs->reports().size()); |
| 117 for (const auto& report : {reports1[0], reports1[1], reports1[2], | 114 for (const auto& report : {reports1[0], reports1[1], reports1[2], |
| 118 reports2[0], reports2[1], reports2[2]}) { | 115 reports2[0], reports2[1], reports2[2]}) { |
| 119 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); | 116 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); |
| 120 } | 117 } |
| 121 } | 118 } |
| 122 } | 119 } |
| 123 | 120 |
| 124 } // namespace metrics | 121 } // namespace metrics |
| OLD | NEW |