OLD | NEW |
---|---|
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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/memory_mapped_file.h" | 7 #include "base/files/memory_mapped_file.h" |
8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 return t->cb_.Run(); | 129 return t->cb_.Run(); |
130 } | 130 } |
131 | 131 |
132 sqlite3* db_; | 132 sqlite3* db_; |
133 base::Callback<int(void)> cb_; | 133 base::Callback<int(void)> cb_; |
134 | 134 |
135 DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); | 135 DISALLOW_COPY_AND_ASSIGN(ScopedCommitHook); |
136 }; | 136 }; |
137 | 137 |
138 } // namespace test | 138 } // namespace test |
139 } // namespace sql | |
Scott Hess - ex-Googler
2015/10/19 22:23:41
Slight namespace rearrangement to allow test-frien
| |
140 | 139 |
141 namespace { | 140 namespace { |
142 | 141 |
143 // Helper to return the count of items in sqlite_master. Return -1 in | 142 // Helper to return the count of items in sqlite_master. Return -1 in |
144 // case of error. | 143 // case of error. |
145 int SqliteMasterCount(sql::Connection* db) { | 144 int SqliteMasterCount(sql::Connection* db) { |
146 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; | 145 const char* kMasterCount = "SELECT COUNT(*) FROM sqlite_master"; |
147 sql::Statement s(db->GetUniqueStatement(kMasterCount)); | 146 sql::Statement s(db->GetUniqueStatement(kMasterCount)); |
148 return s.Step() ? s.ColumnInt(0) : -1; | 147 return s.Step() ? s.ColumnInt(0) : -1; |
149 } | 148 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 explicit ScopedUmaskSetter(mode_t target_mask) { | 202 explicit ScopedUmaskSetter(mode_t target_mask) { |
204 old_umask_ = umask(target_mask); | 203 old_umask_ = umask(target_mask); |
205 } | 204 } |
206 ~ScopedUmaskSetter() { umask(old_umask_); } | 205 ~ScopedUmaskSetter() { umask(old_umask_); } |
207 private: | 206 private: |
208 mode_t old_umask_; | 207 mode_t old_umask_; |
209 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); | 208 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter); |
210 }; | 209 }; |
211 #endif | 210 #endif |
212 | 211 |
212 // SQLite function to adjust mock time by |argv[0]| milliseconds. | |
Scott Hess - ex-Googler
2015/10/19 22:23:41
Moved here with no changes to keep it in an anonym
| |
213 void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, | |
214 sqlite3_context* context, | |
215 int argc, sqlite3_value** argv) { | |
216 int64 milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; | |
217 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
218 sqlite3_result_int64(context, milliseconds); | |
219 } | |
220 | |
221 // Adjust mock time by |milliseconds| on commit. | |
222 int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, | |
223 int64 milliseconds) { | |
224 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
225 return SQLITE_OK; | |
226 } | |
227 | |
228 const char kCommitTime[] = "Sqlite.CommitTime.Test"; | |
229 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; | |
230 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; | |
231 const char kQueryTime[] = "Sqlite.QueryTime.Test"; | |
232 | |
233 } // namespace | |
234 | |
213 class SQLConnectionTest : public sql::SQLTestBase { | 235 class SQLConnectionTest : public sql::SQLTestBase { |
214 public: | 236 public: |
215 void SetUp() override { | 237 void SetUp() override { |
216 // Any macro histograms which fire before the recorder is initialized cannot | 238 // Any macro histograms which fire before the recorder is initialized cannot |
217 // be tested. So this needs to be ahead of Open(). | 239 // be tested. So this needs to be ahead of Open(). |
218 base::StatisticsRecorder::Initialize(); | 240 base::StatisticsRecorder::Initialize(); |
219 | 241 |
220 SQLTestBase::SetUp(); | 242 SQLTestBase::SetUp(); |
221 } | 243 } |
222 | 244 |
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1140 sql::Connection::EVENT_STATEMENT_ROWS, 2); | 1162 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
1141 tester.ExpectBucketCount(kGlobalHistogramName, | 1163 tester.ExpectBucketCount(kGlobalHistogramName, |
1142 sql::Connection::EVENT_STATEMENT_ROWS, 2); | 1164 sql::Connection::EVENT_STATEMENT_ROWS, 2); |
1143 tester.ExpectBucketCount(kHistogramName, | 1165 tester.ExpectBucketCount(kHistogramName, |
1144 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); | 1166 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
1145 tester.ExpectBucketCount(kGlobalHistogramName, | 1167 tester.ExpectBucketCount(kGlobalHistogramName, |
1146 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); | 1168 sql::Connection::EVENT_STATEMENT_SUCCESS, 1); |
1147 } | 1169 } |
1148 } | 1170 } |
1149 | 1171 |
1150 // SQLite function to adjust mock time by |argv[0]| milliseconds. | |
1151 void sqlite_adjust_millis(sql::test::ScopedMockTimeSource* time_mock, | |
1152 sqlite3_context* context, | |
1153 int argc, sqlite3_value** argv) { | |
1154 int64 milliseconds = argc > 0 ? sqlite3_value_int64(argv[0]) : 1000; | |
1155 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
1156 sqlite3_result_int64(context, milliseconds); | |
1157 } | |
1158 | |
1159 // Adjust mock time by |milliseconds| on commit. | |
1160 int adjust_commit_hook(sql::test::ScopedMockTimeSource* time_mock, | |
1161 int64 milliseconds) { | |
1162 time_mock->adjust(base::TimeDelta::FromMilliseconds(milliseconds)); | |
1163 return SQLITE_OK; | |
1164 } | |
1165 | |
1166 const char kCommitTime[] = "Sqlite.CommitTime.Test"; | |
1167 const char kAutoCommitTime[] = "Sqlite.AutoCommitTime.Test"; | |
1168 const char kUpdateTime[] = "Sqlite.UpdateTime.Test"; | |
1169 const char kQueryTime[] = "Sqlite.QueryTime.Test"; | |
1170 | |
1171 // Read-only query allocates time to QueryTime, but not others. | 1172 // Read-only query allocates time to QueryTime, but not others. |
1172 TEST_F(SQLConnectionTest, TimeQuery) { | 1173 TEST_F(SQLConnectionTest, TimeQuery) { |
1173 // Re-open with histogram tag. Use an in-memory database to minimize variance | 1174 // Re-open with histogram tag. Use an in-memory database to minimize variance |
1174 // due to filesystem. | 1175 // due to filesystem. |
1175 db().Close(); | 1176 db().Close(); |
1176 db().set_histogram_tag("Test"); | 1177 db().set_histogram_tag("Test"); |
1177 ASSERT_TRUE(db().OpenInMemory()); | 1178 ASSERT_TRUE(db().OpenInMemory()); |
1178 | 1179 |
1179 sql::test::ScopedMockTimeSource time_mock(db()); | 1180 sql::test::ScopedMockTimeSource time_mock(db()); |
1180 | 1181 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1380 #endif | 1381 #endif |
1381 | 1382 |
1382 TEST_F(SQLConnectionTest, OnMemoryDump) { | 1383 TEST_F(SQLConnectionTest, OnMemoryDump) { |
1383 base::trace_event::ProcessMemoryDump pmd(nullptr); | 1384 base::trace_event::ProcessMemoryDump pmd(nullptr); |
1384 base::trace_event::MemoryDumpArgs args = { | 1385 base::trace_event::MemoryDumpArgs args = { |
1385 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; | 1386 base::trace_event::MemoryDumpLevelOfDetail::DETAILED}; |
1386 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); | 1387 ASSERT_TRUE(db().OnMemoryDump(args, &pmd)); |
1387 EXPECT_GE(pmd.allocator_dumps().size(), 1u); | 1388 EXPECT_GE(pmd.allocator_dumps().size(), 1u); |
1388 } | 1389 } |
1389 | 1390 |
1390 } // namespace | 1391 TEST_F(SQLConnectionTest, RegisterIntentToUpload) { |
1392 base::FilePath breadcrumb_path( | |
1393 db_path().DirName().Append(FILE_PATH_LITERAL("sqlite-diag"))); | |
1394 | |
1395 // No stale diagnostic store. | |
1396 ASSERT_TRUE(!base::PathExists(breadcrumb_path)); | |
1397 | |
1398 // The histogram tag is required to enable diagnostic features. | |
1399 EXPECT_FALSE(db().RegisterIntentToUpload()); | |
1400 EXPECT_TRUE(!base::PathExists(breadcrumb_path)); | |
1401 | |
1402 db().Close(); | |
1403 db().set_histogram_tag("Test"); | |
1404 ASSERT_TRUE(db().Open(db_path())); | |
1405 | |
1406 // Should signal upload only once. | |
1407 EXPECT_TRUE(db().RegisterIntentToUpload()); | |
1408 EXPECT_TRUE(base::PathExists(breadcrumb_path)); | |
1409 EXPECT_FALSE(db().RegisterIntentToUpload()); | |
1410 | |
1411 // Changing the histogram tag should allow new upload to succeed. | |
1412 db().Close(); | |
1413 db().set_histogram_tag("NewTest"); | |
1414 ASSERT_TRUE(db().Open(db_path())); | |
1415 EXPECT_TRUE(db().RegisterIntentToUpload()); | |
1416 EXPECT_FALSE(db().RegisterIntentToUpload()); | |
1417 | |
1418 // Old tag is still prevented. | |
1419 db().Close(); | |
1420 db().set_histogram_tag("Test"); | |
1421 ASSERT_TRUE(db().Open(db_path())); | |
1422 EXPECT_FALSE(db().RegisterIntentToUpload()); | |
1423 } | |
1424 | |
1425 } // namespace sql | |
OLD | NEW |