[email protected] | 64bb4cb3 | 2012-01-05 19:17:16 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 5 | #include "chrome/common/important_file_writer.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 6 | |
| 7 | #include "base/compiler_specific.h" |
| 8 | #include "base/file_path.h" |
| 9 | #include "base/file_util.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/message_loop.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 12 | #include "base/scoped_temp_dir.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 14 | #include "base/time.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | std::string GetFileContent(const FilePath& path) { |
| 20 | std::string content; |
| 21 | if (!file_util::ReadFileToString(path, &content)) { |
| 22 | NOTREACHED(); |
| 23 | } |
| 24 | return content; |
| 25 | } |
| 26 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 27 | class DataSerializer : public ImportantFileWriter::DataSerializer { |
| 28 | public: |
| 29 | explicit DataSerializer(const std::string& data) : data_(data) { |
| 30 | } |
| 31 | |
| 32 | virtual bool SerializeData(std::string* output) { |
| 33 | output->assign(data_); |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | const std::string data_; |
| 39 | }; |
| 40 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 41 | } // namespace |
| 42 | |
| 43 | class ImportantFileWriterTest : public testing::Test { |
| 44 | public: |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 45 | ImportantFileWriterTest() { } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 46 | virtual void SetUp() { |
| 47 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 48 | file_ = temp_dir_.path().AppendASCII("test-file"); |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | FilePath file_; |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 53 | MessageLoop loop_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 54 | |
| 55 | private: |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 56 | ScopedTempDir temp_dir_; |
| 57 | }; |
| 58 | |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 59 | TEST_F(ImportantFileWriterTest, Basic) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 60 | ImportantFileWriter writer(file_, |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 61 | base::MessageLoopProxy::current()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 62 | EXPECT_FALSE(file_util::PathExists(writer.path())); |
| 63 | writer.WriteNow("foo"); |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 64 | loop_.RunAllPending(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 65 | |
| 66 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 67 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 68 | } |
| 69 | |
| 70 | TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 71 | ImportantFileWriter writer(file_, |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 72 | base::MessageLoopProxy::current()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 73 | writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 74 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 75 | DataSerializer serializer("foo"); |
| 76 | writer.ScheduleWrite(&serializer); |
| 77 | EXPECT_TRUE(writer.HasPendingWrite()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 78 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | a778709f | 2011-12-10 00:28:17 | [diff] [blame] | 79 | MessageLoop::QuitClosure(), 100); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 80 | MessageLoop::current()->Run(); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 81 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 82 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 83 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 84 | } |
| 85 | |
| 86 | TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 87 | ImportantFileWriter writer(file_, |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 88 | base::MessageLoopProxy::current()); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 89 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 90 | DataSerializer serializer("foo"); |
| 91 | writer.ScheduleWrite(&serializer); |
| 92 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 93 | writer.DoScheduledWrite(); |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 94 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | a778709f | 2011-12-10 00:28:17 | [diff] [blame] | 95 | MessageLoop::QuitClosure(), 100); |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 96 | MessageLoop::current()->Run(); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 97 | EXPECT_FALSE(writer.HasPendingWrite()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 98 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 99 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 100 | } |
| 101 | |
[email protected] | 64bb4cb3 | 2012-01-05 19:17:16 | [diff] [blame^] | 102 | // Flaky - http://crbug.com/109292 |
| 103 | TEST_F(ImportantFileWriterTest, FLAKY_BatchingWrites) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 104 | ImportantFileWriter writer(file_, |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 105 | base::MessageLoopProxy::current()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 106 | writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25)); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 107 | DataSerializer foo("foo"), bar("bar"), baz("baz"); |
| 108 | writer.ScheduleWrite(&foo); |
| 109 | writer.ScheduleWrite(&bar); |
| 110 | writer.ScheduleWrite(&baz); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 111 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | a778709f | 2011-12-10 00:28:17 | [diff] [blame] | 112 | MessageLoop::QuitClosure(), 100); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 113 | MessageLoop::current()->Run(); |
| 114 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 115 | EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 116 | } |