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

Side by Side Diff: components/browser_watcher/watcher_metrics_provider_win_unittest.cc

Issue 2963573004: [Cleanup] Migrate the WatcherMetricsProvider to use the Task Scheduler. (Closed)
Patch Set: Document that registry interactions can block, and adjust the unittest not to be flaky Created 3 years, 5 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 | « components/browser_watcher/watcher_metrics_provider_win.cc ('k') | no next file » | 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) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 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/browser_watcher/watcher_metrics_provider_win.h" 5 #include "components/browser_watcher/watcher_metrics_provider_win.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <cstdlib> 10 #include <cstdlib>
11 11
12 #include "base/process/process_handle.h" 12 #include "base/process/process_handle.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/test/histogram_tester.h" 15 #include "base/test/histogram_tester.h"
16 #include "base/test/scoped_task_environment.h"
16 #include "base/test/test_reg_util_win.h" 17 #include "base/test/test_reg_util_win.h"
17 #include "base/test/test_simple_task_runner.h"
18 #include "base/win/registry.h" 18 #include "base/win/registry.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace browser_watcher { 21 namespace browser_watcher {
22 22
23 namespace { 23 namespace {
24 24
25 using GetExecutableDetailsCallback = 25 using GetExecutableDetailsCallback =
26 WatcherMetricsProviderWin::GetExecutableDetailsCallback; 26 WatcherMetricsProviderWin::GetExecutableDetailsCallback;
27 27
28 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest"; 28 const wchar_t kRegistryPath[] = L"Software\\WatcherMetricsProviderWinTest";
29 29
30 class WatcherMetricsProviderWinTest : public testing::Test { 30 class WatcherMetricsProviderWinTest : public testing::Test {
31 public: 31 public:
32 typedef testing::Test Super; 32 typedef testing::Test Super;
33 33
34 void SetUp() override { 34 void SetUp() override {
35 Super::SetUp(); 35 Super::SetUp();
36 36
37 ASSERT_NO_FATAL_FAILURE( 37 ASSERT_NO_FATAL_FAILURE(
38 override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); 38 override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
39 test_task_runner_ = new base::TestSimpleTaskRunner();
40 } 39 }
41 40
42 void AddProcessExitCode(bool use_own_pid, int exit_code) { 41 void AddProcessExitCode(bool use_own_pid, int exit_code) {
43 int pid = 0; 42 int pid = 0;
44 if (use_own_pid) { 43 if (use_own_pid) {
45 pid = base::GetCurrentProcId(); 44 pid = base::GetCurrentProcId();
46 } else { 45 } else {
47 // Make sure not to accidentally collide with own pid. 46 // Make sure not to accidentally collide with own pid.
48 do { 47 do {
49 pid = rand(); 48 pid = rand();
50 } while (pid == static_cast<int>(base::GetCurrentProcId())); 49 } while (pid == static_cast<int>(base::GetCurrentProcId()));
51 } 50 }
52 51
53 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_WRITE); 52 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_WRITE);
54 53
55 // Make up a unique key, starting with the given pid. 54 // Make up a unique key, starting with the given pid.
56 base::string16 key_name(base::StringPrintf(L"%d-%d", pid, rand())); 55 base::string16 key_name(base::StringPrintf(L"%d-%d", pid, rand()));
57 56
58 // Write the exit code to registry. 57 // Write the exit code to registry.
59 LONG result = key.WriteValue(key_name.c_str(), exit_code); 58 LONG result = key.WriteValue(key_name.c_str(), exit_code);
60 ASSERT_EQ(result, ERROR_SUCCESS); 59 ASSERT_EQ(result, ERROR_SUCCESS);
61 } 60 }
62 61
63 size_t ExitCodeRegistryPathValueCount() { 62 size_t ExitCodeRegistryPathValueCount() {
64 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ); 63 base::win::RegKey key(HKEY_CURRENT_USER, kRegistryPath, KEY_READ);
65 return key.GetValueCount(); 64 return key.GetValueCount();
66 } 65 }
67 66
68 protected: 67 protected:
68 base::test::ScopedTaskEnvironment scoped_task_environment_;
69 registry_util::RegistryOverrideManager override_manager_; 69 registry_util::RegistryOverrideManager override_manager_;
70 base::HistogramTester histogram_tester_; 70 base::HistogramTester histogram_tester_;
71 scoped_refptr<base::TestSimpleTaskRunner> test_task_runner_;
72 }; 71 };
73 72
74 } // namespace 73 } // namespace
75 74
76 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) { 75 TEST_F(WatcherMetricsProviderWinTest, RecordsStabilityHistogram) {
77 // Record multiple success exits. 76 // Record multiple success exits.
78 for (size_t i = 0; i < 11; ++i) 77 for (size_t i = 0; i < 11; ++i)
79 AddProcessExitCode(false, 0); 78 AddProcessExitCode(false, 0);
80 79
81 // Record a single failure. 80 // Record a single failure.
82 AddProcessExitCode(false, 100); 81 AddProcessExitCode(false, 100);
83 82
84 WatcherMetricsProviderWin provider( 83 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(),
85 kRegistryPath, base::FilePath(), base::FilePath(), 84 base::FilePath(),
86 GetExecutableDetailsCallback(), test_task_runner_.get()); 85 GetExecutableDetailsCallback());
87 86
88 provider.ProvideStabilityMetrics(NULL); 87 provider.ProvideStabilityMetrics(NULL);
89 histogram_tester_.ExpectBucketCount( 88 histogram_tester_.ExpectBucketCount(
90 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); 89 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11);
91 histogram_tester_.ExpectBucketCount( 90 histogram_tester_.ExpectBucketCount(
92 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1); 91 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 100, 1);
93 histogram_tester_.ExpectTotalCount( 92 histogram_tester_.ExpectTotalCount(
94 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12); 93 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 12);
95 94
96 // Verify that the reported values are gone. 95 // Verify that the reported values are gone.
97 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount()); 96 EXPECT_EQ(0u, ExitCodeRegistryPathValueCount());
98 } 97 }
99 98
100 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) { 99 TEST_F(WatcherMetricsProviderWinTest, DoesNotReportOwnProcessId) {
101 // Record multiple success exits. 100 // Record multiple success exits.
102 for (size_t i = 0; i < 11; ++i) 101 for (size_t i = 0; i < 11; ++i)
103 AddProcessExitCode(i, 0); 102 AddProcessExitCode(i, 0);
104 103
105 // Record own process as STILL_ACTIVE. 104 // Record own process as STILL_ACTIVE.
106 AddProcessExitCode(true, STILL_ACTIVE); 105 AddProcessExitCode(true, STILL_ACTIVE);
107 106
108 WatcherMetricsProviderWin provider( 107 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(),
109 kRegistryPath, base::FilePath(), base::FilePath(), 108 base::FilePath(),
110 GetExecutableDetailsCallback(), test_task_runner_.get()); 109 GetExecutableDetailsCallback());
111 110
112 provider.ProvideStabilityMetrics(NULL); 111 provider.ProvideStabilityMetrics(NULL);
113 histogram_tester_.ExpectUniqueSample( 112 histogram_tester_.ExpectUniqueSample(
114 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11); 113 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, 0, 11);
115 114
116 // Verify that the reported values are gone. 115 // Verify that the reported values are gone.
117 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount()); 116 EXPECT_EQ(1u, ExitCodeRegistryPathValueCount());
118 } 117 }
119 118
120 TEST_F(WatcherMetricsProviderWinTest, DeletesExitcodeKeyWhenNotReporting) { 119 TEST_F(WatcherMetricsProviderWinTest, DeletesExitcodeKeyWhenNotReporting) {
121 // Test that the registry at kRegistryPath is deleted when reporting is 120 // Test that the registry at kRegistryPath is deleted when reporting is
122 // disabled. 121 // disabled.
123 122
124 // Record multiple success exits. 123 // Record multiple success exits.
125 for (size_t i = 0; i < 11; ++i) 124 for (size_t i = 0; i < 11; ++i)
126 AddProcessExitCode(false, 0); 125 AddProcessExitCode(false, 0);
127 // Record a single failure. 126 // Record a single failure.
128 AddProcessExitCode(false, 100); 127 AddProcessExitCode(false, 100);
129 128
129 // Verify that the key exists prior to deletion.
130 base::win::RegKey key;
131 ASSERT_EQ(ERROR_SUCCESS,
132 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ));
133
130 // Make like the user is opted out of reporting. 134 // Make like the user is opted out of reporting.
131 WatcherMetricsProviderWin provider( 135 WatcherMetricsProviderWin provider(kRegistryPath, base::FilePath(),
132 kRegistryPath, base::FilePath(), base::FilePath(), 136 base::FilePath(),
133 GetExecutableDetailsCallback(), test_task_runner_.get()); 137 GetExecutableDetailsCallback());
134 provider.OnRecordingDisabled(); 138 provider.OnRecordingDisabled();
135 139
136 base::win::RegKey key;
137 {
138 // The deletion should be scheduled to the test_task_runner, and not happen
139 // immediately.
140 ASSERT_EQ(ERROR_SUCCESS,
141 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ));
142 }
143
144 // Flush the task(s). 140 // Flush the task(s).
145 test_task_runner_->RunPendingTasks(); 141 scoped_task_environment_.RunUntilIdle();
146 142
147 // Make sure the subkey for the pseudo process has been deleted on reporting. 143 // Make sure the subkey for the pseudo process has been deleted on reporting.
148 ASSERT_EQ(ERROR_FILE_NOT_FOUND, 144 ASSERT_EQ(ERROR_FILE_NOT_FOUND,
149 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ)); 145 key.Open(HKEY_CURRENT_USER, kRegistryPath, KEY_READ));
150 } 146 }
151 147
152 } // namespace browser_watcher 148 } // namespace browser_watcher
OLDNEW
« no previous file with comments | « components/browser_watcher/watcher_metrics_provider_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698