[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] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 5 | #include "base/files/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 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 17 | namespace base { |
| 18 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | std::string GetFileContent(const FilePath& path) { |
| 22 | std::string content; |
| 23 | if (!file_util::ReadFileToString(path, &content)) { |
| 24 | NOTREACHED(); |
| 25 | } |
| 26 | return content; |
| 27 | } |
| 28 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 29 | class DataSerializer : public ImportantFileWriter::DataSerializer { |
| 30 | public: |
| 31 | explicit DataSerializer(const std::string& data) : data_(data) { |
| 32 | } |
| 33 | |
| 34 | virtual bool SerializeData(std::string* output) { |
| 35 | output->assign(data_); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | private: |
| 40 | const std::string data_; |
| 41 | }; |
| 42 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 43 | } // namespace |
| 44 | |
| 45 | class ImportantFileWriterTest : public testing::Test { |
| 46 | public: |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 47 | ImportantFileWriterTest() { } |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 48 | virtual void SetUp() { |
| 49 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 50 | file_ = temp_dir_.path().AppendASCII("test-file"); |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | FilePath file_; |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 55 | MessageLoop loop_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 56 | |
| 57 | private: |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 58 | ScopedTempDir temp_dir_; |
| 59 | }; |
| 60 | |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 61 | TEST_F(ImportantFileWriterTest, Basic) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 62 | ImportantFileWriter writer(file_, |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 63 | MessageLoopProxy::current()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 64 | EXPECT_FALSE(file_util::PathExists(writer.path())); |
| 65 | writer.WriteNow("foo"); |
[email protected] | 1f18bfe | 2012-10-30 01:02:55 | [diff] [blame] | 66 | loop_.RunUntilIdle(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 67 | |
| 68 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 69 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 70 | } |
| 71 | |
| 72 | TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 73 | ImportantFileWriter writer(file_, |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 74 | MessageLoopProxy::current()); |
| 75 | writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 76 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 77 | DataSerializer serializer("foo"); |
| 78 | writer.ScheduleWrite(&serializer); |
| 79 | EXPECT_TRUE(writer.HasPendingWrite()); |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 80 | MessageLoop::current()->PostDelayedTask( |
| 81 | FROM_HERE, |
| 82 | MessageLoop::QuitClosure(), |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 83 | TimeDelta::FromMilliseconds(100)); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 84 | MessageLoop::current()->Run(); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 85 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 86 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 87 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 88 | } |
| 89 | |
| 90 | TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 91 | ImportantFileWriter writer(file_, |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 92 | MessageLoopProxy::current()); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 93 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 94 | DataSerializer serializer("foo"); |
| 95 | writer.ScheduleWrite(&serializer); |
| 96 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 97 | writer.DoScheduledWrite(); |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 98 | MessageLoop::current()->PostDelayedTask( |
| 99 | FROM_HERE, |
| 100 | MessageLoop::QuitClosure(), |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 101 | TimeDelta::FromMilliseconds(100)); |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 102 | MessageLoop::current()->Run(); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 103 | EXPECT_FALSE(writer.HasPendingWrite()); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 104 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 105 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 106 | } |
| 107 | |
[email protected] | b9a8ee4d | 2012-11-08 04:29:12 | [diff] [blame^] | 108 | TEST_F(ImportantFileWriterTest, BatchingWrites) { |
[email protected] | 6658ca8 | 2010-05-20 18:20:29 | [diff] [blame] | 109 | ImportantFileWriter writer(file_, |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 110 | MessageLoopProxy::current()); |
| 111 | writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 112 | DataSerializer foo("foo"), bar("bar"), baz("baz"); |
| 113 | writer.ScheduleWrite(&foo); |
| 114 | writer.ScheduleWrite(&bar); |
| 115 | writer.ScheduleWrite(&baz); |
[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 116 | MessageLoop::current()->PostDelayedTask( |
| 117 | FROM_HERE, |
| 118 | MessageLoop::QuitClosure(), |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 119 | TimeDelta::FromMilliseconds(100)); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 120 | MessageLoop::current()->Run(); |
| 121 | ASSERT_TRUE(file_util::PathExists(writer.path())); |
| 122 | EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 123 | } |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 124 | |
| 125 | } // namespace base |