Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / base / files / important_file_writer_unittest.cc
bloba284d38709ea79c09dd96949269f280d950a3254
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
3 // found in the LICENSE file.
5 #include "base/files/important_file_writer.h"
7 #include "base/compiler_specific.h"
8 #include "base/file_path.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "base/scoped_temp_dir.h"
13 #include "base/threading/thread.h"
14 #include "base/time.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace base {
19 namespace {
21 std::string GetFileContent(const FilePath& path) {
22 std::string content;
23 if (!file_util::ReadFileToString(path, &content)) {
24 NOTREACHED();
26 return content;
29 class DataSerializer : public ImportantFileWriter::DataSerializer {
30 public:
31 explicit DataSerializer(const std::string& data) : data_(data) {
34 virtual bool SerializeData(std::string* output) {
35 output->assign(data_);
36 return true;
39 private:
40 const std::string data_;
43 } // namespace
45 class ImportantFileWriterTest : public testing::Test {
46 public:
47 ImportantFileWriterTest() { }
48 virtual void SetUp() {
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
50 file_ = temp_dir_.path().AppendASCII("test-file");
53 protected:
54 FilePath file_;
55 MessageLoop loop_;
57 private:
58 ScopedTempDir temp_dir_;
61 TEST_F(ImportantFileWriterTest, Basic) {
62 ImportantFileWriter writer(file_,
63 MessageLoopProxy::current());
64 EXPECT_FALSE(file_util::PathExists(writer.path()));
65 writer.WriteNow("foo");
66 loop_.RunUntilIdle();
68 ASSERT_TRUE(file_util::PathExists(writer.path()));
69 EXPECT_EQ("foo", GetFileContent(writer.path()));
72 TEST_F(ImportantFileWriterTest, ScheduleWrite) {
73 ImportantFileWriter writer(file_,
74 MessageLoopProxy::current());
75 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
76 EXPECT_FALSE(writer.HasPendingWrite());
77 DataSerializer serializer("foo");
78 writer.ScheduleWrite(&serializer);
79 EXPECT_TRUE(writer.HasPendingWrite());
80 MessageLoop::current()->PostDelayedTask(
81 FROM_HERE,
82 MessageLoop::QuitClosure(),
83 TimeDelta::FromMilliseconds(100));
84 MessageLoop::current()->Run();
85 EXPECT_FALSE(writer.HasPendingWrite());
86 ASSERT_TRUE(file_util::PathExists(writer.path()));
87 EXPECT_EQ("foo", GetFileContent(writer.path()));
90 TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
91 ImportantFileWriter writer(file_,
92 MessageLoopProxy::current());
93 EXPECT_FALSE(writer.HasPendingWrite());
94 DataSerializer serializer("foo");
95 writer.ScheduleWrite(&serializer);
96 EXPECT_TRUE(writer.HasPendingWrite());
97 writer.DoScheduledWrite();
98 MessageLoop::current()->PostDelayedTask(
99 FROM_HERE,
100 MessageLoop::QuitClosure(),
101 TimeDelta::FromMilliseconds(100));
102 MessageLoop::current()->Run();
103 EXPECT_FALSE(writer.HasPendingWrite());
104 ASSERT_TRUE(file_util::PathExists(writer.path()));
105 EXPECT_EQ("foo", GetFileContent(writer.path()));
108 TEST_F(ImportantFileWriterTest, BatchingWrites) {
109 ImportantFileWriter writer(file_,
110 MessageLoopProxy::current());
111 writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
112 DataSerializer foo("foo"), bar("bar"), baz("baz");
113 writer.ScheduleWrite(&foo);
114 writer.ScheduleWrite(&bar);
115 writer.ScheduleWrite(&baz);
116 MessageLoop::current()->PostDelayedTask(
117 FROM_HERE,
118 MessageLoop::QuitClosure(),
119 TimeDelta::FromMilliseconds(100));
120 MessageLoop::current()->Run();
121 ASSERT_TRUE(file_util::PathExists(writer.path()));
122 EXPECT_EQ("baz", GetFileContent(writer.path()));
125 } // namespace base