OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/trace_event/memory_dump_manager.h" | 5 #include "base/trace_event/memory_dump_manager.h" |
6 | 6 |
7 #include "base/trace_event/memory_dump_provider.h" | 7 #include "base/trace_event/memory_dump_provider.h" |
8 #include "base/trace_event/process_memory_dump.h" | 8 #include "base/trace_event/process_memory_dump.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using testing::_; | 12 using testing::_; |
| 13 using testing::Return; |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace trace_event { | 16 namespace trace_event { |
16 | 17 |
17 class MemoryDumpManagerTest : public testing::Test { | 18 class MemoryDumpManagerTest : public testing::Test { |
18 public: | 19 public: |
19 void SetUp() override { | 20 void SetUp() override { |
20 mdm_.reset(new MemoryDumpManager()); | 21 mdm_.reset(new MemoryDumpManager()); |
21 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); | 22 MemoryDumpManager::SetInstanceForTesting(mdm_.get()); |
22 ASSERT_EQ(mdm_, MemoryDumpManager::GetInstance()); | 23 ASSERT_EQ(mdm_, MemoryDumpManager::GetInstance()); |
(...skipping 18 matching lines...) Expand all Loading... |
41 | 42 |
42 scoped_ptr<MemoryDumpManager> mdm_; | 43 scoped_ptr<MemoryDumpManager> mdm_; |
43 | 44 |
44 private: | 45 private: |
45 // We want our singleton torn down after each test. | 46 // We want our singleton torn down after each test. |
46 ShadowingAtExitManager at_exit_manager_; | 47 ShadowingAtExitManager at_exit_manager_; |
47 }; | 48 }; |
48 | 49 |
49 class MockDumpProvider : public MemoryDumpProvider { | 50 class MockDumpProvider : public MemoryDumpProvider { |
50 public: | 51 public: |
51 MOCK_METHOD1(DumpInto, void(ProcessMemoryDump* pmd)); | 52 MOCK_METHOD1(DumpInto, bool(ProcessMemoryDump* pmd)); |
| 53 |
| 54 const char* GetFriendlyName() const override { return "MockDumpProvider"; } |
52 }; | 55 }; |
53 | 56 |
54 TEST_F(MemoryDumpManagerTest, SingleDumper) { | 57 TEST_F(MemoryDumpManagerTest, SingleDumper) { |
55 MockDumpProvider mdp; | 58 MockDumpProvider mdp; |
56 mdm_->RegisterDumpProvider(&mdp); | 59 mdm_->RegisterDumpProvider(&mdp); |
57 | 60 |
58 // Check that the dumper is not called if the memory category is not enabled. | 61 // Check that the dumper is not called if the memory category is not enabled. |
59 EnableTracing("foo-and-bar-but-not-memory"); | 62 EnableTracing("foo-and-bar-but-not-memory"); |
60 EXPECT_CALL(mdp, DumpInto(_)).Times(0); | 63 EXPECT_CALL(mdp, DumpInto(_)).Times(0); |
61 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 64 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
62 DisableTracing(); | 65 DisableTracing(); |
63 | 66 |
64 // Now repeat enabling the memory category and check that the dumper is | 67 // Now repeat enabling the memory category and check that the dumper is |
65 // invoked this time. | 68 // invoked this time. |
66 EnableTracing(kTraceCategory); | 69 EnableTracing(kTraceCategory); |
67 EXPECT_CALL(mdp, DumpInto(_)).Times(3); | 70 EXPECT_CALL(mdp, DumpInto(_)).Times(3).WillRepeatedly(Return(true)); |
68 for (int i = 0; i < 3; ++i) | 71 for (int i = 0; i < 3; ++i) |
69 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 72 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
70 DisableTracing(); | 73 DisableTracing(); |
71 | 74 |
72 mdm_->UnregisterDumpProvider(&mdp); | 75 mdm_->UnregisterDumpProvider(&mdp); |
73 | 76 |
74 // Finally check the unregister logic (no calls to the mdp after unregister). | 77 // Finally check the unregister logic (no calls to the mdp after unregister). |
75 EnableTracing(kTraceCategory); | 78 EnableTracing(kTraceCategory); |
76 EXPECT_CALL(mdp, DumpInto(_)).Times(0); | 79 EXPECT_CALL(mdp, DumpInto(_)).Times(0); |
77 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 80 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
78 TraceLog::GetInstance()->SetDisabled(); | 81 TraceLog::GetInstance()->SetDisabled(); |
79 } | 82 } |
80 | 83 |
81 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileTracing) { | 84 TEST_F(MemoryDumpManagerTest, UnregisterDumperWhileTracing) { |
82 MockDumpProvider mdp; | 85 MockDumpProvider mdp; |
83 mdm_->RegisterDumpProvider(&mdp); | 86 mdm_->RegisterDumpProvider(&mdp); |
84 | 87 |
85 EnableTracing(kTraceCategory); | 88 EnableTracing(kTraceCategory); |
86 EXPECT_CALL(mdp, DumpInto(_)).Times(1); | 89 EXPECT_CALL(mdp, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
87 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 90 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
88 | 91 |
89 mdm_->UnregisterDumpProvider(&mdp); | 92 mdm_->UnregisterDumpProvider(&mdp); |
90 EXPECT_CALL(mdp, DumpInto(_)).Times(0); | 93 EXPECT_CALL(mdp, DumpInto(_)).Times(0); |
91 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 94 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
92 | 95 |
93 DisableTracing(); | 96 DisableTracing(); |
94 } | 97 } |
95 | 98 |
96 TEST_F(MemoryDumpManagerTest, MultipleDumpers) { | 99 TEST_F(MemoryDumpManagerTest, MultipleDumpers) { |
97 MockDumpProvider mdp1; | 100 MockDumpProvider mdp1; |
98 MockDumpProvider mdp2; | 101 MockDumpProvider mdp2; |
99 | 102 |
100 // Enable only mdp1. | 103 // Enable only mdp1. |
101 mdm_->RegisterDumpProvider(&mdp1); | 104 mdm_->RegisterDumpProvider(&mdp1); |
102 EnableTracing(kTraceCategory); | 105 EnableTracing(kTraceCategory); |
103 EXPECT_CALL(mdp1, DumpInto(_)).Times(1); | 106 EXPECT_CALL(mdp1, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
104 EXPECT_CALL(mdp2, DumpInto(_)).Times(0); | 107 EXPECT_CALL(mdp2, DumpInto(_)).Times(0); |
105 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 108 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
106 DisableTracing(); | 109 DisableTracing(); |
107 | 110 |
108 // Invert: enable mdp1 and disable mdp2. | 111 // Invert: enable mdp1 and disable mdp2. |
109 mdm_->UnregisterDumpProvider(&mdp1); | 112 mdm_->UnregisterDumpProvider(&mdp1); |
110 mdm_->RegisterDumpProvider(&mdp2); | 113 mdm_->RegisterDumpProvider(&mdp2); |
111 EnableTracing(kTraceCategory); | 114 EnableTracing(kTraceCategory); |
112 EXPECT_CALL(mdp1, DumpInto(_)).Times(0); | 115 EXPECT_CALL(mdp1, DumpInto(_)).Times(0); |
113 EXPECT_CALL(mdp2, DumpInto(_)).Times(1); | 116 EXPECT_CALL(mdp2, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
114 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 117 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
115 DisableTracing(); | 118 DisableTracing(); |
116 | 119 |
117 // Enable both mdp1 and mdp2. | 120 // Enable both mdp1 and mdp2. |
118 mdm_->RegisterDumpProvider(&mdp1); | 121 mdm_->RegisterDumpProvider(&mdp1); |
119 EnableTracing(kTraceCategory); | 122 EnableTracing(kTraceCategory); |
120 EXPECT_CALL(mdp1, DumpInto(_)).Times(1); | 123 EXPECT_CALL(mdp1, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
121 EXPECT_CALL(mdp2, DumpInto(_)).Times(1); | 124 EXPECT_CALL(mdp2, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
122 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); | 125 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
123 DisableTracing(); | 126 DisableTracing(); |
124 } | 127 } |
125 | 128 |
| 129 // Enable both dump providers, make mdp1 fail and assert that only mdp2 is |
| 130 // invoked the 2nd time. |
| 131 // FIXME(primiano): remove once crbug.com/461788 gets fixed. |
| 132 TEST_F(MemoryDumpManagerTest, DisableFailingDumpers) { |
| 133 MockDumpProvider mdp1; |
| 134 MockDumpProvider mdp2; |
| 135 |
| 136 mdm_->RegisterDumpProvider(&mdp1); |
| 137 mdm_->RegisterDumpProvider(&mdp2); |
| 138 EnableTracing(kTraceCategory); |
| 139 |
| 140 EXPECT_CALL(mdp1, DumpInto(_)).Times(1).WillRepeatedly(Return(false)); |
| 141 EXPECT_CALL(mdp2, DumpInto(_)).Times(1).WillRepeatedly(Return(true)); |
| 142 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
| 143 |
| 144 EXPECT_CALL(mdp1, DumpInto(_)).Times(0); |
| 145 EXPECT_CALL(mdp2, DumpInto(_)).Times(1).WillRepeatedly(Return(false)); |
| 146 mdm_->RequestDumpPoint(DumpPointType::EXPLICITLY_TRIGGERED); |
| 147 |
| 148 DisableTracing(); |
| 149 } |
| 150 |
126 } // namespace trace_Event | 151 } // namespace trace_Event |
127 } // namespace base | 152 } // namespace base |
OLD | NEW |