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

Side by Side Diff: chrome/browser/visitedlink_unittest.cc

Issue 19028: Roll forward 8722,8721 (Closed)
Patch Set: Created 11 years, 11 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 | « chrome/browser/visitedlink_perftest.cc ('k') | chrome/chrome.xcodeproj/project.pbxproj » ('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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <vector> 5 #include <vector>
6 #include <string> 6 #include <string>
7 #include <cstdio> 7 #include <cstdio>
8 8
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/process_util.h"
12 #include "base/shared_memory.h" 13 #include "base/shared_memory.h"
13 #include "base/string_util.h" 14 #include "base/string_util.h"
14 #include "chrome/browser/visitedlink_master.h" 15 #include "chrome/browser/visitedlink_master.h"
15 #include "chrome/renderer/visitedlink_slave.h" 16 #include "chrome/renderer/visitedlink_slave.h"
16 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 namespace { 20 namespace {
20 21
21 // a nice long URL that we can append numbers to to get new URLs 22 // a nice long URL that we can append numbers to to get new URLs
22 const char g_test_prefix[] = 23 const char g_test_prefix[] =
23 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq="; 24 "http://www.google.com/products/foo/index.html?id=45028640526508376&seq=";
24 const int g_test_count = 1000; 25 const int g_test_count = 1000;
25 26
26 // Returns a test URL for index |i| 27 // Returns a test URL for index |i|
27 GURL TestURL(int i) { 28 GURL TestURL(int i) {
28 return GURL(StringPrintf("%s%d", g_test_prefix, i)); 29 return GURL(StringPrintf("%s%d", g_test_prefix, i));
29 } 30 }
30 31
31 // when testing in single-threaded mode
32 VisitedLinkMaster* g_master = NULL;
33 std::vector<VisitedLinkSlave*> g_slaves; 32 std::vector<VisitedLinkSlave*> g_slaves;
34 33
35 VisitedLinkMaster::PostNewTableEvent SynchronousBroadcastNewTableEvent; 34 VisitedLinkMaster::PostNewTableEvent SynchronousBroadcastNewTableEvent;
36 void SynchronousBroadcastNewTableEvent(base::SharedMemory* table) { 35 void SynchronousBroadcastNewTableEvent(base::SharedMemory* table) {
37 if (table) { 36 if (table) {
38 for (std::vector<VisitedLinkSlave>::size_type i = 0; 37 for (std::vector<VisitedLinkSlave>::size_type i = 0;
39 i < (int)g_slaves.size(); i++) { 38 i < g_slaves.size(); i++) {
40 base::SharedMemoryHandle new_handle = NULL; 39 base::SharedMemoryHandle new_handle = NULL;
41 table->ShareToProcess(GetCurrentProcess(), &new_handle); 40 table->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
42 g_slaves[i]->Init(new_handle); 41 g_slaves[i]->Init(new_handle);
43 } 42 }
44 } 43 }
45 } 44 }
46 45
46 } // namespace
47
47 class VisitedLinkTest : public testing::Test { 48 class VisitedLinkTest : public testing::Test {
48 protected: 49 protected:
49 // Initialize the history system. This should be called before InitVisited(). 50 // Initialize the history system. This should be called before InitVisited().
50 bool InitHistory() { 51 bool InitHistory() {
51 history_service_ = new HistoryService; 52 history_service_ = new HistoryService;
52 return history_service_->Init(history_dir_, NULL); 53 return history_service_->Init(history_dir_.ToWStringHack(), NULL);
53 } 54 }
54 55
55 // Initializes the visited link objects. Pass in the size that you want a 56 // Initializes the visited link objects. Pass in the size that you want a
56 // freshly created table to be. 0 means use the default. 57 // freshly created table to be. 0 means use the default.
57 // 58 //
58 // |suppress_rebuild| is set when we're not testing rebuilding, see 59 // |suppress_rebuild| is set when we're not testing rebuilding, see
59 // the VisitedLinkMaster constructor. 60 // the VisitedLinkMaster constructor.
60 bool InitVisited(int initial_size, bool suppress_rebuild) { 61 bool InitVisited(int initial_size, bool suppress_rebuild) {
61 // Initialize the visited link system. 62 // Initialize the visited link system.
62 master_.reset(new VisitedLinkMaster(NULL, 63 master_.reset(new VisitedLinkMaster(NULL,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 ASSERT_TRUE(InitVisited(0, true)); 97 ASSERT_TRUE(InitVisited(0, true));
97 master_->DebugValidate(); 98 master_->DebugValidate();
98 99
99 // check that the table has the proper number of entries 100 // check that the table has the proper number of entries
100 int used_count = master_->GetUsedCount(); 101 int used_count = master_->GetUsedCount();
101 ASSERT_EQ(used_count, g_test_count); 102 ASSERT_EQ(used_count, g_test_count);
102 103
103 // Create a slave database. 104 // Create a slave database.
104 VisitedLinkSlave slave; 105 VisitedLinkSlave slave;
105 base::SharedMemoryHandle new_handle = NULL; 106 base::SharedMemoryHandle new_handle = NULL;
106 master_->ShareToProcess(GetCurrentProcess(), &new_handle); 107 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
107 bool success = slave.Init(new_handle); 108 bool success = slave.Init(new_handle);
108 ASSERT_TRUE(success); 109 ASSERT_TRUE(success);
109 g_slaves.push_back(&slave); 110 g_slaves.push_back(&slave);
110 111
111 bool found; 112 bool found;
112 for (int i = 0; i < g_test_count; i++) { 113 for (int i = 0; i < g_test_count; i++) {
113 GURL cur = TestURL(i); 114 GURL cur = TestURL(i);
114 found = master_->IsVisited(cur); 115 found = master_->IsVisited(cur);
115 EXPECT_TRUE(found) << "URL " << i << "not found in master."; 116 EXPECT_TRUE(found) << "URL " << i << "not found in master.";
116 117
117 found = slave.IsVisited(cur); 118 found = slave.IsVisited(cur);
118 EXPECT_TRUE(found) << "URL " << i << "not found in slave."; 119 EXPECT_TRUE(found) << "URL " << i << "not found in slave.";
119 } 120 }
120 121
121 // test some random URL so we know that it returns false sometimes too 122 // test some random URL so we know that it returns false sometimes too
122 found = master_->IsVisited(GURL("http://unfound.site/")); 123 found = master_->IsVisited(GURL("http://unfound.site/"));
123 ASSERT_FALSE(found); 124 ASSERT_FALSE(found);
124 found = slave.IsVisited(GURL("http://unfound.site/")); 125 found = slave.IsVisited(GURL("http://unfound.site/"));
125 ASSERT_FALSE(found); 126 ASSERT_FALSE(found);
126 127
127 master_->DebugValidate(); 128 master_->DebugValidate();
128 129
129 g_slaves.clear(); 130 g_slaves.clear();
130 } 131 }
131 132
132 // testing::Test 133 // testing::Test
133 virtual void SetUp() { 134 virtual void SetUp() {
134 PathService::Get(base::DIR_TEMP, &history_dir_); 135 PathService::Get(base::DIR_TEMP, &history_dir_);
135 file_util::AppendToPath(&history_dir_, L"VisitedLinkTest"); 136 history_dir_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinkTest"));
136 file_util::Delete(history_dir_, true); 137 file_util::Delete(history_dir_, true);
137 file_util::CreateDirectory(history_dir_); 138 file_util::CreateDirectory(history_dir_);
138 139
139 visited_file_ = history_dir_; 140 visited_file_ = history_dir_.Append(FILE_PATH_LITERAL("VisitedLinks"));
140 file_util::AppendToPath(&visited_file_, L"VisitedLinks");
141 } 141 }
142 142
143 virtual void TearDown() { 143 virtual void TearDown() {
144 ClearDB(); 144 ClearDB();
145 file_util::Delete(history_dir_, true); 145 file_util::Delete(history_dir_, true);
146 } 146 }
147 147
148 MessageLoop message_loop_; 148 MessageLoop message_loop_;
149 149
150 // Filenames for the services; 150 // Filenames for the services;
151 std::wstring history_dir_; 151 FilePath history_dir_;
152 std::wstring visited_file_; 152 FilePath visited_file_;
153 153
154 scoped_ptr<VisitedLinkMaster> master_; 154 scoped_ptr<VisitedLinkMaster> master_;
155 scoped_refptr<HistoryService> history_service_; 155 scoped_refptr<HistoryService> history_service_;
156 }; 156 };
157 157
158 } // namespace
159
160 // This test creates and reads some databases to make sure the data is 158 // This test creates and reads some databases to make sure the data is
161 // preserved throughout those operations. 159 // preserved throughout those operations.
162 TEST_F(VisitedLinkTest, DatabaseIO) { 160 TEST_F(VisitedLinkTest, DatabaseIO) {
163 ASSERT_TRUE(InitHistory()); 161 ASSERT_TRUE(InitHistory());
164 ASSERT_TRUE(InitVisited(0, true)); 162 ASSERT_TRUE(InitVisited(0, true));
165 163
166 for (int i = 0; i < g_test_count; i++) 164 for (int i = 0; i < g_test_count; i++)
167 master_->AddURL(TestURL(i)); 165 master_->AddURL(TestURL(i));
168 166
169 // Test that the database was written properly 167 // Test that the database was written properly
170 Reload(); 168 Reload();
171 } 169 }
172 170
173 // Checks that we can delete things properly when there are collisions. 171 // Checks that we can delete things properly when there are collisions.
174 TEST_F(VisitedLinkTest, Delete) { 172 TEST_F(VisitedLinkTest, Delete) {
175 static const int32 kInitialSize = 17; 173 static const int32 kInitialSize = 17;
176 ASSERT_TRUE(InitHistory()); 174 ASSERT_TRUE(InitHistory());
177 ASSERT_TRUE(InitVisited(kInitialSize, true)); 175 ASSERT_TRUE(InitVisited(kInitialSize, true));
178 176
179 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the 177 // Add a cluster from 14-17 wrapping around to 0. These will all hash to the
180 // same value. 178 // same value.
181 const int kFingerprint0 = kInitialSize * 0 + 14; 179 const VisitedLinkCommon::Fingerprint kFingerprint0 = kInitialSize * 0 + 14;
182 const int kFingerprint1 = kInitialSize * 1 + 14; 180 const VisitedLinkCommon::Fingerprint kFingerprint1 = kInitialSize * 1 + 14;
183 const int kFingerprint2 = kInitialSize * 2 + 14; 181 const VisitedLinkCommon::Fingerprint kFingerprint2 = kInitialSize * 2 + 14;
184 const int kFingerprint3 = kInitialSize * 3 + 14; 182 const VisitedLinkCommon::Fingerprint kFingerprint3 = kInitialSize * 3 + 14;
185 const int kFingerprint4 = kInitialSize * 4 + 14; 183 const VisitedLinkCommon::Fingerprint kFingerprint4 = kInitialSize * 4 + 14;
186 master_->AddFingerprint(kFingerprint0); // @14 184 master_->AddFingerprint(kFingerprint0); // @14
187 master_->AddFingerprint(kFingerprint1); // @15 185 master_->AddFingerprint(kFingerprint1); // @15
188 master_->AddFingerprint(kFingerprint2); // @16 186 master_->AddFingerprint(kFingerprint2); // @16
189 master_->AddFingerprint(kFingerprint3); // @0 187 master_->AddFingerprint(kFingerprint3); // @0
190 master_->AddFingerprint(kFingerprint4); // @1 188 master_->AddFingerprint(kFingerprint4); // @1
191 189
192 // Deleting 14 should move the next value up one slot (we do not specify an 190 // Deleting 14 should move the next value up one slot (we do not specify an
193 // order). 191 // order).
194 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]); 192 EXPECT_EQ(kFingerprint3, master_->hash_table_[0]);
195 master_->DeleteFingerprint(kFingerprint3, false); 193 master_->DeleteFingerprint(kFingerprint3, false);
196 EXPECT_EQ(0, master_->hash_table_[1]); 194 VisitedLinkCommon::Fingerprint zero_fingerprint = 0;
197 EXPECT_NE(0, master_->hash_table_[0]); 195 EXPECT_EQ(zero_fingerprint, master_->hash_table_[1]);
196 EXPECT_NE(zero_fingerprint, master_->hash_table_[0]);
198 197
199 // Deleting the other four should leave the table empty. 198 // Deleting the other four should leave the table empty.
200 master_->DeleteFingerprint(kFingerprint0, false); 199 master_->DeleteFingerprint(kFingerprint0, false);
201 master_->DeleteFingerprint(kFingerprint1, false); 200 master_->DeleteFingerprint(kFingerprint1, false);
202 master_->DeleteFingerprint(kFingerprint2, false); 201 master_->DeleteFingerprint(kFingerprint2, false);
203 master_->DeleteFingerprint(kFingerprint4, false); 202 master_->DeleteFingerprint(kFingerprint4, false);
204 203
205 EXPECT_EQ(0, master_->used_items_); 204 EXPECT_EQ(0, master_->used_items_);
206 for (int i = 0; i < kInitialSize; i++) 205 for (int i = 0; i < kInitialSize; i++)
207 EXPECT_EQ(0, master_->hash_table_[i]) << "Hash table has values in it."; 206 EXPECT_EQ(zero_fingerprint, master_->hash_table_[i]) <<
207 "Hash table has values in it.";
208 } 208 }
209 209
210 // When we delete more than kBigDeleteThreshold we trigger different behavior 210 // When we delete more than kBigDeleteThreshold we trigger different behavior
211 // where the entire file is rewritten. 211 // where the entire file is rewritten.
212 TEST_F(VisitedLinkTest, BigDelete) { 212 TEST_F(VisitedLinkTest, BigDelete) {
213 ASSERT_TRUE(InitHistory()); 213 ASSERT_TRUE(InitHistory());
214 ASSERT_TRUE(InitVisited(16381, true)); 214 ASSERT_TRUE(InitVisited(16381, true));
215 215
216 // Add the base set of URLs that won't be deleted. 216 // Add the base set of URLs that won't be deleted.
217 // Reload() will test for these. 217 // Reload() will test for these.
(...skipping 15 matching lines...) Expand all
233 Reload(); 233 Reload();
234 } 234 }
235 235
236 TEST_F(VisitedLinkTest, DeleteAll) { 236 TEST_F(VisitedLinkTest, DeleteAll) {
237 ASSERT_TRUE(InitHistory()); 237 ASSERT_TRUE(InitHistory());
238 ASSERT_TRUE(InitVisited(0, true)); 238 ASSERT_TRUE(InitVisited(0, true));
239 239
240 { 240 {
241 VisitedLinkSlave slave; 241 VisitedLinkSlave slave;
242 base::SharedMemoryHandle new_handle = NULL; 242 base::SharedMemoryHandle new_handle = NULL;
243 master_->ShareToProcess(GetCurrentProcess(), &new_handle); 243 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
244 ASSERT_TRUE(slave.Init(new_handle)); 244 ASSERT_TRUE(slave.Init(new_handle));
245 g_slaves.push_back(&slave); 245 g_slaves.push_back(&slave);
246 246
247 // Add the test URLs. 247 // Add the test URLs.
248 for (int i = 0; i < g_test_count; i++) { 248 for (int i = 0; i < g_test_count; i++) {
249 master_->AddURL(TestURL(i)); 249 master_->AddURL(TestURL(i));
250 ASSERT_EQ(i + 1, master_->GetUsedCount()); 250 ASSERT_EQ(i + 1, master_->GetUsedCount());
251 } 251 }
252 master_->DebugValidate(); 252 master_->DebugValidate();
253 253
(...skipping 27 matching lines...) Expand all
281 // full, notifies its slaves of the change, and updates the disk. 281 // full, notifies its slaves of the change, and updates the disk.
282 TEST_F(VisitedLinkTest, Resizing) { 282 TEST_F(VisitedLinkTest, Resizing) {
283 // Create a very small database. 283 // Create a very small database.
284 const int32 initial_size = 17; 284 const int32 initial_size = 17;
285 ASSERT_TRUE(InitHistory()); 285 ASSERT_TRUE(InitHistory());
286 ASSERT_TRUE(InitVisited(initial_size, true)); 286 ASSERT_TRUE(InitVisited(initial_size, true));
287 287
288 // ...and a slave 288 // ...and a slave
289 VisitedLinkSlave slave; 289 VisitedLinkSlave slave;
290 base::SharedMemoryHandle new_handle = NULL; 290 base::SharedMemoryHandle new_handle = NULL;
291 master_->ShareToProcess(GetCurrentProcess(), &new_handle); 291 master_->ShareToProcess(base::GetCurrentProcessHandle(), &new_handle);
292 bool success = slave.Init(new_handle); 292 bool success = slave.Init(new_handle);
293 ASSERT_TRUE(success); 293 ASSERT_TRUE(success);
294 g_slaves.push_back(&slave); 294 g_slaves.push_back(&slave);
295 295
296 int32 used_count = master_->GetUsedCount(); 296 int32 used_count = master_->GetUsedCount();
297 ASSERT_EQ(used_count, 0); 297 ASSERT_EQ(used_count, 0);
298 298
299 for (int i = 0; i < g_test_count; i++) { 299 for (int i = 0; i < g_test_count; i++) {
300 master_->AddURL(TestURL(i)); 300 master_->AddURL(TestURL(i));
301 used_count = master_->GetUsedCount(); 301 used_count = master_->GetUsedCount();
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 master_->set_rebuild_complete_task(new MessageLoop::QuitTask); 367 master_->set_rebuild_complete_task(new MessageLoop::QuitTask);
368 MessageLoop::current()->Run(); 368 MessageLoop::current()->Run();
369 369
370 // Test that all URLs were written to the database properly. 370 // Test that all URLs were written to the database properly.
371 Reload(); 371 Reload();
372 372
373 // Make sure the extra one was *not* written (Reload won't test this). 373 // Make sure the extra one was *not* written (Reload won't test this).
374 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count))); 374 EXPECT_FALSE(master_->IsVisited(TestURL(g_test_count)));
375 } 375 }
376 376
OLDNEW
« no previous file with comments | « chrome/browser/visitedlink_perftest.cc ('k') | chrome/chrome.xcodeproj/project.pbxproj » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698