Index: net/base/file_stream_unittest.cc |
=================================================================== |
--- net/base/file_stream_unittest.cc (revision 145483) |
+++ net/base/file_stream_unittest.cc (working copy) |
@@ -322,45 +322,36 @@ |
} |
} |
-// Similar to AsyncRead_EarlyDelete but using a given file handler rather than |
-// calling FileStream::Open, to ensure that deleting a stream with in-flight |
-// operation without auto-closing feature is also ok. |
-TEST_F(FileStreamTest, AsyncRead_EarlyDelete_NoAutoClose) { |
+// Similar to AsyncRead_EarlyDelete but calls CloseAndCancelAsync() instead, |
+// to ensure that it works as expected while an async read is in flight. |
+TEST_F(FileStreamTest, AsyncRead_CloseAndCancelAsync) { |
int64 file_size; |
bool ok = file_util::GetFileSize(temp_file_path(), &file_size); |
EXPECT_TRUE(ok); |
- bool created = false; |
+ scoped_ptr<FileStream> stream(new FileStream(NULL)); |
int flags = base::PLATFORM_FILE_OPEN | |
base::PLATFORM_FILE_READ | |
base::PLATFORM_FILE_ASYNC; |
- base::PlatformFileError error_code = base::PLATFORM_FILE_ERROR_FAILED; |
- base::PlatformFile file = base::CreatePlatformFile( |
- temp_file_path(), flags, &created, &error_code); |
- EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
+ TestCompletionCallback callback; |
+ int rv = stream->Open(temp_file_path(), flags, callback.callback()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ EXPECT_EQ(OK, callback.WaitForResult()); |
- scoped_ptr<FileStream> stream(new FileStream(file, flags, NULL)); |
int64 total_bytes_avail = stream->Available(); |
EXPECT_EQ(file_size, total_bytes_avail); |
- TestCompletionCallback callback; |
scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); |
- int rv = stream->Read(buf, buf->size(), callback.callback()); |
- stream.reset(); // Delete instead of closing it. |
+ rv = stream->Read(buf, buf->size(), callback.callback()); |
+ stream->CloseAndCancelAsync(); |
if (rv < 0) { |
EXPECT_EQ(ERR_IO_PENDING, rv); |
- // The callback should not be called if the request is cancelled. |
+ // The callback should not be called if the request is canceled. |
MessageLoop::current()->RunAllPending(); |
EXPECT_FALSE(callback.have_result()); |
} else { |
EXPECT_EQ(std::string(kTestData, rv), std::string(buf->data(), rv)); |
} |
- |
- base::PlatformFileInfo info; |
- // The file should still be open. |
- EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); |
- // Clean up. |
- EXPECT_TRUE(base::ClosePlatformFile(file)); |
} |
TEST_F(FileStreamTest, BasicRead_FromOffset) { |
@@ -1219,6 +1210,7 @@ |
// Close the stream without waiting for the completion. Should be safe. |
stream.CloseSync(); |
// open_callback won't be called. |
+ MessageLoop::current()->RunAllPending(); |
EXPECT_FALSE(open_callback.have_result()); |
} |
@@ -1235,9 +1227,27 @@ |
// complete. Should be safe. |
stream.reset(); |
// open_callback won't be called. |
+ MessageLoop::current()->RunAllPending(); |
EXPECT_FALSE(open_callback.have_result()); |
} |
+TEST_F(FileStreamTest, AsyncOpenAndCancelAsync) { |
+ scoped_ptr<FileStream> stream(new FileStream(NULL)); |
+ int flags = base::PLATFORM_FILE_OPEN | |
+ base::PLATFORM_FILE_WRITE | |
+ base::PLATFORM_FILE_ASYNC; |
+ TestCompletionCallback open_callback; |
+ int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
+ EXPECT_EQ(ERR_IO_PENDING, rv); |
+ |
+ // Close and cancel without waiting for the open operation to be |
+ // complete. Should be safe. |
+ stream->CloseAndCancelAsync(); |
+ // open_callback won't be called. |
+ MessageLoop::current()->RunAllPending(); |
+ EXPECT_FALSE(open_callback.have_result()); |
+} |
+ |
TEST_F(FileStreamTest, AsyncCloseAndDelete) { |
scoped_ptr<FileStream> stream(new FileStream(NULL)); |
int flags = base::PLATFORM_FILE_OPEN | |
@@ -1256,6 +1266,7 @@ |
// complete. Should be safe. |
stream.reset(); |
// close_callback won't be called. |
+ MessageLoop::current()->RunAllPending(); |
EXPECT_FALSE(close_callback.have_result()); |
} |