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

Side by Side Diff: net/base/file_stream_unittest.cc

Issue 23500006: URLFetcher won't call delegate until file is closed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reorder two methods. Created 7 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 "net/base/file_stream.h" 5 #include "net/base/file_stream.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 EXPECT_EQ(OK, rv); 71 EXPECT_EQ(OK, rv);
72 EXPECT_TRUE(stream.IsOpen()); 72 EXPECT_TRUE(stream.IsOpen());
73 file = stream.GetPlatformFileForTesting(); 73 file = stream.GetPlatformFileForTesting();
74 } 74 }
75 EXPECT_NE(base::kInvalidPlatformFileValue, file); 75 EXPECT_NE(base::kInvalidPlatformFileValue, file);
76 base::PlatformFileInfo info; 76 base::PlatformFileInfo info;
77 // The file should be closed. 77 // The file should be closed.
78 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info)); 78 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
79 } 79 }
80 80
81 TEST_F(FileStreamTest, BasicOpenExplicitClose) {
82 base::PlatformFile file = base::kInvalidPlatformFileValue;
83 FileStream stream(NULL);
84 int rv = stream.OpenSync(temp_file_path(),
85 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
86 EXPECT_EQ(OK, rv);
87 EXPECT_TRUE(stream.IsOpen());
88 file = stream.GetPlatformFileForTesting();
89 EXPECT_NE(base::kInvalidPlatformFileValue, file);
90 EXPECT_EQ(OK, stream.CloseSync());
91 EXPECT_FALSE(stream.IsOpen());
92 base::PlatformFileInfo info;
93 // The file should be closed.
94 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
95 }
96
97 TEST_F(FileStreamTest, AsyncOpenExplicitClose) {
98 base::PlatformFile file = base::kInvalidPlatformFileValue;
99 TestCompletionCallback callback;
100 {
101 FileStream stream(NULL);
mmenke 2013/09/10 18:32:52 nit: I think it's generally a little clearer to u
waffles 2013/09/10 21:41:34 I agree that scoped_ptr is better. I had noticed t
102 int flags = base::PLATFORM_FILE_OPEN |
103 base::PLATFORM_FILE_READ |
104 base::PLATFORM_FILE_ASYNC;
105 int rv = stream.Open(temp_file_path(), flags, callback.callback());
106 EXPECT_EQ(ERR_IO_PENDING, rv);
107 EXPECT_EQ(OK, callback.WaitForResult());
108 EXPECT_TRUE(stream.IsOpen());
109 file = stream.GetPlatformFileForTesting();
110 EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback()));
111 EXPECT_EQ(OK, callback.WaitForResult());
112 EXPECT_FALSE(stream.IsOpen());
113 }
114 base::PlatformFileInfo info;
115 // The file should be closed.
116 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
117 }
118
119 TEST_F(FileStreamTest, AsyncOpenExplicitCloseOrphaned) {
120 base::PlatformFile file = base::kInvalidPlatformFileValue;
121 TestCompletionCallback callback;
122 base::PlatformFileInfo info;
123 {
124 FileStream stream(NULL, base::MessageLoopProxy::current());
125 int flags = base::PLATFORM_FILE_OPEN |
126 base::PLATFORM_FILE_READ |
127 base::PLATFORM_FILE_ASYNC;
128 int rv = stream.Open(temp_file_path(), flags, callback.callback());
129 EXPECT_EQ(ERR_IO_PENDING, rv);
130 EXPECT_EQ(OK, callback.WaitForResult());
131 EXPECT_TRUE(stream.IsOpen());
132 file = stream.GetPlatformFileForTesting();
133 EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback()));
134 }
135 // File isn't actually closed yet.
136 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
137 base::RunLoop runloop;
138 runloop.RunUntilIdle();
139 // The file should now be closed, though the callback has not been called.
140 EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
141 }
142
81 TEST_F(FileStreamTest, FileHandleNotLeftOpen) { 143 TEST_F(FileStreamTest, FileHandleNotLeftOpen) {
82 bool created = false; 144 bool created = false;
83 ASSERT_EQ(kTestDataSize, 145 ASSERT_EQ(kTestDataSize,
84 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize)); 146 file_util::WriteFile(temp_file_path(), kTestData, kTestDataSize));
85 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ; 147 int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ;
86 base::PlatformFile file = base::CreatePlatformFile( 148 base::PlatformFile file = base::CreatePlatformFile(
87 temp_file_path(), flags, &created, NULL); 149 temp_file_path(), flags, &created, NULL);
88 150
89 { 151 {
90 // Seek to the beginning of the file and read. 152 // Seek to the beginning of the file and read.
(...skipping 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after
1095 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL); 1157 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(NULL);
1096 rv = stream->Read(buf.get(), 1, callback.callback()); 1158 rv = stream->Read(buf.get(), 1, callback.callback());
1097 if (rv == ERR_IO_PENDING) 1159 if (rv == ERR_IO_PENDING)
1098 rv = callback.WaitForResult(); 1160 rv = callback.WaitForResult();
1099 EXPECT_LT(rv, 0); 1161 EXPECT_LT(rv, 0);
1100 } 1162 }
1101 1163
1102 } // namespace 1164 } // namespace
1103 1165
1104 } // namespace net 1166 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698