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

Side by Side Diff: sync/internal_api/attachments/on_disk_attachment_store_unittest.cc

Issue 690723004: Add per attachment metadata records. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 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 "sync/api/attachments/attachment_store.h" 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "sync/internal_api/attachments/attachment_store_test_template.h" 14 #include "sync/internal_api/attachments/attachment_store_test_template.h"
15 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" 15 #include "sync/internal_api/attachments/proto/attachment_store.pb.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 102
103 std::string ReadStoreMetadataRecord(const base::FilePath& path) { 103 std::string ReadStoreMetadataRecord(const base::FilePath& path) {
104 scoped_ptr<leveldb::DB> db = OpenLevelDB(path); 104 scoped_ptr<leveldb::DB> db = OpenLevelDB(path);
105 std::string content; 105 std::string content;
106 leveldb::Status s = 106 leveldb::Status s =
107 db->Get(leveldb::ReadOptions(), "database-metadata", &content); 107 db->Get(leveldb::ReadOptions(), "database-metadata", &content);
108 EXPECT_TRUE(s.ok()); 108 EXPECT_TRUE(s.ok());
109 return content; 109 return content;
110 } 110 }
111 111
112 void VerifyAttachmentRecordsPresent(const base::FilePath& path,
113 const AttachmentId& attachment_id,
114 bool expect_records_present) {
115 scoped_ptr<leveldb::DB> db = OpenLevelDB(path);
116
117 std::string metadata_key =
118 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id);
119 std::string data_key =
120 OnDiskAttachmentStore::MakeDataKeyFromAttachmentId(attachment_id);
121 std::string data;
122 leveldb::Status s = db->Get(leveldb::ReadOptions(), data_key, &data);
123 if (expect_records_present)
124 EXPECT_TRUE(s.ok());
125 else
126 EXPECT_TRUE(s.IsNotFound());
127 s = db->Get(leveldb::ReadOptions(), metadata_key, &data);
128 if (expect_records_present)
129 EXPECT_TRUE(s.ok());
130 else
131 EXPECT_TRUE(s.IsNotFound());
132 }
133
112 void RunLoop() { 134 void RunLoop() {
113 base::RunLoop run_loop; 135 base::RunLoop run_loop;
114 run_loop.RunUntilIdle(); 136 run_loop.RunUntilIdle();
115 } 137 }
116 }; 138 };
117 139
118 // Ensure that store can be closed and reopen while retaining stored 140 // Ensure that store can be closed and reopen while retaining stored
119 // attachments. 141 // attachments.
120 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { 142 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) {
121 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 143 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 result = AttachmentStore::SUCCESS; 259 result = AttachmentStore::SUCCESS;
238 AttachmentStore::CreateOnDiskStore( 260 AttachmentStore::CreateOnDiskStore(
239 temp_dir_.path(), 261 temp_dir_.path(),
240 base::ThreadTaskRunnerHandle::Get(), 262 base::ThreadTaskRunnerHandle::Get(),
241 base::Bind(&AttachmentStoreCreated, &store_, &result)); 263 base::Bind(&AttachmentStoreCreated, &store_, &result));
242 RunLoop(); 264 RunLoop();
243 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); 265 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR);
244 EXPECT_EQ(store_.get(), nullptr); 266 EXPECT_EQ(store_.get(), nullptr);
245 } 267 }
246 268
269 TEST_F(OnDiskAttachmentStoreSpecificTest, AttachmentRecordMetadata) {
270 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
271 base::FilePath db_path =
272 temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb"));
273
274 // Create attachment store.
275 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR;
276 AttachmentStore::CreateOnDiskStore(
277 temp_dir_.path(),
278 base::ThreadTaskRunnerHandle::Get(),
279 base::Bind(&AttachmentStoreCreated, &store_, &result));
280 RunLoop();
281 EXPECT_EQ(result, AttachmentStore::SUCCESS);
282
283 // Write two attachments.
284 std::string some_data;
285 AttachmentList attachments;
286 some_data = "data1";
287 attachments.push_back(
288 Attachment::Create(base::RefCountedString::TakeString(&some_data)));
289 some_data = "data2";
290 attachments.push_back(
291 Attachment::Create(base::RefCountedString::TakeString(&some_data)));
292 store_->Write(attachments,
293 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult,
294 base::Unretained(this),
295 &result));
296 RunLoop();
297 EXPECT_EQ(result, AttachmentStore::SUCCESS);
298
299 // Delete one of written attachments.
300 AttachmentIdList attachment_ids;
301 attachment_ids.push_back(attachments[0].GetId());
302 store_->Drop(attachment_ids,
303 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult,
304 base::Unretained(this),
305 &result));
306 RunLoop();
307 EXPECT_EQ(result, AttachmentStore::SUCCESS);
308 store_ = nullptr;
309 RunLoop();
310
311 // Verify that attachment store contains only records for second attachment.
312 VerifyAttachmentRecordsPresent(db_path, attachments[0].GetId(), false);
313 VerifyAttachmentRecordsPresent(db_path, attachments[1].GetId(), true);
314 }
315
247 } // namespace syncer 316 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698