| OLD | NEW |
| (Empty) |
| 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/bind.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/files/file_util.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "base/threading/thread.h" | |
| 18 #include "base/time/time.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace base { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 std::string GetFileContent(const FilePath& path) { | |
| 26 std::string content; | |
| 27 if (!ReadFileToString(path, &content)) { | |
| 28 NOTREACHED(); | |
| 29 } | |
| 30 return content; | |
| 31 } | |
| 32 | |
| 33 class DataSerializer : public ImportantFileWriter::DataSerializer { | |
| 34 public: | |
| 35 explicit DataSerializer(const std::string& data) : data_(data) { | |
| 36 } | |
| 37 | |
| 38 bool SerializeData(std::string* output) override { | |
| 39 output->assign(data_); | |
| 40 return true; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 const std::string data_; | |
| 45 }; | |
| 46 | |
| 47 class SuccessfulWriteObserver { | |
| 48 public: | |
| 49 SuccessfulWriteObserver() : successful_write_observed_(false) {} | |
| 50 | |
| 51 // Register on_successful_write() to be called on the next successful write | |
| 52 // of |writer|. | |
| 53 void ObserveNextSuccessfulWrite(ImportantFileWriter* writer); | |
| 54 | |
| 55 // Returns true if a successful write was observed via on_successful_write() | |
| 56 // and resets the observation state to false regardless. | |
| 57 bool GetAndResetObservationState(); | |
| 58 | |
| 59 private: | |
| 60 void on_successful_write() { | |
| 61 EXPECT_FALSE(successful_write_observed_); | |
| 62 successful_write_observed_ = true; | |
| 63 } | |
| 64 | |
| 65 bool successful_write_observed_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver); | |
| 68 }; | |
| 69 | |
| 70 void SuccessfulWriteObserver::ObserveNextSuccessfulWrite( | |
| 71 ImportantFileWriter* writer) { | |
| 72 writer->RegisterOnNextSuccessfulWriteCallback(base::Bind( | |
| 73 &SuccessfulWriteObserver::on_successful_write, base::Unretained(this))); | |
| 74 } | |
| 75 | |
| 76 bool SuccessfulWriteObserver::GetAndResetObservationState() { | |
| 77 bool was_successful_write_observed = successful_write_observed_; | |
| 78 successful_write_observed_ = false; | |
| 79 return was_successful_write_observed; | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 class ImportantFileWriterTest : public testing::Test { | |
| 85 public: | |
| 86 ImportantFileWriterTest() { } | |
| 87 void SetUp() override { | |
| 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 89 file_ = temp_dir_.path().AppendASCII("test-file"); | |
| 90 } | |
| 91 | |
| 92 protected: | |
| 93 SuccessfulWriteObserver successful_write_observer_; | |
| 94 FilePath file_; | |
| 95 MessageLoop loop_; | |
| 96 | |
| 97 private: | |
| 98 ScopedTempDir temp_dir_; | |
| 99 }; | |
| 100 | |
| 101 TEST_F(ImportantFileWriterTest, Basic) { | |
| 102 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | |
| 103 EXPECT_FALSE(PathExists(writer.path())); | |
| 104 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 105 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | |
| 106 RunLoop().RunUntilIdle(); | |
| 107 | |
| 108 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 109 ASSERT_TRUE(PathExists(writer.path())); | |
| 110 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
| 111 } | |
| 112 | |
| 113 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | |
| 114 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | |
| 115 EXPECT_FALSE(PathExists(writer.path())); | |
| 116 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 117 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | |
| 118 writer.WriteNow(make_scoped_ptr(new std::string("foo"))); | |
| 119 RunLoop().RunUntilIdle(); | |
| 120 | |
| 121 // Confirm that the observer is invoked. | |
| 122 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | |
| 123 ASSERT_TRUE(PathExists(writer.path())); | |
| 124 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
| 125 | |
| 126 // Confirm that re-installing the observer works for another write. | |
| 127 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 128 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | |
| 129 writer.WriteNow(make_scoped_ptr(new std::string("bar"))); | |
| 130 RunLoop().RunUntilIdle(); | |
| 131 | |
| 132 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | |
| 133 ASSERT_TRUE(PathExists(writer.path())); | |
| 134 EXPECT_EQ("bar", GetFileContent(writer.path())); | |
| 135 | |
| 136 // Confirm that writing again without re-installing the observer doesn't | |
| 137 // result in a notification. | |
| 138 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 139 writer.WriteNow(make_scoped_ptr(new std::string("baz"))); | |
| 140 RunLoop().RunUntilIdle(); | |
| 141 | |
| 142 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | |
| 143 ASSERT_TRUE(PathExists(writer.path())); | |
| 144 EXPECT_EQ("baz", GetFileContent(writer.path())); | |
| 145 } | |
| 146 | |
| 147 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | |
| 148 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | |
| 149 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | |
| 150 EXPECT_FALSE(writer.HasPendingWrite()); | |
| 151 DataSerializer serializer("foo"); | |
| 152 writer.ScheduleWrite(&serializer); | |
| 153 EXPECT_TRUE(writer.HasPendingWrite()); | |
| 154 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 155 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | |
| 156 TimeDelta::FromMilliseconds(100)); | |
| 157 MessageLoop::current()->Run(); | |
| 158 EXPECT_FALSE(writer.HasPendingWrite()); | |
| 159 ASSERT_TRUE(PathExists(writer.path())); | |
| 160 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
| 161 } | |
| 162 | |
| 163 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | |
| 164 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | |
| 165 EXPECT_FALSE(writer.HasPendingWrite()); | |
| 166 DataSerializer serializer("foo"); | |
| 167 writer.ScheduleWrite(&serializer); | |
| 168 EXPECT_TRUE(writer.HasPendingWrite()); | |
| 169 writer.DoScheduledWrite(); | |
| 170 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 171 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | |
| 172 TimeDelta::FromMilliseconds(100)); | |
| 173 MessageLoop::current()->Run(); | |
| 174 EXPECT_FALSE(writer.HasPendingWrite()); | |
| 175 ASSERT_TRUE(PathExists(writer.path())); | |
| 176 EXPECT_EQ("foo", GetFileContent(writer.path())); | |
| 177 } | |
| 178 | |
| 179 TEST_F(ImportantFileWriterTest, BatchingWrites) { | |
| 180 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); | |
| 181 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | |
| 182 DataSerializer foo("foo"), bar("bar"), baz("baz"); | |
| 183 writer.ScheduleWrite(&foo); | |
| 184 writer.ScheduleWrite(&bar); | |
| 185 writer.ScheduleWrite(&baz); | |
| 186 ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 187 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), | |
| 188 TimeDelta::FromMilliseconds(100)); | |
| 189 MessageLoop::current()->Run(); | |
| 190 ASSERT_TRUE(PathExists(writer.path())); | |
| 191 EXPECT_EQ("baz", GetFileContent(writer.path())); | |
| 192 } | |
| 193 | |
| 194 } // namespace base | |
| OLD | NEW |