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

Side by Side Diff: chrome/browser/chromeos/drive/file_system/update_operation_unittest.cc

Issue 139783004: drive: Do not clear dirty bit if someone is writing to the file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/drive/file_system/update_operation.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/file_system/update_operation.h" 5 #include "chrome/browser/chromeos/drive/file_system/update_operation.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/file_util.h"
9 #include "base/md5.h"
8 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
9 #include "chrome/browser/chromeos/drive/file_cache.h" 11 #include "chrome/browser/chromeos/drive/file_cache.h"
10 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" 12 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
11 #include "chrome/browser/chromeos/drive/file_system_interface.h" 13 #include "chrome/browser/chromeos/drive/file_system_interface.h"
12 #include "chrome/browser/drive/fake_drive_service.h" 14 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "google_apis/drive/gdata_wapi_parser.h" 15 #include "google_apis/drive/gdata_wapi_parser.h"
14 #include "google_apis/drive/test_util.h" 16 #include "google_apis/drive/test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 18
17 namespace drive { 19 namespace drive {
18 namespace file_system { 20 namespace file_system {
19 21
20 class UpdateOperationTest : public OperationTestBase { 22 class UpdateOperationTest : public OperationTestBase {
21 protected: 23 protected:
22 virtual void SetUp() OVERRIDE { 24 virtual void SetUp() OVERRIDE {
23 OperationTestBase::SetUp(); 25 OperationTestBase::SetUp();
24 operation_.reset(new UpdateOperation(blocking_task_runner(), 26 operation_.reset(new UpdateOperation(blocking_task_runner(),
25 observer(), 27 observer(),
26 scheduler(), 28 scheduler(),
27 metadata(), 29 metadata(),
28 cache())); 30 cache()));
29 } 31 }
30 32
31 scoped_ptr<UpdateOperation> operation_; 33 // Stores |content| to the cache and mark it as dirty.
34 FileError StoreAndMarkDirty(const std::string& local_id,
35 const std::string& content) {
36 base::FilePath path;
37 if (!base::CreateTemporaryFileInDir(temp_dir(), &path) ||
38 !google_apis::test_util::WriteStringToFile(path, content))
39 return FILE_ERROR_FAILED;
40
41 // Store the file to cache.
42 FileError error = FILE_ERROR_FAILED;
43 base::PostTaskAndReplyWithResult(
44 blocking_task_runner(),
45 FROM_HERE,
46 base::Bind(&internal::FileCache::Store,
47 base::Unretained(cache()),
48 local_id, base::MD5String(content), path,
49 internal::FileCache::FILE_OPERATION_COPY),
50 google_apis::test_util::CreateCopyResultCallback(&error));
51 test_util::RunBlockingPoolTask();
52 if (error != FILE_ERROR_OK)
53 return error;
54
55 // Add the dirty bit.
56 error = FILE_ERROR_FAILED;
57 scoped_ptr<base::ScopedClosureRunner> file_closer;
58 base::PostTaskAndReplyWithResult(
59 blocking_task_runner(),
60 FROM_HERE,
61 base::Bind(&internal::FileCache::OpenForWrite,
62 base::Unretained(cache()),
63 local_id,
64 &file_closer),
65 google_apis::test_util::CreateCopyResultCallback(&error));
66 test_util::RunBlockingPoolTask();
67 return error;
68 }
69
70 scoped_ptr<UpdateOperation> operation_;
32 }; 71 };
33 72
34 TEST_F(UpdateOperationTest, UpdateFileByLocalId_PersistentFile) { 73 TEST_F(UpdateOperationTest, UpdateFileByLocalId) {
35 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt")); 74 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt"));
36 const std::string kResourceId("file:2_file_resource_id"); 75 const std::string kResourceId("file:2_file_resource_id");
37 const std::string kMd5("3b4382ebefec6e743578c76bbd0575ce");
38
39 const base::FilePath kTestFile = temp_dir().Append(FILE_PATH_LITERAL("foo"));
40 const std::string kTestFileContent = "I'm being uploaded! Yay!";
41 google_apis::test_util::WriteStringToFile(kTestFile, kTestFileContent);
42 76
43 const std::string local_id = GetLocalId(kFilePath); 77 const std::string local_id = GetLocalId(kFilePath);
44 EXPECT_FALSE(local_id.empty()); 78 EXPECT_FALSE(local_id.empty());
45 79
46 // Pin the file so it'll be store in "persistent" directory. 80 const std::string kTestFileContent = "I'm being uploaded! Yay!";
hashimoto 2014/01/16 05:47:31 Removing this "persistent" part as it seems obsole
47 FileError error = FILE_ERROR_FAILED; 81 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent));
48 base::PostTaskAndReplyWithResult(
49 blocking_task_runner(),
50 FROM_HERE,
51 base::Bind(&internal::FileCache::Pin,
52 base::Unretained(cache()),
53 local_id),
54 google_apis::test_util::CreateCopyResultCallback(&error));
55 test_util::RunBlockingPoolTask();
56 EXPECT_EQ(FILE_ERROR_OK, error);
57
58 // First store a file to cache.
59 error = FILE_ERROR_FAILED;
60 base::PostTaskAndReplyWithResult(
61 blocking_task_runner(),
62 FROM_HERE,
63 base::Bind(&internal::FileCache::Store,
64 base::Unretained(cache()),
65 local_id, kMd5, kTestFile,
66 internal::FileCache::FILE_OPERATION_COPY),
67 google_apis::test_util::CreateCopyResultCallback(&error));
68 test_util::RunBlockingPoolTask();
69 EXPECT_EQ(FILE_ERROR_OK, error);
70
71 // Add the dirty bit.
72 error = FILE_ERROR_FAILED;
73 scoped_ptr<base::ScopedClosureRunner> file_closer;
74 base::PostTaskAndReplyWithResult(
75 blocking_task_runner(),
76 FROM_HERE,
77 base::Bind(&internal::FileCache::OpenForWrite,
78 base::Unretained(cache()),
79 local_id,
80 &file_closer),
81 google_apis::test_util::CreateCopyResultCallback(&error));
82 test_util::RunBlockingPoolTask();
83 EXPECT_EQ(FILE_ERROR_OK, error);
84 82
85 int64 original_changestamp = fake_service()->largest_changestamp(); 83 int64 original_changestamp = fake_service()->largest_changestamp();
86 84
87 // The callback will be called upon completion of UpdateFileByLocalId(). 85 // The callback will be called upon completion of UpdateFileByLocalId().
88 error = FILE_ERROR_FAILED; 86 FileError error = FILE_ERROR_FAILED;
89 operation_->UpdateFileByLocalId( 87 operation_->UpdateFileByLocalId(
90 local_id, 88 local_id,
91 ClientContext(USER_INITIATED), 89 ClientContext(USER_INITIATED),
92 UpdateOperation::RUN_CONTENT_CHECK, 90 UpdateOperation::RUN_CONTENT_CHECK,
93 google_apis::test_util::CreateCopyResultCallback(&error)); 91 google_apis::test_util::CreateCopyResultCallback(&error));
94 test_util::RunBlockingPoolTask(); 92 test_util::RunBlockingPoolTask();
95 EXPECT_EQ(FILE_ERROR_OK, error); 93 EXPECT_EQ(FILE_ERROR_OK, error);
96 94
97 // Check that the server has received an update. 95 // Check that the server has received an update.
98 EXPECT_LT(original_changestamp, fake_service()->largest_changestamp()); 96 EXPECT_LT(original_changestamp, fake_service()->largest_changestamp());
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 ClientContext(USER_INITIATED), 130 ClientContext(USER_INITIATED),
133 UpdateOperation::RUN_CONTENT_CHECK, 131 UpdateOperation::RUN_CONTENT_CHECK,
134 google_apis::test_util::CreateCopyResultCallback(&error)); 132 google_apis::test_util::CreateCopyResultCallback(&error));
135 test_util::RunBlockingPoolTask(); 133 test_util::RunBlockingPoolTask();
136 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); 134 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
137 } 135 }
138 136
139 TEST_F(UpdateOperationTest, UpdateFileByLocalId_Md5) { 137 TEST_F(UpdateOperationTest, UpdateFileByLocalId_Md5) {
140 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt")); 138 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt"));
141 const std::string kResourceId("file:2_file_resource_id"); 139 const std::string kResourceId("file:2_file_resource_id");
142 const std::string kMd5("3b4382ebefec6e743578c76bbd0575ce");
143
144 const base::FilePath kTestFile = temp_dir().Append(FILE_PATH_LITERAL("foo"));
145 const std::string kTestFileContent = "I'm being uploaded! Yay!";
146 google_apis::test_util::WriteStringToFile(kTestFile, kTestFileContent);
147 140
148 const std::string local_id = GetLocalId(kFilePath); 141 const std::string local_id = GetLocalId(kFilePath);
149 EXPECT_FALSE(local_id.empty()); 142 EXPECT_FALSE(local_id.empty());
150 143
151 // First store a file to cache. 144 const std::string kTestFileContent = "I'm being uploaded! Yay!";
152 FileError error = FILE_ERROR_FAILED; 145 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent));
153 base::PostTaskAndReplyWithResult(
154 blocking_task_runner(),
155 FROM_HERE,
156 base::Bind(&internal::FileCache::Store,
157 base::Unretained(cache()),
158 local_id, kMd5, kTestFile,
159 internal::FileCache::FILE_OPERATION_COPY),
160 google_apis::test_util::CreateCopyResultCallback(&error));
161 test_util::RunBlockingPoolTask();
162 EXPECT_EQ(FILE_ERROR_OK, error);
163
164 // Add the dirty bit.
165 error = FILE_ERROR_FAILED;
166 scoped_ptr<base::ScopedClosureRunner> file_closer;
167 base::PostTaskAndReplyWithResult(
168 blocking_task_runner(),
169 FROM_HERE,
170 base::Bind(&internal::FileCache::OpenForWrite,
171 base::Unretained(cache()),
172 local_id,
173 &file_closer),
174 google_apis::test_util::CreateCopyResultCallback(&error));
175 test_util::RunBlockingPoolTask();
176 EXPECT_EQ(FILE_ERROR_OK, error);
177 146
178 int64 original_changestamp = fake_service()->largest_changestamp(); 147 int64 original_changestamp = fake_service()->largest_changestamp();
179 148
180 // The callback will be called upon completion of UpdateFileByLocalId(). 149 // The callback will be called upon completion of UpdateFileByLocalId().
181 error = FILE_ERROR_FAILED; 150 FileError error = FILE_ERROR_FAILED;
182 operation_->UpdateFileByLocalId( 151 operation_->UpdateFileByLocalId(
183 local_id, 152 local_id,
184 ClientContext(USER_INITIATED), 153 ClientContext(USER_INITIATED),
185 UpdateOperation::RUN_CONTENT_CHECK, 154 UpdateOperation::RUN_CONTENT_CHECK,
186 google_apis::test_util::CreateCopyResultCallback(&error)); 155 google_apis::test_util::CreateCopyResultCallback(&error));
187 test_util::RunBlockingPoolTask(); 156 test_util::RunBlockingPoolTask();
188 EXPECT_EQ(FILE_ERROR_OK, error); 157 EXPECT_EQ(FILE_ERROR_OK, error);
189 158
190 // Check that the server has received an update. 159 // Check that the server has received an update.
191 EXPECT_LT(original_changestamp, fake_service()->largest_changestamp()); 160 EXPECT_LT(original_changestamp, fake_service()->largest_changestamp());
(...skipping 19 matching lines...) Expand all
211 base::Bind(&internal::FileCache::GetCacheEntry, 180 base::Bind(&internal::FileCache::GetCacheEntry,
212 base::Unretained(cache()), 181 base::Unretained(cache()),
213 local_id, 182 local_id,
214 &cache_entry), 183 &cache_entry),
215 google_apis::test_util::CreateCopyResultCallback(&success)); 184 google_apis::test_util::CreateCopyResultCallback(&success));
216 test_util::RunBlockingPoolTask(); 185 test_util::RunBlockingPoolTask();
217 ASSERT_TRUE(success); 186 ASSERT_TRUE(success);
218 EXPECT_FALSE(cache_entry.is_dirty()); 187 EXPECT_FALSE(cache_entry.is_dirty());
219 188
220 // Again mark the cache file dirty. 189 // Again mark the cache file dirty.
190 scoped_ptr<base::ScopedClosureRunner> file_closer;
221 error = FILE_ERROR_FAILED; 191 error = FILE_ERROR_FAILED;
222 base::PostTaskAndReplyWithResult( 192 base::PostTaskAndReplyWithResult(
223 blocking_task_runner(), 193 blocking_task_runner(),
224 FROM_HERE, 194 FROM_HERE,
225 base::Bind(&internal::FileCache::OpenForWrite, 195 base::Bind(&internal::FileCache::OpenForWrite,
226 base::Unretained(cache()), 196 base::Unretained(cache()),
227 local_id, 197 local_id,
228 &file_closer), 198 &file_closer),
229 google_apis::test_util::CreateCopyResultCallback(&error)); 199 google_apis::test_util::CreateCopyResultCallback(&error));
230 test_util::RunBlockingPoolTask(); 200 test_util::RunBlockingPoolTask();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 ClientContext(USER_INITIATED), 252 ClientContext(USER_INITIATED),
283 UpdateOperation::NO_CONTENT_CHECK, 253 UpdateOperation::NO_CONTENT_CHECK,
284 google_apis::test_util::CreateCopyResultCallback(&error)); 254 google_apis::test_util::CreateCopyResultCallback(&error));
285 test_util::RunBlockingPoolTask(); 255 test_util::RunBlockingPoolTask();
286 EXPECT_EQ(FILE_ERROR_OK, error); 256 EXPECT_EQ(FILE_ERROR_OK, error);
287 257
288 // Make sure that the server is receiving a change. 258 // Make sure that the server is receiving a change.
289 EXPECT_LE(original_changestamp, fake_service()->largest_changestamp()); 259 EXPECT_LE(original_changestamp, fake_service()->largest_changestamp());
290 } 260 }
291 261
262 TEST_F(UpdateOperationTest, UpdateFileByLocalId_OpenedForWrite) {
263 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt"));
264 const std::string kResourceId("file:2_file_resource_id");
265
266 const std::string local_id = GetLocalId(kFilePath);
267 EXPECT_FALSE(local_id.empty());
268
269 const std::string kTestFileContent = "I'm being uploaded! Yay!";
270 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent));
271
272 // Emulate a situation where someone is writing to the file.
273 scoped_ptr<base::ScopedClosureRunner> file_closer;
274 FileError error = FILE_ERROR_FAILED;
275 base::PostTaskAndReplyWithResult(
276 blocking_task_runner(),
277 FROM_HERE,
278 base::Bind(&internal::FileCache::OpenForWrite,
279 base::Unretained(cache()),
280 local_id,
281 &file_closer),
282 google_apis::test_util::CreateCopyResultCallback(&error));
283 test_util::RunBlockingPoolTask();
284 EXPECT_EQ(FILE_ERROR_OK, error);
285
286 // Update. This should not clear the dirty bit.
287 error = FILE_ERROR_FAILED;
288 operation_->UpdateFileByLocalId(
289 local_id,
290 ClientContext(USER_INITIATED),
291 UpdateOperation::RUN_CONTENT_CHECK,
292 google_apis::test_util::CreateCopyResultCallback(&error));
293 test_util::RunBlockingPoolTask();
294 EXPECT_EQ(FILE_ERROR_OK, error);
295
296 // Make sure that the cache is still dirty.
297 bool success = false;
298 FileCacheEntry cache_entry;
299 base::PostTaskAndReplyWithResult(
300 blocking_task_runner(),
301 FROM_HERE,
302 base::Bind(&internal::FileCache::GetCacheEntry,
303 base::Unretained(cache()),
304 local_id,
305 &cache_entry),
306 google_apis::test_util::CreateCopyResultCallback(&success));
307 test_util::RunBlockingPoolTask();
308 EXPECT_TRUE(success);
309 EXPECT_TRUE(cache_entry.is_dirty());
310
311 // Close the file.
312 file_closer.reset();
313
314 // Update. This should clear the dirty bit.
315 error = FILE_ERROR_FAILED;
316 operation_->UpdateFileByLocalId(
317 local_id,
318 ClientContext(USER_INITIATED),
319 UpdateOperation::RUN_CONTENT_CHECK,
320 google_apis::test_util::CreateCopyResultCallback(&error));
321 test_util::RunBlockingPoolTask();
322 EXPECT_EQ(FILE_ERROR_OK, error);
323
324 // Make sure that the cache is no longer dirty.
325 base::PostTaskAndReplyWithResult(
326 blocking_task_runner(),
327 FROM_HERE,
328 base::Bind(&internal::FileCache::GetCacheEntry,
329 base::Unretained(cache()),
330 local_id,
331 &cache_entry),
332 google_apis::test_util::CreateCopyResultCallback(&success));
333 test_util::RunBlockingPoolTask();
334 EXPECT_TRUE(success);
335 EXPECT_FALSE(cache_entry.is_dirty());
336 }
337
292 } // namespace file_system 338 } // namespace file_system
293 } // namespace drive 339 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_system/update_operation.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698