diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-17 22:10:34 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-17 22:10:34 +0000 |
commit | e3d66fde2328cc9220c25b51d06cc17b2f8522e7 (patch) | |
tree | c5da44e3c8bfcc898eb19e3176f57491fba16523 /net/base/file_stream_unittest.cc | |
parent | 17cb8a80ed6014b9071fb2a7daef0dc52bf8ab45 (diff) | |
download | chromium_src-e3d66fde2328cc9220c25b51d06cc17b2f8522e7.zip chromium_src-e3d66fde2328cc9220c25b51d06cc17b2f8522e7.tar.gz chromium_src-e3d66fde2328cc9220c25b51d06cc17b2f8522e7.tar.bz2 |
net: Add FileStream::Open() and Close() that perform asynchronously.
However, these aren't used yet. Clients of FileStream that reads/writes
files asynchronously should be migrated to use the new functions.
BUG=72001,114783
TEST=net_unittests.
Review URL: https://chromiumcodereview.appspot.com/9415031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@122601 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/file_stream_unittest.cc')
-rw-r--r-- | net/base/file_stream_unittest.cc | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index 537e6b1..91eb075 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -10,6 +10,9 @@ #include "base/message_loop.h" #include "base/path_service.h" #include "base/platform_file.h" +#include "base/synchronization/waitable_event.h" +#include "base/test/test_timeouts.h" +#include "net/base/capturing_net_log.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" @@ -30,6 +33,45 @@ IOBufferWithSize* CreateTestDataBuffer() { return buf; } +// This NetLog is used for notifying when a file stream is closed +// (i.e. TYPE_FILE_STREAM_CLOSE event is recorded). +class NetLogForNotifyingFileClosure : public NetLog { + public: + NetLogForNotifyingFileClosure() + : id_(0), + on_closure_(false /* manual_reset */, false /* initially_signaled */) { + } + + // Wait until a file closure event is recorded. + bool WaitForClosure() { + const base::TimeDelta timeout( + base::TimeDelta::FromMilliseconds( + TestTimeouts::action_max_timeout_ms())); + return on_closure_.TimedWait(timeout); + } + + // NetLog overrides: + virtual void AddEntry(EventType type, + const base::TimeTicks& time, + const Source& source, + EventPhase phase, + EventParameters* extra_parameters) { + if (type == TYPE_FILE_STREAM_CLOSE) { + on_closure_.Signal(); + } + } + + virtual uint32 NextID() { return id_++; } + virtual LogLevel GetLogLevel() const { return LOG_ALL; } + virtual void AddThreadSafeObserver(ThreadSafeObserver* observer) {} + virtual void RemoveThreadSafeObserver(ThreadSafeObserver* observer) {}; + + + private: + uint32 id_; + base::WaitableEvent on_closure_; +}; + } // namespace class FileStreamTest : public PlatformTest { @@ -972,6 +1014,59 @@ TEST_F(FileStreamTest, Truncate) { EXPECT_EQ("01230123", read_contents); } +TEST_F(FileStreamTest, AsyncBasicOpenClose) { + FileStream stream(NULL); + int flags = base::PLATFORM_FILE_OPEN | + base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_ASYNC; + TestCompletionCallback callback; + int rv = stream.Open(temp_file_path(), flags, callback.callback()); + EXPECT_EQ(ERR_IO_PENDING, rv); + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_TRUE(stream.IsOpen()); + + stream.Close(callback.callback()); + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_FALSE(stream.IsOpen()); +} + +TEST_F(FileStreamTest, SyncCloseTwice) { + FileStream stream(NULL); + int flags = base::PLATFORM_FILE_OPEN | + base::PLATFORM_FILE_READ; + int rv = stream.OpenSync(temp_file_path(), flags); + EXPECT_EQ(OK, rv); + EXPECT_TRUE(stream.IsOpen()); + + // Closing twice should be safe. + stream.CloseSync(); + EXPECT_FALSE(stream.IsOpen()); + + stream.CloseSync(); + EXPECT_FALSE(stream.IsOpen()); +} + +TEST_F(FileStreamTest, AsyncCloseTwice) { + FileStream stream(NULL); + int flags = base::PLATFORM_FILE_OPEN | + base::PLATFORM_FILE_READ | + base::PLATFORM_FILE_ASYNC; + TestCompletionCallback callback; + int rv = stream.Open(temp_file_path(), flags, callback.callback()); + EXPECT_EQ(ERR_IO_PENDING, rv); + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_TRUE(stream.IsOpen()); + + // Closing twice should be safe. + stream.Close(callback.callback()); + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_FALSE(stream.IsOpen()); + + stream.Close(callback.callback()); + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_FALSE(stream.IsOpen()); +} + } // namespace } // namespace net |