[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 | |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 12 | #include "base/location.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | 543540e | 2015-12-24 05:15:32 | [diff] [blame] | 14 | #include "base/macros.h" |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
Gabriel Charette | 629ba73c | 2018-05-01 22:53:54 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 17 | #include "base/run_loop.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
Devlin Cronin | 15291e9c | 2018-06-07 21:37:48 | [diff] [blame] | 19 | #include "base/test/metrics/histogram_tester.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
gab | 6cce2f3 | 2016-05-11 19:53:43 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 22 | #include "base/time/time.h" |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 23 | #include "base/timer/mock_timer.h" |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 26 | namespace base { |
| 27 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | std::string GetFileContent(const FilePath& path) { |
| 31 | std::string content; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 32 | if (!ReadFileToString(path, &content)) { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 33 | NOTREACHED(); |
| 34 | } |
| 35 | return content; |
| 36 | } |
| 37 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 38 | class DataSerializer : public ImportantFileWriter::DataSerializer { |
| 39 | public: |
| 40 | explicit DataSerializer(const std::string& data) : data_(data) { |
| 41 | } |
| 42 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 43 | bool SerializeData(std::string* output) override { |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 44 | output->assign(data_); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | const std::string data_; |
| 50 | }; |
| 51 | |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 52 | class FailingDataSerializer : public ImportantFileWriter::DataSerializer { |
| 53 | public: |
| 54 | bool SerializeData(std::string* output) override { return false; } |
| 55 | }; |
| 56 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 57 | enum WriteCallbackObservationState { |
| 58 | NOT_CALLED, |
| 59 | CALLED_WITH_ERROR, |
| 60 | CALLED_WITH_SUCCESS, |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 61 | }; |
| 62 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 63 | class WriteCallbacksObserver { |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 64 | public: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 65 | WriteCallbacksObserver() = default; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 66 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 67 | // Register OnBeforeWrite() and OnAfterWrite() to be called on the next write |
| 68 | // of |writer|. |
| 69 | void ObserveNextWriteCallbacks(ImportantFileWriter* writer); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 70 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 71 | // Returns the |WriteCallbackObservationState| which was observed, then resets |
| 72 | // it to |NOT_CALLED|. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 73 | WriteCallbackObservationState GetAndResetObservationState(); |
| 74 | |
| 75 | private: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 76 | void OnBeforeWrite() { |
| 77 | EXPECT_FALSE(before_write_called_); |
| 78 | before_write_called_ = true; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 79 | } |
| 80 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 81 | void OnAfterWrite(bool success) { |
| 82 | EXPECT_EQ(NOT_CALLED, after_write_observation_state_); |
| 83 | after_write_observation_state_ = |
| 84 | success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR; |
| 85 | } |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 86 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 87 | bool before_write_called_ = false; |
| 88 | WriteCallbackObservationState after_write_observation_state_ = NOT_CALLED; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(WriteCallbacksObserver); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 91 | }; |
| 92 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 93 | void WriteCallbacksObserver::ObserveNextWriteCallbacks( |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 94 | ImportantFileWriter* writer) { |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 95 | writer->RegisterOnNextWriteCallbacks( |
| 96 | base::Bind(&WriteCallbacksObserver::OnBeforeWrite, |
| 97 | base::Unretained(this)), |
| 98 | base::Bind(&WriteCallbacksObserver::OnAfterWrite, |
| 99 | base::Unretained(this))); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 100 | } |
| 101 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 102 | WriteCallbackObservationState |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 103 | WriteCallbacksObserver::GetAndResetObservationState() { |
| 104 | EXPECT_EQ(after_write_observation_state_ != NOT_CALLED, before_write_called_) |
| 105 | << "The before-write callback should always be called before the " |
| 106 | "after-write callback"; |
| 107 | |
| 108 | WriteCallbackObservationState state = after_write_observation_state_; |
| 109 | before_write_called_ = false; |
| 110 | after_write_observation_state_ = NOT_CALLED; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 111 | return state; |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 114 | } // namespace |
| 115 | |
| 116 | class ImportantFileWriterTest : public testing::Test { |
| 117 | public: |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 118 | ImportantFileWriterTest() = default; |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 119 | void SetUp() override { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 120 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 411f4fc | 2016-09-08 09:26:27 | [diff] [blame] | 121 | file_ = temp_dir_.GetPath().AppendASCII("test-file"); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | protected: |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 125 | WriteCallbacksObserver write_callback_observer_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 126 | FilePath file_; |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 127 | MessageLoop loop_; |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 128 | |
| 129 | private: |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 130 | ScopedTempDir temp_dir_; |
| 131 | }; |
| 132 | |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 133 | TEST_F(ImportantFileWriterTest, Basic) { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 134 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 135 | EXPECT_FALSE(PathExists(writer.path())); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 136 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 137 | writer.WriteNow(std::make_unique<std::string>("foo")); |
[email protected] | 7ff48ca | 2013-02-06 16:56:19 | [diff] [blame] | 138 | RunLoop().RunUntilIdle(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 139 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 140 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 141 | ASSERT_TRUE(PathExists(writer.path())); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 142 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 143 | } |
| 144 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 145 | TEST_F(ImportantFileWriterTest, WriteWithObserver) { |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 146 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 147 | EXPECT_FALSE(PathExists(writer.path())); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 148 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
| 149 | |
| 150 | // Confirm that the observer is invoked. |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 151 | write_callback_observer_.ObserveNextWriteCallbacks(&writer); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 152 | writer.WriteNow(std::make_unique<std::string>("foo")); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 153 | RunLoop().RunUntilIdle(); |
| 154 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 155 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 156 | write_callback_observer_.GetAndResetObservationState()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 157 | ASSERT_TRUE(PathExists(writer.path())); |
| 158 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 159 | |
| 160 | // Confirm that re-installing the observer works for another write. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 161 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 162 | write_callback_observer_.ObserveNextWriteCallbacks(&writer); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 163 | writer.WriteNow(std::make_unique<std::string>("bar")); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 164 | RunLoop().RunUntilIdle(); |
| 165 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 166 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 167 | write_callback_observer_.GetAndResetObservationState()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 168 | ASSERT_TRUE(PathExists(writer.path())); |
| 169 | EXPECT_EQ("bar", GetFileContent(writer.path())); |
| 170 | |
| 171 | // Confirm that writing again without re-installing the observer doesn't |
| 172 | // result in a notification. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 173 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 174 | writer.WriteNow(std::make_unique<std::string>("baz")); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 175 | RunLoop().RunUntilIdle(); |
| 176 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 177 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
[email protected] | e33c951 | 2014-05-12 02:24:13 | [diff] [blame] | 178 | ASSERT_TRUE(PathExists(writer.path())); |
| 179 | EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 180 | } |
| 181 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 182 | TEST_F(ImportantFileWriterTest, FailedWriteWithObserver) { |
| 183 | // Use an invalid file path (relative paths are invalid) to get a |
| 184 | // FILE_ERROR_ACCESS_DENIED error when trying to write the file. |
| 185 | ImportantFileWriter writer(FilePath().AppendASCII("bad/../path"), |
| 186 | ThreadTaskRunnerHandle::Get()); |
| 187 | EXPECT_FALSE(PathExists(writer.path())); |
| 188 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 189 | write_callback_observer_.ObserveNextWriteCallbacks(&writer); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 190 | writer.WriteNow(std::make_unique<std::string>("foo")); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 191 | RunLoop().RunUntilIdle(); |
| 192 | |
| 193 | // Confirm that the write observer was invoked with its boolean parameter set |
| 194 | // to false. |
| 195 | EXPECT_EQ(CALLED_WITH_ERROR, |
| 196 | write_callback_observer_.GetAndResetObservationState()); |
| 197 | EXPECT_FALSE(PathExists(writer.path())); |
| 198 | } |
| 199 | |
| 200 | TEST_F(ImportantFileWriterTest, CallbackRunsOnWriterThread) { |
| 201 | base::Thread file_writer_thread("ImportantFileWriter test thread"); |
| 202 | file_writer_thread.Start(); |
| 203 | ImportantFileWriter writer(file_, file_writer_thread.task_runner()); |
| 204 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
| 205 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 206 | // Block execution on |file_writer_thread| to verify that callbacks are |
| 207 | // executed on it. |
| 208 | base::WaitableEvent wait_helper( |
| 209 | base::WaitableEvent::ResetPolicy::MANUAL, |
| 210 | base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 211 | file_writer_thread.task_runner()->PostTask( |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 212 | FROM_HERE, base::BindOnce(&base::WaitableEvent::Wait, |
| 213 | base::Unretained(&wait_helper))); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 214 | |
| 215 | write_callback_observer_.ObserveNextWriteCallbacks(&writer); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 216 | writer.WriteNow(std::make_unique<std::string>("foo")); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 217 | RunLoop().RunUntilIdle(); |
| 218 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 219 | // Expect the callback to not have been executed before the |
| 220 | // |file_writer_thread| is unblocked. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 221 | EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); |
| 222 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 223 | wait_helper.Signal(); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 224 | file_writer_thread.FlushForTesting(); |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 225 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 226 | EXPECT_EQ(CALLED_WITH_SUCCESS, |
| 227 | write_callback_observer_.GetAndResetObservationState()); |
| 228 | ASSERT_TRUE(PathExists(writer.path())); |
| 229 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 230 | } |
| 231 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 232 | TEST_F(ImportantFileWriterTest, ScheduleWrite) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 233 | constexpr TimeDelta kCommitInterval = TimeDelta::FromSeconds(12345); |
| 234 | MockTimer timer(true, false); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 235 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get(), |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 236 | kCommitInterval); |
| 237 | writer.SetTimerForTesting(&timer); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 238 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 239 | DataSerializer serializer("foo"); |
| 240 | writer.ScheduleWrite(&serializer); |
| 241 | EXPECT_TRUE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 242 | ASSERT_TRUE(timer.IsRunning()); |
| 243 | EXPECT_EQ(kCommitInterval, timer.GetCurrentDelay()); |
| 244 | timer.Fire(); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 245 | EXPECT_FALSE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 246 | EXPECT_FALSE(timer.IsRunning()); |
| 247 | RunLoop().RunUntilIdle(); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 248 | ASSERT_TRUE(PathExists(writer.path())); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 249 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 250 | } |
| 251 | |
| 252 | TEST_F(ImportantFileWriterTest, DoScheduledWrite) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 253 | MockTimer timer(true, false); |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 254 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 255 | writer.SetTimerForTesting(&timer); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 256 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 257 | DataSerializer serializer("foo"); |
| 258 | writer.ScheduleWrite(&serializer); |
| 259 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 260 | writer.DoScheduledWrite(); |
| 261 | EXPECT_FALSE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 262 | RunLoop().RunUntilIdle(); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 263 | ASSERT_TRUE(PathExists(writer.path())); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 264 | EXPECT_EQ("foo", GetFileContent(writer.path())); |
| 265 | } |
| 266 | |
[email protected] | b9a8ee4d | 2012-11-08 04:29:12 | [diff] [blame] | 267 | TEST_F(ImportantFileWriterTest, BatchingWrites) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 268 | MockTimer timer(true, false); |
| 269 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 270 | writer.SetTimerForTesting(&timer); |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 271 | DataSerializer foo("foo"), bar("bar"), baz("baz"); |
| 272 | writer.ScheduleWrite(&foo); |
| 273 | writer.ScheduleWrite(&bar); |
| 274 | writer.ScheduleWrite(&baz); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 275 | ASSERT_TRUE(timer.IsRunning()); |
| 276 | timer.Fire(); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 277 | RunLoop().RunUntilIdle(); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 278 | ASSERT_TRUE(PathExists(writer.path())); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 279 | EXPECT_EQ("baz", GetFileContent(writer.path())); |
| 280 | } |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 281 | |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 282 | TEST_F(ImportantFileWriterTest, ScheduleWrite_FailToSerialize) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 283 | MockTimer timer(true, false); |
| 284 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 285 | writer.SetTimerForTesting(&timer); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 286 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 287 | FailingDataSerializer serializer; |
| 288 | writer.ScheduleWrite(&serializer); |
| 289 | EXPECT_TRUE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 290 | ASSERT_TRUE(timer.IsRunning()); |
| 291 | timer.Fire(); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 292 | EXPECT_FALSE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 293 | RunLoop().RunUntilIdle(); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 294 | EXPECT_FALSE(PathExists(writer.path())); |
| 295 | } |
| 296 | |
| 297 | TEST_F(ImportantFileWriterTest, ScheduleWrite_WriteNow) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 298 | MockTimer timer(true, false); |
| 299 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 300 | writer.SetTimerForTesting(&timer); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 301 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 302 | DataSerializer serializer("foo"); |
| 303 | writer.ScheduleWrite(&serializer); |
| 304 | EXPECT_TRUE(writer.HasPendingWrite()); |
Jeremy Roman | 9532f25 | 2017-08-16 23:27:24 | [diff] [blame] | 305 | writer.WriteNow(std::make_unique<std::string>("bar")); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 306 | EXPECT_FALSE(writer.HasPendingWrite()); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 307 | EXPECT_FALSE(timer.IsRunning()); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 308 | |
| 309 | RunLoop().RunUntilIdle(); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 310 | ASSERT_TRUE(PathExists(writer.path())); |
| 311 | EXPECT_EQ("bar", GetFileContent(writer.path())); |
| 312 | } |
| 313 | |
| 314 | TEST_F(ImportantFileWriterTest, DoScheduledWrite_FailToSerialize) { |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 315 | MockTimer timer(true, false); |
| 316 | ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); |
| 317 | writer.SetTimerForTesting(&timer); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 318 | EXPECT_FALSE(writer.HasPendingWrite()); |
| 319 | FailingDataSerializer serializer; |
| 320 | writer.ScheduleWrite(&serializer); |
| 321 | EXPECT_TRUE(writer.HasPendingWrite()); |
| 322 | |
| 323 | writer.DoScheduledWrite(); |
Sam McNally | 365428e | 2017-05-22 05:25:22 | [diff] [blame] | 324 | EXPECT_FALSE(timer.IsRunning()); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 325 | EXPECT_FALSE(writer.HasPendingWrite()); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 326 | RunLoop().RunUntilIdle(); |
Sam McNally | 8f50faa | 2017-05-19 05:08:00 | [diff] [blame] | 327 | EXPECT_FALSE(PathExists(writer.path())); |
| 328 | } |
| 329 | |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 330 | TEST_F(ImportantFileWriterTest, WriteFileAtomicallyHistogramSuffixTest) { |
| 331 | base::HistogramTester histogram_tester; |
| 332 | EXPECT_FALSE(PathExists(file_)); |
| 333 | EXPECT_TRUE(ImportantFileWriter::WriteFileAtomically(file_, "baz", "test")); |
| 334 | EXPECT_TRUE(PathExists(file_)); |
| 335 | EXPECT_EQ("baz", GetFileContent(file_)); |
| 336 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError", 0); |
| 337 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError.test", 0); |
| 338 | |
Scott Graham | d2c3c29 | 2017-06-20 19:28:47 | [diff] [blame] | 339 | FilePath invalid_file_ = FilePath().AppendASCII("bad/../non_existent/path"); |
xaerox | 4ae8d17 | 2017-06-20 10:20:12 | [diff] [blame] | 340 | EXPECT_FALSE(PathExists(invalid_file_)); |
| 341 | EXPECT_FALSE( |
| 342 | ImportantFileWriter::WriteFileAtomically(invalid_file_, nullptr)); |
| 343 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError", 1); |
| 344 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError.test", 0); |
| 345 | EXPECT_FALSE( |
| 346 | ImportantFileWriter::WriteFileAtomically(invalid_file_, nullptr, "test")); |
| 347 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError", 1); |
| 348 | histogram_tester.ExpectTotalCount("ImportantFile.FileCreateError.test", 1); |
| 349 | } |
| 350 | |
[email protected] | 4348625 | 2012-10-24 16:33:36 | [diff] [blame] | 351 | } // namespace base |