Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: base/files/important_file_writer_unittest.cc

Issue 2299523003: Add synchronous callback support to important_file_writer.cc (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Try move logic from ImportantFileWriter to JsonPrefStore Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 28 matching lines...) Expand all
39 39
40 bool SerializeData(std::string* output) override { 40 bool SerializeData(std::string* output) override {
41 output->assign(data_); 41 output->assign(data_);
42 return true; 42 return true;
43 } 43 }
44 44
45 private: 45 private:
46 const std::string data_; 46 const std::string data_;
47 }; 47 };
48 48
49 class SuccessfulWriteObserver { 49 enum WriteCallbackObservationState {
50 NOT_CALLED,
51 CALLED_WITH_ERROR,
52 CALLED_WITH_SUCCESS,
53 };
54
55 class WriteCallbackObserver {
50 public: 56 public:
51 SuccessfulWriteObserver() : successful_write_observed_(false) {} 57 WriteCallbackObserver() : observation_state_(NOT_CALLED) {}
52 58
53 // Register on_successful_write() to be called on the next successful write 59 // Register OnWrite() to be called on the next write of |writer|.
54 // of |writer|. 60 void ObserveNextWriteCallback(ImportantFileWriter* writer);
55 void ObserveNextSuccessfulWrite(ImportantFileWriter* writer);
56 61
57 // Returns true if a successful write was observed via on_successful_write() 62 // Returns true if a write was observed via OnWrite()
58 // and resets the observation state to false regardless. 63 // and resets the observation state to false regardless.
59 bool GetAndResetObservationState(); 64 WriteCallbackObservationState GetAndResetObservationState();
60 65
61 private: 66 private:
62 void on_successful_write() { 67 void OnWrite(bool success) {
63 EXPECT_FALSE(successful_write_observed_); 68 EXPECT_EQ(NOT_CALLED, observation_state_);
64 successful_write_observed_ = true; 69 observation_state_ = success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR;
65 } 70 }
66 71
67 bool successful_write_observed_; 72 WriteCallbackObservationState observation_state_;
68 73
69 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteObserver); 74 DISALLOW_COPY_AND_ASSIGN(WriteCallbackObserver);
70 }; 75 };
71 76
72 void SuccessfulWriteObserver::ObserveNextSuccessfulWrite( 77 void WriteCallbackObserver::ObserveNextWriteCallback(
73 ImportantFileWriter* writer) { 78 ImportantFileWriter* writer) {
74 writer->RegisterOnNextSuccessfulWriteCallback(base::Bind( 79 writer->RegisterOnNextWriteCallback(
75 &SuccessfulWriteObserver::on_successful_write, base::Unretained(this))); 80 base::Bind(&WriteCallbackObserver::OnWrite, base::Unretained(this)));
76 } 81 }
77 82
78 bool SuccessfulWriteObserver::GetAndResetObservationState() { 83 WriteCallbackObservationState
79 bool was_successful_write_observed = successful_write_observed_; 84 WriteCallbackObserver::GetAndResetObservationState() {
80 successful_write_observed_ = false; 85 WriteCallbackObservationState state = observation_state_;
81 return was_successful_write_observed; 86 observation_state_ = NOT_CALLED;
87 return state;
82 } 88 }
83 89
84 } // namespace 90 } // namespace
85 91
86 class ImportantFileWriterTest : public testing::Test { 92 class ImportantFileWriterTest : public testing::Test {
87 public: 93 public:
88 ImportantFileWriterTest() { } 94 ImportantFileWriterTest() { }
89 void SetUp() override { 95 void SetUp() override {
90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 96 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
91 file_ = temp_dir_.GetPath().AppendASCII("test-file"); 97 file_ = temp_dir_.GetPath().AppendASCII("test-file");
92 } 98 }
93 99
94 protected: 100 protected:
95 SuccessfulWriteObserver successful_write_observer_; 101 WriteCallbackObserver write_callback_observer_;
96 FilePath file_; 102 FilePath file_;
97 MessageLoop loop_; 103 MessageLoop loop_;
98 104
99 private: 105 private:
100 ScopedTempDir temp_dir_; 106 ScopedTempDir temp_dir_;
101 }; 107 };
102 108
103 TEST_F(ImportantFileWriterTest, Basic) { 109 TEST_F(ImportantFileWriterTest, Basic) {
104 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); 110 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
105 EXPECT_FALSE(PathExists(writer.path())); 111 EXPECT_FALSE(PathExists(writer.path()));
106 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 112 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
107 writer.WriteNow(MakeUnique<std::string>("foo")); 113 writer.WriteNow(WrapUnique(new std::string("foo")));
108 RunLoop().RunUntilIdle(); 114 RunLoop().RunUntilIdle();
109 115
110 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 116 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
111 ASSERT_TRUE(PathExists(writer.path())); 117 ASSERT_TRUE(PathExists(writer.path()));
112 EXPECT_EQ("foo", GetFileContent(writer.path())); 118 EXPECT_EQ("foo", GetFileContent(writer.path()));
113 } 119 }
114 120
115 TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) { 121 TEST_F(ImportantFileWriterTest, WriteWithObserver) {
116 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get()); 122 ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
123
gab 2016/09/19 18:01:30 rm this empty line to be consistent with surroundi
proberge 2016/09/19 21:24:25 Done.
117 EXPECT_FALSE(PathExists(writer.path())); 124 EXPECT_FALSE(PathExists(writer.path()));
118 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 125 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
119 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); 126
120 writer.WriteNow(MakeUnique<std::string>("foo")); 127 // Confirm that the observer is invoked.
128 write_callback_observer_.ObserveNextWriteCallback(&writer);
129 writer.WriteNow(WrapUnique(new std::string("foo")));
dcheng 2016/09/16 22:42:05 Nit: MakeUnique<std::string> here and elsewhere.
proberge 2016/09/19 21:24:25 Done.
121 RunLoop().RunUntilIdle(); 130 RunLoop().RunUntilIdle();
122 131
123 // Confirm that the observer is invoked. 132 EXPECT_EQ(CALLED_WITH_SUCCESS,
124 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); 133 write_callback_observer_.GetAndResetObservationState());
125 ASSERT_TRUE(PathExists(writer.path())); 134 ASSERT_TRUE(PathExists(writer.path()));
126 EXPECT_EQ("foo", GetFileContent(writer.path())); 135 EXPECT_EQ("foo", GetFileContent(writer.path()));
127 136
128 // Confirm that re-installing the observer works for another write. 137 // Confirm that re-installing the observer works for another write.
129 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 138 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
130 successful_write_observer_.ObserveNextSuccessfulWrite(&writer); 139 write_callback_observer_.ObserveNextWriteCallback(&writer);
131 writer.WriteNow(MakeUnique<std::string>("bar")); 140 writer.WriteNow(WrapUnique(new std::string("bar")));
132 RunLoop().RunUntilIdle(); 141 RunLoop().RunUntilIdle();
133 142
134 EXPECT_TRUE(successful_write_observer_.GetAndResetObservationState()); 143 EXPECT_EQ(CALLED_WITH_SUCCESS,
144 write_callback_observer_.GetAndResetObservationState());
135 ASSERT_TRUE(PathExists(writer.path())); 145 ASSERT_TRUE(PathExists(writer.path()));
136 EXPECT_EQ("bar", GetFileContent(writer.path())); 146 EXPECT_EQ("bar", GetFileContent(writer.path()));
137 147
138 // Confirm that writing again without re-installing the observer doesn't 148 // Confirm that writing again without re-installing the observer doesn't
139 // result in a notification. 149 // result in a notification.
140 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 150 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
141 writer.WriteNow(MakeUnique<std::string>("baz")); 151 writer.WriteNow(WrapUnique(new std::string("baz")));
142 RunLoop().RunUntilIdle(); 152 RunLoop().RunUntilIdle();
143 153
144 EXPECT_FALSE(successful_write_observer_.GetAndResetObservationState()); 154 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
145 ASSERT_TRUE(PathExists(writer.path())); 155 ASSERT_TRUE(PathExists(writer.path()));
146 EXPECT_EQ("baz", GetFileContent(writer.path())); 156 EXPECT_EQ("baz", GetFileContent(writer.path()));
147 } 157 }
148 158
159 TEST_F(ImportantFileWriterTest, FailedWriteWithObserver) {
160 // Use an invalid file path (relative paths are invalid) to get a
161 // FILE_ERROR_ACCESS_DENIED error when trying to write the file.
162 ImportantFileWriter writer(FilePath().AppendASCII("bad/../path"),
163 ThreadTaskRunnerHandle::Get());
164 EXPECT_FALSE(PathExists(writer.path()));
165 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
166 write_callback_observer_.ObserveNextWriteCallback(&writer);
167 writer.WriteNow(WrapUnique(new std::string("foo")));
168 RunLoop().RunUntilIdle();
169
170 // Confirm that the write observer was invoked with its boolean parameter set
171 // to false.
172 EXPECT_EQ(CALLED_WITH_ERROR,
173 write_callback_observer_.GetAndResetObservationState());
174 EXPECT_FALSE(PathExists(writer.path()));
175 }
176
gab 2016/09/19 18:01:30 Re-add the test with the independent thread and co
proberge 2016/09/19 21:24:25 Done. Did not add a WaitableEvent::Wait task as I
149 TEST_F(ImportantFileWriterTest, ScheduleWrite) { 177 TEST_F(ImportantFileWriterTest, ScheduleWrite) {
150 ImportantFileWriter writer(file_, 178 ImportantFileWriter writer(file_,
151 ThreadTaskRunnerHandle::Get(), 179 ThreadTaskRunnerHandle::Get(),
152 TimeDelta::FromMilliseconds(25)); 180 TimeDelta::FromMilliseconds(25));
153 EXPECT_FALSE(writer.HasPendingWrite()); 181 EXPECT_FALSE(writer.HasPendingWrite());
154 DataSerializer serializer("foo"); 182 DataSerializer serializer("foo");
155 writer.ScheduleWrite(&serializer); 183 writer.ScheduleWrite(&serializer);
156 EXPECT_TRUE(writer.HasPendingWrite()); 184 EXPECT_TRUE(writer.HasPendingWrite());
157 ThreadTaskRunnerHandle::Get()->PostDelayedTask( 185 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
158 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), 186 FROM_HERE, MessageLoop::QuitWhenIdleClosure(),
(...skipping 30 matching lines...) Expand all
189 writer.ScheduleWrite(&baz); 217 writer.ScheduleWrite(&baz);
190 ThreadTaskRunnerHandle::Get()->PostDelayedTask( 218 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
191 FROM_HERE, MessageLoop::QuitWhenIdleClosure(), 219 FROM_HERE, MessageLoop::QuitWhenIdleClosure(),
192 TimeDelta::FromMilliseconds(100)); 220 TimeDelta::FromMilliseconds(100));
193 RunLoop().Run(); 221 RunLoop().Run();
194 ASSERT_TRUE(PathExists(writer.path())); 222 ASSERT_TRUE(PathExists(writer.path()));
195 EXPECT_EQ("baz", GetFileContent(writer.path())); 223 EXPECT_EQ("baz", GetFileContent(writer.path()));
196 } 224 }
197 225
198 } // namespace base 226 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698