joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/files/important_file_writer.h" |
| 6 | |
| 7 | #include "base/compiler_specific.h" |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 8 | #include "base/file_util.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
brettw@chromium.org | 091db52 | 2012-11-17 05:34:23 +0900 | [diff] [blame] | 10 | #include "base/files/scoped_temp_dir.h" |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/message_loop.h" |
tfarina@chromium.org | 7d4a0ec | 2013-02-07 01:56:19 +0900 | [diff] [blame] | 13 | #include "base/run_loop.h" |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
avi@chromium.org | b039e8b | 2013-06-28 09:49:07 +0900 | [diff] [blame] | 15 | #include "base/time/time.h" |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | std::string GetFileContent(const FilePath& path) { |
| 23 | std::string content; |
| 24 | if (!file_util::ReadFileToString(path, &content)) { |
| 25 | NOTREACHED(); |
| 26 | } |
| 27 | return content; |
| 28 | } |
| 29 | |
| 30 | class DataSerializer : public ImportantFileWriter::DataSerializer { |
| 31 | public: |
| 32 | explicit DataSerializer(const std::string& data) : data_(data) { |
| 33 | } |
| 34 | |
rsleevi@chromium.org | 7cc002a | 2013-02-07 16:20:55 +0900 | [diff] [blame] | 35 | virtual bool SerializeData(std::string* output) OVERRIDE { |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 36 | output->assign(data_); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | const std::string data_; |
| 42 | }; |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | class ImportantFileWriterTest : public testing::Test { |
| 47 | public: |
| 48 | ImportantFileWriterTest() { } |
| 49 | virtual void SetUp() { |
| 50 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 51 | file_ = temp_dir_.path().AppendASCII("test-file"); |
| 52 | } |
| 53 | |
| 54 | protected: |
| 55 | FilePath file_; |
| 56 | MessageLoop loop_; |
| 57 | |
| 58 | private: |
| 59 | ScopedTempDir temp_dir_; |
| 60 | }; |
| 61 | |
| 62 | TEST_F(ImportantFileWriterTest, Basic) { |
rsleevi@chromium.org | 64293f1 | 2013-06-12 01:46:36 +0900 | [diff] [blame] | 63 | ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 64 | EXPECT_FALSE(PathExists(writer.path())); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 65 | writer.WriteNow("foo"); |
tfarina@chromium.org | 7d4a0ec | 2013-02-07 01:56:19 +0900 | [diff] [blame] | 66 | RunLoop().RunUntilIdle(); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 67 | |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 68 | ASSERT_TRUE(PathExists(writer.path())); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 69 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 70 | } |
| 71 | |
| 72 | TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
rsleevi@chromium.org | 64293f1 | 2013-06-12 01:46:36 +0900 | [diff] [blame] | 73 | ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 74 | writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
| 75 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 76 | DataSerializer serializer("foo"); |
| 77 | writer.ScheduleWrite(&serializer); |
| 78 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 79 | MessageLoop::current()->PostDelayedTask( |
| 80 | FROM_HERE, |
thakis@chromium.org | fc6f929 | 2013-02-05 08:40:00 +0900 | [diff] [blame] | 81 | MessageLoop::QuitWhenIdleClosure(), |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 82 | TimeDelta::FromMilliseconds(100)); |
| 83 | MessageLoop::current()->Run(); |
| 84 | EXPECT_FALSE(writer.HasPendingWrite()); |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 85 | ASSERT_TRUE(PathExists(writer.path())); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 86 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 87 | } |
| 88 | |
| 89 | TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
rsleevi@chromium.org | 64293f1 | 2013-06-12 01:46:36 +0900 | [diff] [blame] | 90 | ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 91 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 92 | DataSerializer serializer("foo"); |
| 93 | writer.ScheduleWrite(&serializer); |
| 94 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 95 | writer.DoScheduledWrite(); |
| 96 | MessageLoop::current()->PostDelayedTask( |
| 97 | FROM_HERE, |
thakis@chromium.org | fc6f929 | 2013-02-05 08:40:00 +0900 | [diff] [blame] | 98 | MessageLoop::QuitWhenIdleClosure(), |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 99 | TimeDelta::FromMilliseconds(100)); |
| 100 | MessageLoop::current()->Run(); |
| 101 | EXPECT_FALSE(writer.HasPendingWrite()); |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 102 | ASSERT_TRUE(PathExists(writer.path())); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 103 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 104 | } |
| 105 | |
phajdan.jr@chromium.org | f3dfdd4 | 2012-11-08 13:29:12 +0900 | [diff] [blame] | 106 | TEST_F(ImportantFileWriterTest, BatchingWrites) { |
rsleevi@chromium.org | 64293f1 | 2013-06-12 01:46:36 +0900 | [diff] [blame] | 107 | ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 108 | writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
| 109 | DataSerializer foo("foo"), bar("bar"), baz("baz"); |
| 110 | writer.ScheduleWrite(&foo); |
| 111 | writer.ScheduleWrite(&bar); |
| 112 | writer.ScheduleWrite(&baz); |
| 113 | MessageLoop::current()->PostDelayedTask( |
| 114 | FROM_HERE, |
thakis@chromium.org | fc6f929 | 2013-02-05 08:40:00 +0900 | [diff] [blame] | 115 | MessageLoop::QuitWhenIdleClosure(), |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 116 | TimeDelta::FromMilliseconds(100)); |
| 117 | MessageLoop::current()->Run(); |
brettw@chromium.org | 10b6412 | 2013-07-12 02:36:07 +0900 | [diff] [blame^] | 118 | ASSERT_TRUE(PathExists(writer.path())); |
joi@chromium.org | 20009e3 | 2012-10-25 01:33:36 +0900 | [diff] [blame] | 119 | EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 120 | } |
| 121 | |
| 122 | } // namespace base |