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

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

Issue 250783002: net: Make sure the FileStream file is closed on the worker thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use an explicit worker pool Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « net/base/file_stream_context.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/files/file.h" 10 #include "base/files/file.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h" 12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "base/test/test_timeouts.h" 16 #include "base/test/test_timeouts.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/threading/thread_restrictions.h"
17 #include "net/base/capturing_net_log.h" 19 #include "net/base/capturing_net_log.h"
18 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
19 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
20 #include "net/base/test_completion_callback.h" 22 #include "net/base/test_completion_callback.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h" 24 #include "testing/platform_test.h"
23 25
24 #if defined(OS_ANDROID) 26 #if defined(OS_ANDROID)
25 #include "base/test/test_file_util.h" 27 #include "base/test/test_file_util.h"
26 #endif 28 #endif
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 EXPECT_LT(0, total_bytes_written); 752 EXPECT_LT(0, total_bytes_written);
751 EXPECT_EQ(kTestDataSize, total_bytes_written); 753 EXPECT_EQ(kTestDataSize, total_bytes_written);
752 754
753 stream.reset(); 755 stream.reset();
754 756
755 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); 757 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
756 EXPECT_EQ(kTestDataSize * 2, file_size); 758 EXPECT_EQ(kTestDataSize * 2, file_size);
757 } 759 }
758 760
759 TEST_F(FileStreamTest, AsyncOpenAndDelete) { 761 TEST_F(FileStreamTest, AsyncOpenAndDelete) {
760 scoped_ptr<FileStream> stream( 762 scoped_refptr<base::SequencedWorkerPool> pool(
761 new FileStream(base::MessageLoopProxy::current())); 763 new base::SequencedWorkerPool(1, "StreamTest"));
764
765 bool prev = base::ThreadRestrictions::SetIOAllowed(false);
766 scoped_ptr<FileStream> stream(new FileStream(pool.get()));
762 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | 767 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
763 base::File::FLAG_ASYNC; 768 base::File::FLAG_ASYNC;
764 TestCompletionCallback open_callback; 769 TestCompletionCallback open_callback;
765 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); 770 int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
766 EXPECT_EQ(ERR_IO_PENDING, rv); 771 EXPECT_EQ(ERR_IO_PENDING, rv);
767 772
768 // Delete the stream without waiting for the open operation to be 773 // Delete the stream without waiting for the open operation to be
769 // complete. Should be safe. 774 // complete. Should be safe.
770 stream.reset(); 775 stream.reset();
776
777 // Force an operation through the pool.
778 scoped_ptr<FileStream> stream2(new FileStream(pool.get()));
779 TestCompletionCallback open_callback2;
780 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback());
781 EXPECT_EQ(OK, open_callback2.GetResult(rv));
782 stream2.reset();
783
784 pool->Shutdown();
785
771 // open_callback won't be called. 786 // open_callback won't be called.
772 base::RunLoop().RunUntilIdle(); 787 base::RunLoop().RunUntilIdle();
773 EXPECT_FALSE(open_callback.have_result()); 788 EXPECT_FALSE(open_callback.have_result());
789 base::ThreadRestrictions::SetIOAllowed(prev);
774 } 790 }
775 791
776 // Verify that async Write() errors are mapped correctly. 792 // Verify that async Write() errors are mapped correctly.
777 TEST_F(FileStreamTest, AsyncWriteError) { 793 TEST_F(FileStreamTest, AsyncWriteError) {
778 // Try opening file as read-only and then writing to it using FileStream. 794 // Try opening file as read-only and then writing to it using FileStream.
779 uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ | 795 uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
780 base::File::FLAG_ASYNC; 796 base::File::FLAG_ASYNC;
781 797
782 base::File file(temp_file_path(), flags); 798 base::File file(temp_file_path(), flags);
783 ASSERT_TRUE(file.IsValid()); 799 ASSERT_TRUE(file.IsValid());
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 total_bytes_read += rv; 878 total_bytes_read += rv;
863 data_read.append(buf->data(), rv); 879 data_read.append(buf->data(), rv);
864 } 880 }
865 EXPECT_EQ(file_size, total_bytes_read); 881 EXPECT_EQ(file_size, total_bytes_read);
866 } 882 }
867 #endif 883 #endif
868 884
869 } // namespace 885 } // namespace
870 886
871 } // namespace net 887 } // namespace net
OLDNEW
« no previous file with comments | « net/base/file_stream_context.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698