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