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