| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/important_file_writer.h" | 5 #include "base/files/important_file_writer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" |
| 15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 std::string GetFileContent(const FilePath& path) { | 24 std::string GetFileContent(const FilePath& path) { |
| 24 std::string content; | 25 std::string content; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 protected: | 91 protected: |
| 91 SuccessfulWriteObserver successful_write_observer_; | 92 SuccessfulWriteObserver successful_write_observer_; |
| 92 FilePath file_; | 93 FilePath file_; |
| 93 MessageLoop loop_; | 94 MessageLoop loop_; |
| 94 | 95 |
| 95 private: | 96 private: |
| 96 ScopedTempDir temp_dir_; | 97 ScopedTempDir temp_dir_; |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 TEST_F(ImportantFileWriterTest, Basic) { | 100 TEST_F(ImportantFileWriterTest, Basic) { |
| 100 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 101 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 101 EXPECT_FALSE(PathExists(writer.path())); | 102 EXPECT_FALSE(PathExists(writer.path())); |
| 102 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 103 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 103 writer.WriteNow("foo"); | 104 writer.WriteNow("foo"); |
| 104 RunLoop().RunUntilIdle(); | 105 RunLoop().RunUntilIdle(); |
| 105 | 106 |
| 106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 107 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 107 ASSERT_TRUE(PathExists(writer.path())); | 108 ASSERT_TRUE(PathExists(writer.path())); |
| 108 EXPECT_EQ("foo", GetFileContent(writer.path())); | 109 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 109 } | 110 } |
| 110 | 111 |
| 111 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { | 112 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { |
| 112 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 113 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 113 EXPECT_FALSE(PathExists(writer.path())); | 114 EXPECT_FALSE(PathExists(writer.path())); |
| 114 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 115 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 115 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); | 116 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); |
| 116 writer.WriteNow("foo"); | 117 writer.WriteNow("foo"); |
| 117 RunLoop().RunUntilIdle(); | 118 RunLoop().RunUntilIdle(); |
| 118 | 119 |
| 119 // Confirm that the observer is invoked. | 120 // Confirm that the observer is invoked. |
| 120 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); | 121 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); |
| 121 ASSERT_TRUE(PathExists(writer.path())); | 122 ASSERT_TRUE(PathExists(writer.path())); |
| 122 EXPECT_EQ("foo", GetFileContent(writer.path())); | 123 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 136 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 137 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 137 writer.WriteNow("baz"); | 138 writer.WriteNow("baz"); |
| 138 RunLoop().RunUntilIdle(); | 139 RunLoop().RunUntilIdle(); |
| 139 | 140 |
| 140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); | 141 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); |
| 141 ASSERT_TRUE(PathExists(writer.path())); | 142 ASSERT_TRUE(PathExists(writer.path())); |
| 142 EXPECT_EQ("baz", GetFileContent(writer.path())); | 143 EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 143 } | 144 } |
| 144 | 145 |
| 145 TEST_F(ImportantFileWriterTest, ScheduleWrite) { | 146 TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
| 146 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 147 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 147 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 148 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
| 148 EXPECT_FALSE(writer.HasPendingWrite()); | 149 EXPECT_FALSE(writer.HasPendingWrite()); |
| 149 DataSerializer serializer("foo"); | 150 DataSerializer serializer("foo"); |
| 150 writer.ScheduleWrite(&serializer); | 151 writer.ScheduleWrite(&serializer); |
| 151 EXPECT_TRUE(writer.HasPendingWrite()); | 152 EXPECT_TRUE(writer.HasPendingWrite()); |
| 152 MessageLoop::current()->PostDelayedTask( | 153 MessageLoop::current()->PostDelayedTask( |
| 153 FROM_HERE, | 154 FROM_HERE, |
| 154 MessageLoop::QuitWhenIdleClosure(), | 155 MessageLoop::QuitWhenIdleClosure(), |
| 155 TimeDelta::FromMilliseconds(100)); | 156 TimeDelta::FromMilliseconds(100)); |
| 156 MessageLoop::current()->Run(); | 157 MessageLoop::current()->Run(); |
| 157 EXPECT_FALSE(writer.HasPendingWrite()); | 158 EXPECT_FALSE(writer.HasPendingWrite()); |
| 158 ASSERT_TRUE(PathExists(writer.path())); | 159 ASSERT_TRUE(PathExists(writer.path())); |
| 159 EXPECT_EQ("foo", GetFileContent(writer.path())); | 160 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 160 } | 161 } |
| 161 | 162 |
| 162 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { | 163 TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
| 163 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 164 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 164 EXPECT_FALSE(writer.HasPendingWrite()); | 165 EXPECT_FALSE(writer.HasPendingWrite()); |
| 165 DataSerializer serializer("foo"); | 166 DataSerializer serializer("foo"); |
| 166 writer.ScheduleWrite(&serializer); | 167 writer.ScheduleWrite(&serializer); |
| 167 EXPECT_TRUE(writer.HasPendingWrite()); | 168 EXPECT_TRUE(writer.HasPendingWrite()); |
| 168 writer.DoScheduledWrite(); | 169 writer.DoScheduledWrite(); |
| 169 MessageLoop::current()->PostDelayedTask( | 170 MessageLoop::current()->PostDelayedTask( |
| 170 FROM_HERE, | 171 FROM_HERE, |
| 171 MessageLoop::QuitWhenIdleClosure(), | 172 MessageLoop::QuitWhenIdleClosure(), |
| 172 TimeDelta::FromMilliseconds(100)); | 173 TimeDelta::FromMilliseconds(100)); |
| 173 MessageLoop::current()->Run(); | 174 MessageLoop::current()->Run(); |
| 174 EXPECT_FALSE(writer.HasPendingWrite()); | 175 EXPECT_FALSE(writer.HasPendingWrite()); |
| 175 ASSERT_TRUE(PathExists(writer.path())); | 176 ASSERT_TRUE(PathExists(writer.path())); |
| 176 EXPECT_EQ("foo", GetFileContent(writer.path())); | 177 EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 177 } | 178 } |
| 178 | 179 |
| 179 TEST_F(ImportantFileWriterTest, BatchingWrites) { | 180 TEST_F(ImportantFileWriterTest, BatchingWrites) { |
| 180 ImportantFileWriter writer(file_, MessageLoopProxy::current().get()); | 181 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 181 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); | 182 writer.set_commit_interval(TimeDelta::FromMilliseconds(25)); |
| 182 DataSerializer foo("foo"), bar("bar"), baz("baz"); | 183 DataSerializer foo("foo"), bar("bar"), baz("baz"); |
| 183 writer.ScheduleWrite(&foo); | 184 writer.ScheduleWrite(&foo); |
| 184 writer.ScheduleWrite(&bar); | 185 writer.ScheduleWrite(&bar); |
| 185 writer.ScheduleWrite(&baz); | 186 writer.ScheduleWrite(&baz); |
| 186 MessageLoop::current()->PostDelayedTask( | 187 MessageLoop::current()->PostDelayedTask( |
| 187 FROM_HERE, | 188 FROM_HERE, |
| 188 MessageLoop::QuitWhenIdleClosure(), | 189 MessageLoop::QuitWhenIdleClosure(), |
| 189 TimeDelta::FromMilliseconds(100)); | 190 TimeDelta::FromMilliseconds(100)); |
| 190 MessageLoop::current()->Run(); | 191 MessageLoop::current()->Run(); |
| 191 ASSERT_TRUE(PathExists(writer.path())); | 192 ASSERT_TRUE(PathExists(writer.path())); |
| 192 EXPECT_EQ("baz", GetFileContent(writer.path())); | 193 EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 193 } | 194 } |
| 194 | 195 |
| 195 } // namespace base | 196 } // namespace base |
| OLD | NEW |