1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
base / files / important_file_writer_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/important_file_writer.h"
#include <optional>
#include <utility>
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "base/timer/mock_timer.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
std::string GetFileContent(const FilePath& path) {
std::string content;
if (!ReadFileToString(path, &content)) {
NOTREACHED();
}
return content;
}
class DataSerializer : public ImportantFileWriter::DataSerializer {
public:
explicit DataSerializer(const std::string& data) : data_(data) {
}
std::optional<std::string> SerializeData() override {
EXPECT_TRUE(sequence_checker_.CalledOnValidSequence());
return data_;
}
private:
const base::SequenceChecker sequence_checker_;
const std::string data_;
};
class FailingDataSerializer : public ImportantFileWriter::DataSerializer {
public:
std::optional<std::string> SerializeData() override { return std::nullopt; }
};
class BackgroundDataSerializer
: public ImportantFileWriter::BackgroundDataSerializer {
public:
explicit BackgroundDataSerializer(
ImportantFileWriter::BackgroundDataProducerCallback
data_producer_callback)
: data_producer_callback_(std::move(data_producer_callback)) {
DCHECK(data_producer_callback_);
}
ImportantFileWriter::BackgroundDataProducerCallback
GetSerializedDataProducerForBackgroundSequence() override {
EXPECT_TRUE(sequence_checker_.CalledOnValidSequence());
return std::move(data_producer_callback_);
}
bool producer_callback_obtained() const {
return data_producer_callback_.is_null();
}
private:
const base::SequenceChecker sequence_checker_;
ImportantFileWriter::BackgroundDataProducerCallback data_producer_callback_;
};
enum WriteCallbackObservationState {
NOT_CALLED,
CALLED_WITH_ERROR,
CALLED_WITH_SUCCESS,
};
class WriteCallbacksObserver {
public:
WriteCallbacksObserver() = default;
WriteCallbacksObserver(const WriteCallbacksObserver&) = delete;
WriteCallbacksObserver& operator=(const WriteCallbacksObserver&) = delete;
// Register OnBeforeWrite() and OnAfterWrite() to be called on the next write
// of |writer|. `after_write_closure` will also be invoked from
// OnAfterWrite().
void ObserveNextWriteCallbacks(
ImportantFileWriter* writer,
base::OnceClosure after_write_closure = base::DoNothing());
// Returns the |WriteCallbackObservationState| which was observed, then resets
// it to |NOT_CALLED|.
WriteCallbackObservationState GetAndResetObservationState();
private:
void OnBeforeWrite() {
EXPECT_FALSE(before_write_called_);
before_write_called_ = true;
}
void OnAfterWrite(bool success) {
EXPECT_EQ(NOT_CALLED, after_write_observation_state_);
after_write_observation_state_ =
success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR;
std::move(after_write_closure_).Run();
}
bool before_write_called_ = false;
WriteCallbackObservationState after_write_observation_state_ = NOT_CALLED;
base::OnceClosure after_write_closure_ = base::DoNothing();
};
void WriteCallbacksObserver::ObserveNextWriteCallbacks(
ImportantFileWriter* writer,
base::OnceClosure after_write_closure) {
after_write_closure_ = std::move(after_write_closure);
writer->RegisterOnNextWriteCallbacks(
base::BindOnce(&WriteCallbacksObserver::OnBeforeWrite,
base::Unretained(this)),
base::BindOnce(&WriteCallbacksObserver::OnAfterWrite,
base::Unretained(this)));
}
WriteCallbackObservationState
WriteCallbacksObserver::GetAndResetObservationState() {
EXPECT_EQ(after_write_observation_state_ != NOT_CALLED, before_write_called_)
<< "The before-write callback should always be called before the "
"after-write callback";
WriteCallbackObservationState state = after_write_observation_state_;
before_write_called_ = false;
after_write_observation_state_ = NOT_CALLED;
return state;
}
} // namespace
class ImportantFileWriterTest : public testing::Test {
public:
ImportantFileWriterTest() = default;
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
file_ = temp_dir_.GetPath().AppendASCII("test-file");
}
protected:
WriteCallbacksObserver write_callback_observer_;
FilePath file_;
test::TaskEnvironment task_environment_;
private:
ScopedTempDir temp_dir_;
};
TEST_F(ImportantFileWriterTest, Basic) {
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
EXPECT_FALSE(PathExists(writer.path()));
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
writer.WriteNow("foo");
RunLoop().RunUntilIdle();
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, WriteWithObserver) {
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
EXPECT_FALSE(PathExists(writer.path()));
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
// Confirm that the observer is invoked.
write_callback_observer_.ObserveNextWriteCallbacks(&writer);
writer.WriteNow("foo");
RunLoop().RunUntilIdle();
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetObservationState());
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
// Confirm that re-installing the observer works for another write.
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
write_callback_observer_.ObserveNextWriteCallbacks(&writer);
writer.WriteNow("bar");
RunLoop().RunUntilIdle();
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetObservationState());
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("bar", GetFileContent(writer.path()));
// Confirm that writing again without re-installing the observer doesn't
// result in a notification.
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
writer.WriteNow("baz");
RunLoop().RunUntilIdle();
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("baz", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, FailedWriteWithObserver) {
// Use an invalid file path (relative paths are invalid) to get a
// FILE_ERROR_ACCESS_DENIED error when trying to write the file.
ImportantFileWriter writer(FilePath().AppendASCII("bad/../path"),
SingleThreadTaskRunner::GetCurrentDefault());
EXPECT_FALSE(PathExists(writer.path()));
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
write_callback_observer_.ObserveNextWriteCallbacks(&writer);
writer.WriteNow("foo");
RunLoop().RunUntilIdle();
// Confirm that the write observer was invoked with its boolean parameter set
// to false.
EXPECT_EQ(CALLED_WITH_ERROR,
write_callback_observer_.GetAndResetObservationState());
EXPECT_FALSE(PathExists(writer.path()));
}
TEST_F(ImportantFileWriterTest, CallbackRunsOnWriterThread) {
base::Thread file_writer_thread("ImportantFileWriter test thread");
file_writer_thread.Start();
ImportantFileWriter writer(file_, file_writer_thread.task_runner());
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
// Block execution on |file_writer_thread| to verify that callbacks are
// executed on it.
base::WaitableEvent wait_helper(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED);
file_writer_thread.task_runner()->PostTask(
FROM_HERE, base::BindOnce(&base::WaitableEvent::Wait,
base::Unretained(&wait_helper)));
write_callback_observer_.ObserveNextWriteCallbacks(&writer);
writer.WriteNow("foo");
RunLoop().RunUntilIdle();
// Expect the callback to not have been executed before the
// |file_writer_thread| is unblocked.
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
wait_helper.Signal();
file_writer_thread.FlushForTesting();
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetObservationState());
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, ScheduleWrite) {
constexpr TimeDelta kCommitInterval = Seconds(12345);
MockOneShotTimer timer;
ImportantFileWriter writer(file_, SingleThreadTaskRunner::GetCurrentDefault(),
kCommitInterval);
EXPECT_EQ(0u, writer.previous_data_size());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
ASSERT_TRUE(timer.IsRunning());
EXPECT_EQ(kCommitInterval, timer.GetCurrentDelay());
timer.Fire();
EXPECT_FALSE(writer.HasPendingWrite());
EXPECT_FALSE(timer.IsRunning());
RunLoop().RunUntilIdle();
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
EXPECT_EQ(3u, writer.previous_data_size());
}
TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
MockOneShotTimer timer;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
writer.DoScheduledWrite();
EXPECT_FALSE(writer.HasPendingWrite());
RunLoop().RunUntilIdle();
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, BatchingWrites) {
MockOneShotTimer timer;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
writer.SetTimerForTesting(&timer);
DataSerializer foo("foo"), bar("bar"), baz("baz");
writer.ScheduleWrite(&foo);
writer.ScheduleWrite(&bar);
writer.ScheduleWrite(&baz);
ASSERT_TRUE(timer.IsRunning());
timer.Fire();
RunLoop().RunUntilIdle();
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("baz", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, ScheduleWrite_FailToSerialize) {
MockOneShotTimer timer;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
FailingDataSerializer serializer;
writer.ScheduleWrite(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
ASSERT_TRUE(timer.IsRunning());
timer.Fire();
EXPECT_FALSE(writer.HasPendingWrite());
RunLoop().RunUntilIdle();
EXPECT_FALSE(PathExists(writer.path()));
}
TEST_F(ImportantFileWriterTest, ScheduleWrite_WriteNow) {
MockOneShotTimer timer;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
writer.WriteNow("bar");
EXPECT_FALSE(writer.HasPendingWrite());
EXPECT_FALSE(timer.IsRunning());
RunLoop().RunUntilIdle();
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("bar", GetFileContent(writer.path()));
}
TEST_F(ImportantFileWriterTest, DoScheduledWrite_FailToSerialize) {
base::HistogramTester histogram_tester;
MockOneShotTimer timer;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
FailingDataSerializer serializer;
writer.ScheduleWrite(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
writer.DoScheduledWrite();
EXPECT_FALSE(timer.IsRunning());
EXPECT_FALSE(writer.HasPendingWrite());
RunLoop().RunUntilIdle();
EXPECT_FALSE(PathExists(writer.path()));
// We don't record metrics in case the serialization fails.
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration", 0);
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.All",
0);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration", 0);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.All", 0);
}
TEST_F(ImportantFileWriterTest, ScheduleWriteWithBackgroundDataSerializer) {
base::HistogramTester histogram_tester;
base::Thread file_writer_thread("ImportantFileWriter test thread");
file_writer_thread.Start();
constexpr TimeDelta kCommitInterval = Seconds(12345);
MockOneShotTimer timer;
ImportantFileWriter writer(file_, file_writer_thread.task_runner(),
kCommitInterval);
EXPECT_EQ(0u, writer.previous_data_size());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
ASSERT_FALSE(file_writer_thread.task_runner()->RunsTasksInCurrentSequence());
BackgroundDataSerializer serializer(
base::BindLambdaForTesting([&]() -> std::optional<std::string> {
EXPECT_TRUE(
file_writer_thread.task_runner()->RunsTasksInCurrentSequence());
return "foo";
}));
writer.ScheduleWriteWithBackgroundDataSerializer(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
EXPECT_FALSE(serializer.producer_callback_obtained());
ASSERT_TRUE(timer.IsRunning());
EXPECT_EQ(kCommitInterval, timer.GetCurrentDelay());
timer.Fire();
EXPECT_FALSE(writer.HasPendingWrite());
EXPECT_TRUE(serializer.producer_callback_obtained());
EXPECT_FALSE(timer.IsRunning());
file_writer_thread.FlushForTesting();
ASSERT_TRUE(PathExists(writer.path()));
EXPECT_EQ("foo", GetFileContent(writer.path()));
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration", 1);
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.All",
1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration", 1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.All", 1);
}
TEST_F(ImportantFileWriterTest,
ScheduleWriteWithBackgroundDataSerializer_FailToSerialize) {
base::HistogramTester histogram_tester;
base::Thread file_writer_thread("ImportantFileWriter test thread");
file_writer_thread.Start();
constexpr TimeDelta kCommitInterval = Seconds(12345);
MockOneShotTimer timer;
ImportantFileWriter writer(file_, file_writer_thread.task_runner(),
kCommitInterval);
EXPECT_EQ(0u, writer.previous_data_size());
writer.SetTimerForTesting(&timer);
EXPECT_FALSE(writer.HasPendingWrite());
ASSERT_FALSE(file_writer_thread.task_runner()->RunsTasksInCurrentSequence());
BackgroundDataSerializer serializer(
base::BindLambdaForTesting([&]() -> std::optional<std::string> {
EXPECT_TRUE(
file_writer_thread.task_runner()->RunsTasksInCurrentSequence());
return std::nullopt;
}));
writer.ScheduleWriteWithBackgroundDataSerializer(&serializer);
EXPECT_TRUE(writer.HasPendingWrite());
EXPECT_FALSE(serializer.producer_callback_obtained());
EXPECT_TRUE(timer.IsRunning());
timer.Fire();
EXPECT_FALSE(timer.IsRunning());
EXPECT_TRUE(serializer.producer_callback_obtained());
EXPECT_FALSE(writer.HasPendingWrite());
file_writer_thread.FlushForTesting();
EXPECT_FALSE(PathExists(writer.path()));
// We record the foreground serialization metric despite later failure in
// background sequence.
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration", 1);
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.All",
1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration", 0);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.All", 0);
}
// Test that the chunking to avoid very large writes works.
TEST_F(ImportantFileWriterTest, WriteLargeFile) {
// One byte larger than kMaxWriteAmount.
const std::string large_data(8 * 1024 * 1024 + 1, 'g');
EXPECT_FALSE(PathExists(file_));
EXPECT_TRUE(ImportantFileWriter::WriteFileAtomically(file_, large_data));
std::string actual;
EXPECT_TRUE(ReadFileToString(file_, &actual));
EXPECT_EQ(large_data, actual);
}
// Verify that a UMA metric for the serialization duration is recorded.
TEST_F(ImportantFileWriterTest, SerializationDuration) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
writer.DoScheduledWrite();
RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration", 1);
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.All",
1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration", 1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.All", 1);
}
// Verify that a UMA metric for the serialization duration is recorded if the
// ImportantFileWriter has a custom histogram suffix.
TEST_F(ImportantFileWriterTest, SerializationDurationWithCustomSuffix) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_, SingleThreadTaskRunner::GetCurrentDefault(),
"Foo");
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
writer.DoScheduledWrite();
RunLoop().RunUntilIdle();
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.Foo",
1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.Foo", 1);
// Should not be written to the unsuffixed ("unknown") histogram.
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration", 0);
histogram_tester.ExpectTotalCount("ImportantFile.SerializationDuration.All",
1);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration", 0);
histogram_tester.ExpectTotalCount("ImportantFile.WriteDuration.All", 1);
}
#if BUILDFLAG(IS_WIN)
// Tests that failures of ReplaceFile are handled. These don't call the OS
// ReplaceFile because they count the exact number of calls, which could be
// flaky if the test runs on a machine with file scanners.
TEST_F(ImportantFileWriterTest, ReplaceFileSuccess) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
// Unconditional success in ReplaceFile.
writer.SetReplaceFileCallbackForTesting(base::BindRepeating(
[](const FilePath&, const FilePath&, File::Error* error) {
*error = File::FILE_OK;
return true;
}));
DataSerializer serializer("foo");
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
base::RunLoop run_loop;
write_callback_observer_.ObserveNextWriteCallbacks(&writer,
run_loop.QuitClosure());
writer.WriteNow("foo");
run_loop.Run();
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetObservationState());
// 0 means no retries were needed.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount", 0,
1);
// FileReplaceRetryCount2 is only recorded if retries were needed.
histogram_tester.ExpectTotalCount("ImportantFile.FileReplaceRetryCount2", 0);
histogram_tester.ExpectTotalCount("ImportantFile.FileReplaceRetryCount2.All",
0);
// 0 means no retries were needed.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult", 0, 1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult.All", 0,
1);
}
TEST_F(ImportantFileWriterTest, ReplaceFileRetry) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
// Fake a failure on the first two calls to ReplaceFile.
size_t retry_count = 0;
writer.SetReplaceFileCallbackForTesting(base::BindLambdaForTesting(
[&retry_count](const FilePath&, const FilePath&, File::Error* error) {
if (retry_count < 2) {
retry_count += 1;
*error = File::FILE_ERROR_IN_USE;
return false;
}
*error = File::FILE_OK;
return true;
}));
DataSerializer serializer("foo");
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
base::RunLoop run_loop;
write_callback_observer_.ObserveNextWriteCallbacks(&writer,
run_loop.QuitClosure());
writer.WriteNow("foo");
run_loop.Run();
EXPECT_EQ(CALLED_WITH_SUCCESS,
write_callback_observer_.GetAndResetObservationState());
EXPECT_EQ(retry_count, 2u);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount", 2,
1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount2", 2,
1);
histogram_tester.ExpectUniqueSample(
"ImportantFile.FileReplaceRetryCount2.All", 2, 1);
// 1 means succeeded with retries.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult", 1, 1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult.All", 1,
1);
}
TEST_F(ImportantFileWriterTest, ReplaceFileFails) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_,
SingleThreadTaskRunner::GetCurrentDefault());
// Unconditional failure in ReplaceFile.
writer.SetReplaceFileCallbackForTesting(base::BindRepeating(
[](const FilePath&, const FilePath&, File::Error* error) {
*error = File::FILE_ERROR_IN_USE;
return false;
}));
DataSerializer serializer("foo");
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
base::RunLoop run_loop;
write_callback_observer_.ObserveNextWriteCallbacks(&writer,
run_loop.QuitClosure());
writer.WriteNow("foo");
run_loop.Run();
EXPECT_EQ(CALLED_WITH_ERROR,
write_callback_observer_.GetAndResetObservationState());
// 10 means ReplaceFile never succeeded.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount", 10,
1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount2", 5,
1);
histogram_tester.ExpectUniqueSample(
"ImportantFile.FileReplaceRetryCount2.All", 5, 1);
// 2 means ReplaceFile never succeeded.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult", 2, 1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult.All", 2,
1);
}
TEST_F(ImportantFileWriterTest, ReplaceFileFailsWithSuffix) {
base::HistogramTester histogram_tester;
ImportantFileWriter writer(file_, SingleThreadTaskRunner::GetCurrentDefault(),
"Foo");
// Unconditional failure in ReplaceFile.
writer.SetReplaceFileCallbackForTesting(base::BindRepeating(
[](const FilePath&, const FilePath&, File::Error* error) {
*error = File::FILE_ERROR_IN_USE;
return false;
}));
DataSerializer serializer("foo");
EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState());
base::RunLoop run_loop;
write_callback_observer_.ObserveNextWriteCallbacks(&writer,
run_loop.QuitClosure());
writer.WriteNow("foo");
run_loop.Run();
EXPECT_EQ(CALLED_WITH_ERROR,
write_callback_observer_.GetAndResetObservationState());
// 10 means ReplaceFile never succeeded.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceRetryCount", 10,
1);
histogram_tester.ExpectUniqueSample(
"ImportantFile.FileReplaceRetryCount2.Foo", 5, 1);
histogram_tester.ExpectUniqueSample(
"ImportantFile.FileReplaceRetryCount2.All", 5, 1);
// 2 means ReplaceFile never succeeded.
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult.Foo", 2,
1);
histogram_tester.ExpectUniqueSample("ImportantFile.FileReplaceResult.All", 2,
1);
}
#endif
} // namespace base