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

Side by Side Diff: chrome/browser/metrics/metrics_service_browsertest.cc

Issue 2697423002: Add test for CHECK exit code behavior. (Closed)
Patch Set: code review changes part deux Created 3 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 unified diff | Download patch
« no previous file with comments | « no previous file | content/browser/frame_host/debug_urls.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // Tests the MetricsService stat recording to make sure that the numbers are 5 // Tests the MetricsService stat recording to make sure that the numbers are
6 // what we expect. 6 // what we expect.
7 7
8 #include "components/metrics/metrics_service.h" 8 #include "components/metrics/metrics_service.h"
9 9
10 #include <string> 10 #include <string>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 } // namespace 58 } // namespace
59 #endif // OS_MACOSX || OS_LINUX 59 #endif // OS_MACOSX || OS_LINUX
60 60
61 class MetricsServiceBrowserTest : public InProcessBrowserTest { 61 class MetricsServiceBrowserTest : public InProcessBrowserTest {
62 public: 62 public:
63 void SetUpCommandLine(base::CommandLine* command_line) override { 63 void SetUpCommandLine(base::CommandLine* command_line) override {
64 // Enable the metrics service for testing (in recording-only mode). 64 // Enable the metrics service for testing (in recording-only mode).
65 command_line->AppendSwitch(switches::kMetricsRecordingOnly); 65 command_line->AppendSwitch(switches::kMetricsRecordingOnly);
66 } 66 }
67 67
68 // Open three tabs then navigate to |crashy_url| and wait for the renderer to
69 // crash.
70 void OpenTabsAndNavigateToCrashyUrl(const std::string& crashy_url) {
71 // Opens three tabs.
72 OpenThreeTabs();
73
74 // Kill the process for one of the tabs by navigating to |crashy_url|.
75 content::RenderProcessHostWatcher observer(
76 browser()->tab_strip_model()->GetActiveWebContents(),
77 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
78 // Opens one tab.
79 ui_test_utils::NavigateToURL(browser(), GURL(crashy_url));
80 observer.Wait();
81
82 // The MetricsService listens for the same notification, so the |observer|
83 // might finish waiting before the MetricsService has a chance to process
84 // the notification. To avoid racing here, we repeatedly run the message
85 // loop until the MetricsService catches up. This should happen "real soon
86 // now", since the notification is posted to all observers essentially
87 // simultaneously... so busy waiting here shouldn't be too bad.
88 const PrefService* prefs = g_browser_process->local_state();
89 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) {
90 content::RunAllPendingInMessageLoop();
91 }
92 }
93
68 // Open a couple of tabs of random content. 94 // Open a couple of tabs of random content.
69 void OpenTabs() { 95 //
96 // Calling this method causes three page load events:
97 // 1. title2.html
98 // 2. iframe.html
99 // 3. title1.html (iframed by iframe.html)
100 void OpenThreeTabs() {
70 const int kBrowserTestFlags = 101 const int kBrowserTestFlags =
71 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | 102 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
72 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION; 103 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION;
73 104
74 base::FilePath test_directory; 105 base::FilePath test_directory;
75 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory)); 106 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_directory));
76 107
77 base::FilePath page1_path = test_directory.AppendASCII("title2.html"); 108 base::FilePath page1_path = test_directory.AppendASCII("title2.html");
78 ui_test_utils::NavigateToURLWithDisposition( 109 ui_test_utils::NavigateToURLWithDisposition(
79 browser(), net::FilePathToFileURL(page1_path), 110 browser(), net::FilePathToFileURL(page1_path),
80 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); 111 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags);
81 112
82 base::FilePath page2_path = test_directory.AppendASCII("iframe.html"); 113 base::FilePath page2_path = test_directory.AppendASCII("iframe.html");
83 ui_test_utils::NavigateToURLWithDisposition( 114 ui_test_utils::NavigateToURLWithDisposition(
84 browser(), net::FilePathToFileURL(page2_path), 115 browser(), net::FilePathToFileURL(page2_path),
85 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags); 116 WindowOpenDisposition::NEW_FOREGROUND_TAB, kBrowserTestFlags);
86 } 117 }
87 }; 118 };
88 119
89 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) { 120 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, CloseRenderersNormally) {
90 OpenTabs(); 121 OpenThreeTabs();
91 122
92 // Verify that the expected stability metrics were recorded. 123 // Verify that the expected stability metrics were recorded.
93 const PrefService* prefs = g_browser_process->local_state(); 124 const PrefService* prefs = g_browser_process->local_state();
94 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); 125 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
95 EXPECT_EQ(3, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); 126 EXPECT_EQ(3, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount));
96 EXPECT_EQ(0, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); 127 EXPECT_EQ(0, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount));
97 // TODO(isherman): We should also verify that 128 // TODO(isherman): We should also verify that
98 // metrics::prefs::kStabilityExitedCleanly 129 // metrics::prefs::kStabilityExitedCleanly
99 // is set to true, but this preference isn't set until the browser 130 // is set to true, but this preference isn't set until the browser
100 // exits... it's not clear to me how to test that. 131 // exits... it's not clear to me how to test that.
101 } 132 }
102 133
103 // Flaky on Linux. See http://crbug.com/131094 134 // Flaky on Linux. See http://crbug.com/131094
104 // Child crashes fail the process on ASan (see crbug.com/411251, 135 // Child crashes fail the process on ASan (see crbug.com/411251,
105 // crbug.com/368525). 136 // crbug.com/368525).
106 #if defined(OS_LINUX) || defined(ADDRESS_SANITIZER) 137 #if defined(OS_LINUX) || defined(ADDRESS_SANITIZER)
107 #define MAYBE_CrashRenderers DISABLED_CrashRenderers 138 #define MAYBE_CrashRenderers DISABLED_CrashRenderers
139 #define MAYBE_CheckCrashRenderers DISABLED_CheckCrashRenderers
108 #else 140 #else
109 #define MAYBE_CrashRenderers CrashRenderers 141 #define MAYBE_CrashRenderers CrashRenderers
142 #define MAYBE_CheckCrashRenderers CheckCrashRenderers
110 #endif 143 #endif
144
111 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) { 145 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CrashRenderers) {
112 base::HistogramTester histogram_tester; 146 base::HistogramTester histogram_tester;
113 OpenTabs();
114 147
115 // Kill the process for one of the tabs. 148 OpenTabsAndNavigateToCrashyUrl(content::kChromeUICrashURL);
116 content::RenderProcessHostWatcher observer(
117 browser()->tab_strip_model()->GetActiveWebContents(),
118 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
119 ui_test_utils::NavigateToURL(browser(), GURL(content::kChromeUICrashURL));
120 observer.Wait();
121
122 // The MetricsService listens for the same notification, so the |observer|
123 // might finish waiting before the MetricsService has a chance to process the
124 // notification. To avoid racing here, we repeatedly run the message loop
125 // until the MetricsService catches up. This should happen "real soon now",
126 // since the notification is posted to all observers essentially
127 // simultaneously... so busy waiting here shouldn't be too bad.
128 const PrefService* prefs = g_browser_process->local_state();
129 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) {
130 content::RunAllPendingInMessageLoop();
131 }
132 149
133 // Verify that the expected stability metrics were recorded. 150 // Verify that the expected stability metrics were recorded.
151 const PrefService* prefs = g_browser_process->local_state();
134 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); 152 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
135 // Expect four page loads: 153 // The three tabs from OpenTabs() and the one tab to open chrome://crash/.
136 // 1. title2.html
137 // 2. iframe.html
138 // 3. title1.html (iframed by iframe.html)
139 // 4. chrome://crash
140 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); 154 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount));
155 // One tab should crash.
141 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); 156 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount));
142 // TODO(isherman): We should also verify that
143 // metrics::prefs::kStabilityExitedCleanly
144 // is set to true, but this preference isn't set until the browser
145 // exits... it's not clear to me how to test that.
146 157
147 #if defined(OS_WIN) 158 #if defined(OS_WIN)
148 histogram_tester.ExpectUniqueSample( 159 histogram_tester.ExpectUniqueSample(
149 "CrashExitCodes.Renderer", 160 "CrashExitCodes.Renderer",
150 std::abs(static_cast<int32_t>(STATUS_ACCESS_VIOLATION)), 1); 161 std::abs(static_cast<int32_t>(STATUS_ACCESS_VIOLATION)), 1);
151 #elif defined(OS_MACOSX) || defined(OS_LINUX) 162 #elif defined(OS_MACOSX) || defined(OS_LINUX)
152 VerifyRendererExitCodeIsSignal(histogram_tester, SIGSEGV); 163 VerifyRendererExitCodeIsSignal(histogram_tester, SIGSEGV);
153 #endif 164 #endif
154 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1); 165 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1);
155 } 166 }
156 167
168 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, MAYBE_CheckCrashRenderers) {
169 base::HistogramTester histogram_tester;
170
171 OpenTabsAndNavigateToCrashyUrl(content::kChromeUICheckCrashURL);
172
173 // Verify that the expected stability metrics were recorded.
174 const PrefService* prefs = g_browser_process->local_state();
175 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
176 // The three tabs from OpenTabs() and the one tab to open
177 // chrome://checkcrash/.
178 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount));
179 // One tab should crash.
180 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount));
181
182 #if defined(OS_WIN)
183 histogram_tester.ExpectUniqueSample(
184 "CrashExitCodes.Renderer",
185 std::abs(static_cast<int32_t>(STATUS_BREAKPOINT)), 1);
186 #elif defined(OS_MACOSX) || defined(OS_LINUX)
187 VerifyRendererExitCodeIsSignal(histogram_tester, SIGTRAP);
188 #endif
189 histogram_tester.ExpectUniqueSample("Tabs.SadTab.CrashCreated", 1, 1);
190 }
191
157 // OOM code only works on Windows. 192 // OOM code only works on Windows.
158 #if defined(OS_WIN) && !defined(ADDRESS_SANITIZER) 193 #if defined(OS_WIN) && !defined(ADDRESS_SANITIZER)
159 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, OOMRenderers) { 194 IN_PROC_BROWSER_TEST_F(MetricsServiceBrowserTest, OOMRenderers) {
160 base::HistogramTester histogram_tester; 195 base::HistogramTester histogram_tester;
161 OpenTabs();
162 196
163 // Kill the process for one of the tabs. 197 OpenTabsAndNavigateToCrashyUrl(content::kChromeUIMemoryExhaustURL);
164 content::RenderProcessHostWatcher observer(
165 browser()->tab_strip_model()->GetActiveWebContents(),
166 content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
167 ui_test_utils::NavigateToURL(browser(),
168 GURL(content::kChromeUIMemoryExhaustURL));
169 observer.Wait();
170
171 // The MetricsService listens for the same notification, so the |observer|
172 // might finish waiting before the MetricsService has a chance to process the
173 // notification. To avoid racing here, we repeatedly run the message loop
174 // until the MetricsService catches up. This should happen "real soon now",
175 // since the notification is posted to all observers essentially
176 // simultaneously... so busy waiting here shouldn't be too bad.
177 const PrefService* prefs = g_browser_process->local_state();
178 while (!prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)) {
179 content::RunAllPendingInMessageLoop();
180 }
181 198
182 // Verify that the expected stability metrics were recorded. 199 // Verify that the expected stability metrics were recorded.
200 const PrefService* prefs = g_browser_process->local_state();
183 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount)); 201 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityLaunchCount));
202 // The three tabs from OpenTabs() and the one tab to open
203 // chrome://memory-exhaust/.
184 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount)); 204 EXPECT_EQ(4, prefs->GetInteger(metrics::prefs::kStabilityPageLoadCount));
205 // One tab should crash.
Ilya Sherman 2017/02/21 23:18:00 Optional nit: This comment feels superfluous to th
Will Harris 2017/02/21 23:43:37 sure I removed it.
185 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount)); 206 EXPECT_EQ(1, prefs->GetInteger(metrics::prefs::kStabilityRendererCrashCount));
186 207
187 // On 64-bit, the Job object should terminate the renderer on an OOM. 208 // On 64-bit, the Job object should terminate the renderer on an OOM.
188 #if defined(ARCH_CPU_64_BITS) 209 #if defined(ARCH_CPU_64_BITS)
189 const int expected_exit_code = sandbox::SBOX_FATAL_MEMORY_EXCEEDED; 210 const int expected_exit_code = sandbox::SBOX_FATAL_MEMORY_EXCEEDED;
190 #else 211 #else
191 const int expected_exit_code = base::win::kOomExceptionCode; 212 const int expected_exit_code = base::win::kOomExceptionCode;
192 #endif 213 #endif
193 214
194 // Exit codes are recorded after being passed through std::abs see 215 // Exit codes are recorded after being passed through std::abs see
195 // MapCrashExitCodeForHistogram. 216 // MapCrashExitCodeForHistogram.
196 histogram_tester.ExpectUniqueSample("CrashExitCodes.Renderer", 217 histogram_tester.ExpectUniqueSample("CrashExitCodes.Renderer",
197 std::abs(expected_exit_code), 1); 218 std::abs(expected_exit_code), 1);
198 219
199 histogram_tester.ExpectUniqueSample("Tabs.SadTab.OomCreated", 1, 1); 220 histogram_tester.ExpectUniqueSample("Tabs.SadTab.OomCreated", 1, 1);
200 } 221 }
201 #endif // OS_WIN && !ADDRESS_SANITIZER 222 #endif // OS_WIN && !ADDRESS_SANITIZER
OLDNEW
« no previous file with comments | « no previous file | content/browser/frame_host/debug_urls.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698