OLD | NEW |
---|---|
(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 // Observer class that receives leak reports and stores them in |reports_|. | |
18 // Only one copy of each unique report will be stored. | |
19 class TestObserver : public LeakDetector::LeakDetector::Observer { | |
20 public: | |
21 TestObserver() {} | |
22 | |
23 void OnLeakFound(const LeakDetector::LeakReport& report) override { | |
24 reports_.insert(report); | |
25 } | |
26 | |
27 const std::set<LeakDetector::LeakReport>& reports() const { return reports_; } | |
28 | |
29 private: | |
30 std::set<LeakDetector::LeakReport> reports_; | |
31 | |
32 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
33 }; | |
34 | |
35 } // namespace | |
36 | |
37 class LeakDetectorTest : public ::testing::Test { | |
38 public: | |
39 LeakDetectorTest() {} | |
40 | |
41 void SetUp() override { detector_.reset(new LeakDetector); } | |
Alexei Svitkine (slow)
2016/02/10 20:16:58
You can just put this in the ctor and avoid having
Simon Que
2016/02/11 01:45:38
I actually call "detector_.reset()" in the second
| |
42 | |
43 void TearDown() override { detector_.reset(); } | |
44 | |
45 protected: | |
46 scoped_ptr<LeakDetector> detector_; | |
47 | |
48 protected: | |
Alexei Svitkine (slow)
2016/02/10 20:16:58
private:
Simon Que
2016/02/11 01:45:38
Done.
| |
49 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); | |
50 }; | |
51 | |
52 TEST_F(LeakDetectorTest, AddAndRemoveObservers) { | |
53 TestObserver obs1, obs2, obs3; | |
54 | |
55 // There are no registered observers at first. Make sure RemoveObserver() | |
56 // returns false in this case. | |
57 EXPECT_FALSE(detector_->RemoveObserver(&obs1)); | |
58 EXPECT_FALSE(detector_->RemoveObserver(&obs2)); | |
59 EXPECT_FALSE(detector_->RemoveObserver(&obs3)); | |
60 | |
61 // Add observer and then remove it. | |
62 EXPECT_TRUE(detector_->AddObserver(&obs1)); | |
63 EXPECT_TRUE(detector_->RemoveObserver(&obs1)); | |
64 EXPECT_FALSE(detector_->RemoveObserver(&obs1)); | |
65 | |
66 // Add all three observers at once, then remove them. | |
67 EXPECT_TRUE(detector_->AddObserver(&obs1)); | |
68 EXPECT_TRUE(detector_->AddObserver(&obs2)); | |
69 EXPECT_TRUE(detector_->AddObserver(&obs3)); | |
70 | |
71 EXPECT_TRUE(detector_->RemoveObserver(&obs1)); | |
72 EXPECT_FALSE(detector_->RemoveObserver(&obs1)); | |
73 EXPECT_TRUE(detector_->RemoveObserver(&obs2)); | |
74 EXPECT_FALSE(detector_->RemoveObserver(&obs2)); | |
75 EXPECT_TRUE(detector_->RemoveObserver(&obs3)); | |
76 EXPECT_FALSE(detector_->RemoveObserver(&obs3)); | |
77 } | |
78 | |
79 TEST_F(LeakDetectorTest, NotifyObservers) { | |
80 // Generate two sets of leak reports. | |
81 std::vector<LeakDetector::LeakReport> reports1(3); | |
82 reports1[0].alloc_size_bytes = 8; | |
83 reports1[0].call_stack = {1, 2, 3, 4}; | |
84 reports1[1].alloc_size_bytes = 16; | |
85 reports1[1].call_stack = {5, 6, 7, 8}; | |
86 reports1[2].alloc_size_bytes = 24; | |
87 reports1[2].call_stack = {9, 10, 11, 12}; | |
88 | |
89 std::vector<LeakDetector::LeakReport> reports2(3); | |
90 reports2[0].alloc_size_bytes = 32; | |
91 reports2[0].call_stack = {1, 2, 4, 8}; | |
92 reports2[1].alloc_size_bytes = 40; | |
93 reports2[1].call_stack = {16, 32, 64, 128}; | |
94 reports2[2].alloc_size_bytes = 48; | |
95 reports2[2].call_stack = {256, 512, 1024, 2048}; | |
96 | |
97 // Register three observers; | |
98 TestObserver obs1, obs2, obs3; | |
99 ASSERT_TRUE(detector_->AddObserver(&obs1)); | |
100 ASSERT_TRUE(detector_->AddObserver(&obs2)); | |
101 ASSERT_TRUE(detector_->AddObserver(&obs3)); | |
102 | |
103 // Pass both sets of reports to the leak detector. | |
104 detector_->NotifyObservers(reports1); | |
105 detector_->NotifyObservers(reports2); | |
106 | |
107 // Shut down the leak detector before checking the reports, so that the | |
108 // stored reports can be examined without new reports being generated. | |
109 detector_.reset(); | |
110 | |
111 // Check that all three observers got both sets of reports, passed in | |
112 // separately. | |
113 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { | |
114 EXPECT_EQ(6U, obs->reports().size()); | |
115 for (const auto& report : {reports1[0], reports1[1], reports1[2], | |
116 reports2[0], reports2[1], reports2[2]}) { | |
117 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); | |
118 } | |
119 } | |
120 } | |
121 | |
122 } // namespace metrics | |
OLD | NEW |