| 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 <cstdio> | 5 #include <cstdio> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 int add_count_; | 135 int add_count_; |
| 136 }; | 136 }; |
| 137 | 137 |
| 138 class VisitedLinkTest : public testing::Test { | 138 class VisitedLinkTest : public testing::Test { |
| 139 protected: | 139 protected: |
| 140 // Initializes the visited link objects. Pass in the size that you want a | 140 // Initializes the visited link objects. Pass in the size that you want a |
| 141 // freshly created table to be. 0 means use the default. | 141 // freshly created table to be. 0 means use the default. |
| 142 // | 142 // |
| 143 // |suppress_rebuild| is set when we're not testing rebuilding, see | 143 // |suppress_rebuild| is set when we're not testing rebuilding, see |
| 144 // the VisitedLinkMaster constructor. | 144 // the VisitedLinkMaster constructor. |
| 145 bool InitVisited(int initial_size, bool suppress_rebuild) { | 145 // |
| 146 // |wait_for_io_complete| wait for result of async loading. |
| 147 bool InitVisited(int initial_size, |
| 148 bool suppress_rebuild, |
| 149 bool wait_for_io_complete) { |
| 146 // Initialize the visited link system. | 150 // Initialize the visited link system. |
| 147 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), | 151 master_.reset(new VisitedLinkMaster(new TrackingVisitedLinkEventListener(), |
| 148 &delegate_, | 152 &delegate_, |
| 149 true, | 153 true, |
| 150 suppress_rebuild, visited_file_, | 154 suppress_rebuild, visited_file_, |
| 151 initial_size)); | 155 initial_size)); |
| 152 return master_->Init(); | 156 bool result = master_->Init(); |
| 157 if (result && wait_for_io_complete) { |
| 158 // Wait for all pending file I/O to be completed. |
| 159 content::RunAllBlockingPoolTasksUntilIdle(); |
| 160 } |
| 161 return result; |
| 153 } | 162 } |
| 154 | 163 |
| 155 // May be called multiple times (some tests will do this to clear things, | 164 // May be called multiple times (some tests will do this to clear things, |
| 156 // and TearDown will do this to make sure eveything is shiny before quitting. | 165 // and TearDown will do this to make sure eveything is shiny before quitting. |
| 157 void ClearDB() { | 166 void ClearDB() { |
| 158 if (master_.get()) | 167 if (master_.get()) |
| 159 master_.reset(NULL); | 168 master_.reset(NULL); |
| 160 | 169 |
| 161 // Wait for all pending file I/O to be completed. | 170 // Wait for all pending file I/O to be completed. |
| 162 content::RunAllBlockingPoolTasksUntilIdle(); | 171 content::RunAllBlockingPoolTasksUntilIdle(); |
| 163 } | 172 } |
| 164 | 173 |
| 165 // Loads the database from disk and makes sure that the same URLs are present | 174 // Loads the database from disk and makes sure that the same URLs are present |
| 166 // as were generated by TestIO_Create(). This also checks the URLs with a | 175 // as were generated by TestIO_Create(). This also checks the URLs with a |
| 167 // slave to make sure it reads the data properly. | 176 // slave to make sure it reads the data properly. |
| 168 void Reload() { | 177 void Reload() { |
| 169 // Clean up after our caller, who may have left the database open. | 178 // Clean up after our caller, who may have left the database open. |
| 170 ClearDB(); | 179 ClearDB(); |
| 171 | 180 |
| 172 ASSERT_TRUE(InitVisited(0, true)); | 181 ASSERT_TRUE(InitVisited(0, true, true)); |
| 182 |
| 173 master_->DebugValidate(); | 183 master_->DebugValidate(); |
| 174 | 184 |
| 175 // check that the table has the proper number of entries | 185 // check that the table has the proper number of entries |
| 176 int used_count = master_->GetUsedCount(); | 186 int used_count = master_->GetUsedCount(); |
| 177 ASSERT_EQ(used_count, g_test_count); | 187 ASSERT_EQ(used_count, g_test_count); |
| 178 | 188 |
| 179 // Create a slave database. | 189 // Create a slave database. |
| 180 VisitedLinkSlave slave; | 190 VisitedLinkSlave slave; |
| 181 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); | 191 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
| 182 master_->shared_memory()->ShareToProcess( | 192 master_->shared_memory()->ShareToProcess( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 base::FilePath visited_file_; | 234 base::FilePath visited_file_; |
| 225 | 235 |
| 226 scoped_ptr<VisitedLinkMaster> master_; | 236 scoped_ptr<VisitedLinkMaster> master_; |
| 227 TestVisitedLinkDelegate delegate_; | 237 TestVisitedLinkDelegate delegate_; |
| 228 content::TestBrowserThreadBundle thread_bundle_; | 238 content::TestBrowserThreadBundle thread_bundle_; |
| 229 }; | 239 }; |
| 230 | 240 |
| 231 // This test creates and reads some databases to make sure the data is | 241 // This test creates and reads some databases to make sure the data is |
| 232 // preserved throughout those operations. | 242 // preserved throughout those operations. |
| 233 TEST_F(VisitedLinkTest, DatabaseIO) { | 243 TEST_F(VisitedLinkTest, DatabaseIO) { |
| 234 ASSERT_TRUE(InitVisited(0, true)); | 244 ASSERT_TRUE(InitVisited(0, true, true)); |
| 235 | 245 |
| 236 for (int i = 0; i < g_test_count; i++) | 246 for (int i = 0; i < g_test_count; i++) |
| 237 master_->AddURL(TestURL(i)); | 247 master_->AddURL(TestURL(i)); |
| 238 | 248 |
| 239 // Test that the database was written properly | 249 // Test that the database was written properly |
| 240 Reload(); | 250 Reload(); |
| 241 } | 251 } |
| 242 | 252 |
| 243 // Checks that we can delete things properly when there are collisions. | 253 // Checks that we can delete things properly when there are collisions. |
| 244 TEST_F(VisitedLinkTest, Delete) { | 254 TEST_F(VisitedLinkTest, Delete) { |
| 245 static const int32 kInitialSize = 17; | 255 static const int32 kInitialSize = 17; |
| 246 ASSERT_TRUE(InitVisited(kInitialSize, true)); | 256 ASSERT_TRUE(InitVisited(kInitialSize, true, true)); |
| 247 | 257 |
| 248 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the | 258 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the |
| 249 // same value. | 259 // same value. |
| 250 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14; | 260 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14; |
| 251 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14; | 261 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14; |
| 252 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14; | 262 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14; |
| 253 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14; | 263 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14; |
| 254 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14; | 264 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14; |
| 255 master_->AddFingerprint(kFingerprint0, false); // @14 | 265 master_->AddFingerprint(kFingerprint0, false); // @14 |
| 256 master_->AddFingerprint(kFingerprint1, false); // @15 | 266 master_->AddFingerprint(kFingerprint1, false); // @15 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 274 | 284 |
| 275 EXPECT_EQ(0, master_->used_items_); | 285 EXPECT_EQ(0, master_->used_items_); |
| 276 for (int i = 0; i < kInitialSize; i++) | 286 for (int i = 0; i < kInitialSize; i++) |
| 277 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) << | 287 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) << |
| 278 "Hash table has values in it."; | 288 "Hash table has values in it."; |
| 279 } | 289 } |
| 280 | 290 |
| 281 // When we delete more than kBigDeleteThreshold we trigger different behavior | 291 // When we delete more than kBigDeleteThreshold we trigger different behavior |
| 282 // where the entire file is rewritten. | 292 // where the entire file is rewritten. |
| 283 TEST_F(VisitedLinkTest, BigDelete) { | 293 TEST_F(VisitedLinkTest, BigDelete) { |
| 284 ASSERT_TRUE(InitVisited(16381, true)); | 294 ASSERT_TRUE(InitVisited(16381, true, true)); |
| 285 | 295 |
| 286 // Add the base set of URLs that won't be deleted. | 296 // Add the base set of URLs that won't be deleted. |
| 287 // Reload() will test for these. | 297 // Reload() will test for these. |
| 288 for (int32 i = 0; i < g_test_count; i++) | 298 for (int32 i = 0; i < g_test_count; i++) |
| 289 master_->AddURL(TestURL(i)); | 299 master_->AddURL(TestURL(i)); |
| 290 | 300 |
| 291 // Add more URLs than necessary to trigger this case. | 301 // Add more URLs than necessary to trigger this case. |
| 292 const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2; | 302 const int kTestDeleteCount = VisitedLinkMaster::kBigDeleteThreshold + 2; |
| 293 URLs urls_to_delete; | 303 URLs urls_to_delete; |
| 294 for (int32 i = g_test_count; i < g_test_count + kTestDeleteCount; i++) { | 304 for (int32 i = g_test_count; i < g_test_count + kTestDeleteCount; i++) { |
| 295 GURL url(TestURL(i)); | 305 GURL url(TestURL(i)); |
| 296 master_->AddURL(url); | 306 master_->AddURL(url); |
| 297 urls_to_delete.push_back(url); | 307 urls_to_delete.push_back(url); |
| 298 } | 308 } |
| 299 | 309 |
| 300 TestURLIterator iterator(urls_to_delete); | 310 TestURLIterator iterator(urls_to_delete); |
| 301 master_->DeleteURLs(&iterator); | 311 master_->DeleteURLs(&iterator); |
| 302 master_->DebugValidate(); | 312 master_->DebugValidate(); |
| 303 | 313 |
| 304 Reload(); | 314 Reload(); |
| 305 } | 315 } |
| 306 | 316 |
| 307 TEST_F(VisitedLinkTest, DeleteAll) { | 317 TEST_F(VisitedLinkTest, DeleteAll) { |
| 308 ASSERT_TRUE(InitVisited(0, true)); | 318 ASSERT_TRUE(InitVisited(0, true, true)); |
| 309 | 319 |
| 310 { | 320 { |
| 311 VisitedLinkSlave slave; | 321 VisitedLinkSlave slave; |
| 312 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); | 322 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
| 313 master_->shared_memory()->ShareToProcess( | 323 master_->shared_memory()->ShareToProcess( |
| 314 base::GetCurrentProcessHandle(), &new_handle); | 324 base::GetCurrentProcessHandle(), &new_handle); |
| 315 slave.OnUpdateVisitedLinks(new_handle); | 325 slave.OnUpdateVisitedLinks(new_handle); |
| 316 g_slaves.push_back(&slave); | 326 g_slaves.push_back(&slave); |
| 317 | 327 |
| 318 // Add the test URLs. | 328 // Add the test URLs. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 333 EXPECT_FALSE(master_->IsVisited(TestURL(i))); | 343 EXPECT_FALSE(master_->IsVisited(TestURL(i))); |
| 334 EXPECT_FALSE(slave.IsVisited(TestURL(i))); | 344 EXPECT_FALSE(slave.IsVisited(TestURL(i))); |
| 335 } | 345 } |
| 336 | 346 |
| 337 // Close the database. | 347 // Close the database. |
| 338 g_slaves.clear(); | 348 g_slaves.clear(); |
| 339 ClearDB(); | 349 ClearDB(); |
| 340 } | 350 } |
| 341 | 351 |
| 342 // Reopen and validate. | 352 // Reopen and validate. |
| 343 ASSERT_TRUE(InitVisited(0, true)); | 353 ASSERT_TRUE(InitVisited(0, true, true)); |
| 344 master_->DebugValidate(); | 354 master_->DebugValidate(); |
| 345 EXPECT_EQ(0, master_->GetUsedCount()); | 355 EXPECT_EQ(0, master_->GetUsedCount()); |
| 346 for (int i = 0; i < g_test_count; i++) | 356 for (int i = 0; i < g_test_count; i++) |
| 347 EXPECT_FALSE(master_->IsVisited(TestURL(i))); | 357 EXPECT_FALSE(master_->IsVisited(TestURL(i))); |
| 348 } | 358 } |
| 349 | 359 |
| 350 // This tests that the master correctly resizes its tables when it gets too | 360 // This tests that the master correctly resizes its tables when it gets too |
| 351 // full, notifies its slaves of the change, and updates the disk. | 361 // full, notifies its slaves of the change, and updates the disk. |
| 352 TEST_F(VisitedLinkTest, Resizing) { | 362 TEST_F(VisitedLinkTest, Resizing) { |
| 353 // Create a very small database. | 363 // Create a very small database. |
| 354 const int32 initial_size = 17; | 364 const int32 initial_size = 17; |
| 355 ASSERT_TRUE(InitVisited(initial_size, true)); | 365 ASSERT_TRUE(InitVisited(initial_size, true, true)); |
| 356 | 366 |
| 357 // ...and a slave | 367 // ...and a slave |
| 358 VisitedLinkSlave slave; | 368 VisitedLinkSlave slave; |
| 359 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); | 369 base::SharedMemoryHandle new_handle = base::SharedMemory::NULLHandle(); |
| 360 master_->shared_memory()->ShareToProcess( | 370 master_->shared_memory()->ShareToProcess( |
| 361 base::GetCurrentProcessHandle(), &new_handle); | 371 base::GetCurrentProcessHandle(), &new_handle); |
| 362 slave.OnUpdateVisitedLinks(new_handle); | 372 slave.OnUpdateVisitedLinks(new_handle); |
| 363 g_slaves.push_back(&slave); | 373 g_slaves.push_back(&slave); |
| 364 | 374 |
| 365 int32 used_count = master_->GetUsedCount(); | 375 int32 used_count = master_->GetUsedCount(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 // Tests that if the database doesn't exist, it will be rebuilt from history. | 411 // Tests that if the database doesn't exist, it will be rebuilt from history. |
| 402 TEST_F(VisitedLinkTest, Rebuild) { | 412 TEST_F(VisitedLinkTest, Rebuild) { |
| 403 // Add half of our URLs to history. This needs to be done before we | 413 // Add half of our URLs to history. This needs to be done before we |
| 404 // initialize the visited link DB. | 414 // initialize the visited link DB. |
| 405 int history_count = g_test_count / 2; | 415 int history_count = g_test_count / 2; |
| 406 for (int i = 0; i < history_count; i++) | 416 for (int i = 0; i < history_count; i++) |
| 407 delegate_.AddURLForRebuild(TestURL(i)); | 417 delegate_.AddURLForRebuild(TestURL(i)); |
| 408 | 418 |
| 409 // Initialize the visited link DB. Since the visited links file doesn't exist | 419 // Initialize the visited link DB. Since the visited links file doesn't exist |
| 410 // and we don't suppress history rebuilding, this will load from history. | 420 // and we don't suppress history rebuilding, this will load from history. |
| 411 ASSERT_TRUE(InitVisited(0, false)); | 421 ASSERT_TRUE(InitVisited(0, false, false)); |
| 412 | 422 |
| 413 // While the table is rebuilding, add the rest of the URLs to the visited | 423 // While the table is rebuilding, add the rest of the URLs to the visited |
| 414 // link system. This isn't guaranteed to happen during the rebuild, so we | 424 // link system. This isn't guaranteed to happen during the rebuild, so we |
| 415 // can't be 100% sure we're testing the right thing, but in practice is. | 425 // can't be 100% sure we're testing the right thing, but in practice is. |
| 416 // All the adds above will generally take some time queuing up on the | 426 // All the adds above will generally take some time queuing up on the |
| 417 // history thread, and it will take a while to catch up to actually | 427 // history thread, and it will take a while to catch up to actually |
| 418 // processing the rebuild that has queued behind it. We will generally | 428 // processing the rebuild that has queued behind it. We will generally |
| 419 // finish adding all of the URLs before it has even found the first URL. | 429 // finish adding all of the URLs before it has even found the first URL. |
| 420 for (int i = history_count; i < g_test_count; i++) | 430 for (int i = history_count; i < g_test_count; i++) |
| 421 master_->AddURL(TestURL(i)); | 431 master_->AddURL(TestURL(i)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 438 | 448 |
| 439 // Test that all URLs were written to the database properly. | 449 // Test that all URLs were written to the database properly. |
| 440 Reload(); | 450 Reload(); |
| 441 | 451 |
| 442 // Make sure the extra one was *not* written (Reload won't test this). | 452 // Make sure the extra one was *not* written (Reload won't test this). |
| 443 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); | 453 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); |
| 444 } | 454 } |
| 445 | 455 |
| 446 // Test that importing a large number of URLs will work | 456 // Test that importing a large number of URLs will work |
| 447 TEST_F(VisitedLinkTest, BigImport) { | 457 TEST_F(VisitedLinkTest, BigImport) { |
| 448 ASSERT_TRUE(InitVisited(0, false)); | 458 ASSERT_TRUE(InitVisited(0, false, false)); |
| 449 | 459 |
| 450 // Before the table rebuilds, add a large number of URLs | 460 // Before the table rebuilds, add a large number of URLs |
| 451 int total_count = VisitedLinkMaster::kDefaultTableSize + 10; | 461 int total_count = VisitedLinkMaster::kDefaultTableSize + 10; |
| 452 for (int i = 0; i < total_count; i++) | 462 for (int i = 0; i < total_count; i++) |
| 453 master_->AddURL(TestURL(i)); | 463 master_->AddURL(TestURL(i)); |
| 454 | 464 |
| 455 // Wait for the rebuild to complete. | 465 // Wait for the rebuild to complete. |
| 456 base::RunLoop run_loop; | 466 base::RunLoop run_loop; |
| 457 master_->set_rebuild_complete_task(run_loop.QuitClosure()); | 467 master_->set_rebuild_complete_task(run_loop.QuitClosure()); |
| 458 run_loop.Run(); | 468 run_loop.Run(); |
| 459 | 469 |
| 460 // Ensure that the right number of URLs are present | 470 // Ensure that the right number of URLs are present |
| 461 int used_count = master_->GetUsedCount(); | 471 int used_count = master_->GetUsedCount(); |
| 462 ASSERT_EQ(used_count, total_count); | 472 ASSERT_EQ(used_count, total_count); |
| 463 } | 473 } |
| 464 | 474 |
| 465 TEST_F(VisitedLinkTest, Listener) { | 475 TEST_F(VisitedLinkTest, Listener) { |
| 466 ASSERT_TRUE(InitVisited(0, true)); | 476 ASSERT_TRUE(InitVisited(0, true, true)); |
| 467 | 477 |
| 468 // Add test URLs. | 478 // Add test URLs. |
| 469 for (int i = 0; i < g_test_count; i++) { | 479 for (int i = 0; i < g_test_count; i++) { |
| 470 master_->AddURL(TestURL(i)); | 480 master_->AddURL(TestURL(i)); |
| 471 ASSERT_EQ(i + 1, master_->GetUsedCount()); | 481 ASSERT_EQ(i + 1, master_->GetUsedCount()); |
| 472 } | 482 } |
| 473 | 483 |
| 474 // Delete an URL. | 484 // Delete an URL. |
| 475 URLs urls_to_delete; | 485 URLs urls_to_delete; |
| 476 urls_to_delete.push_back(TestURL(0)); | 486 urls_to_delete.push_back(TestURL(0)); |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 content::NotificationService::current()->Notify( | 768 content::NotificationService::current()->Notify( |
| 759 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 769 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 760 content::Source<content::RenderProcessHost>(&different_process_host), | 770 content::Source<content::RenderProcessHost>(&different_process_host), |
| 761 content::NotificationService::NoDetails()); | 771 content::NotificationService::NoDetails()); |
| 762 WaitForCoalescense(); | 772 WaitForCoalescense(); |
| 763 | 773 |
| 764 EXPECT_EQ(0, different_context.new_table_count()); | 774 EXPECT_EQ(0, different_context.new_table_count()); |
| 765 } | 775 } |
| 766 | 776 |
| 767 } // namespace visitedlink | 777 } // namespace visitedlink |
| OLD | NEW |