OLD | NEW |
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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "base/test/sequenced_worker_pool_owner.h" | |
20 #include "base/test/test_timeouts.h" | 19 #include "base/test/test_timeouts.h" |
| 20 #include "base/threading/thread.h" |
21 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
25 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
26 #include "net/log/test_net_log.h" | 26 #include "net/log/test_net_log.h" |
27 #include "net/test/gtest_util.h" | 27 #include "net/test/gtest_util.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "testing/platform_test.h" | 30 #include "testing/platform_test.h" |
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 EXPECT_LT(0, total_bytes_written); | 720 EXPECT_LT(0, total_bytes_written); |
721 EXPECT_EQ(kTestDataSize, total_bytes_written); | 721 EXPECT_EQ(kTestDataSize, total_bytes_written); |
722 | 722 |
723 stream.reset(); | 723 stream.reset(); |
724 | 724 |
725 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); | 725 EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); |
726 EXPECT_EQ(kTestDataSize * 2, file_size); | 726 EXPECT_EQ(kTestDataSize * 2, file_size); |
727 } | 727 } |
728 | 728 |
729 TEST_F(FileStreamTest, OpenAndDelete) { | 729 TEST_F(FileStreamTest, OpenAndDelete) { |
730 base::SequencedWorkerPoolOwner pool_owner(1, "StreamTest"); | 730 base::Thread worker_thread("StreamTest"); |
| 731 ASSERT_TRUE(worker_thread.Start()); |
731 | 732 |
732 bool prev = base::ThreadRestrictions::SetIOAllowed(false); | 733 bool prev = base::ThreadRestrictions::SetIOAllowed(false); |
733 std::unique_ptr<FileStream> stream(new FileStream(pool_owner.pool())); | 734 std::unique_ptr<FileStream> stream( |
| 735 new FileStream(worker_thread.task_runner())); |
734 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | | 736 int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE | |
735 base::File::FLAG_ASYNC; | 737 base::File::FLAG_ASYNC; |
736 TestCompletionCallback open_callback; | 738 TestCompletionCallback open_callback; |
737 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); | 739 int rv = stream->Open(temp_file_path(), flags, open_callback.callback()); |
738 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); | 740 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
739 | 741 |
740 // Delete the stream without waiting for the open operation to be | 742 // Delete the stream without waiting for the open operation to be |
741 // complete. Should be safe. | 743 // complete. Should be safe. |
742 stream.reset(); | 744 stream.reset(); |
743 | 745 |
744 // Force an operation through the pool. | 746 // Force an operation through the worker. |
745 std::unique_ptr<FileStream> stream2(new FileStream(pool_owner.pool())); | 747 std::unique_ptr<FileStream> stream2( |
| 748 new FileStream(worker_thread.task_runner())); |
746 TestCompletionCallback open_callback2; | 749 TestCompletionCallback open_callback2; |
747 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); | 750 rv = stream2->Open(temp_file_path(), flags, open_callback2.callback()); |
748 EXPECT_THAT(open_callback2.GetResult(rv), IsOk()); | 751 EXPECT_THAT(open_callback2.GetResult(rv), IsOk()); |
749 stream2.reset(); | 752 stream2.reset(); |
750 | 753 |
751 // open_callback won't be called. | 754 // open_callback won't be called. |
752 base::RunLoop().RunUntilIdle(); | 755 base::RunLoop().RunUntilIdle(); |
753 EXPECT_FALSE(open_callback.have_result()); | 756 EXPECT_FALSE(open_callback.have_result()); |
754 base::ThreadRestrictions::SetIOAllowed(prev); | 757 base::ThreadRestrictions::SetIOAllowed(prev); |
755 } | 758 } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 total_bytes_read += rv; | 846 total_bytes_read += rv; |
844 data_read.append(buf->data(), rv); | 847 data_read.append(buf->data(), rv); |
845 } | 848 } |
846 EXPECT_EQ(file_size, total_bytes_read); | 849 EXPECT_EQ(file_size, total_bytes_read); |
847 } | 850 } |
848 #endif | 851 #endif |
849 | 852 |
850 } // namespace | 853 } // namespace |
851 | 854 |
852 } // namespace net | 855 } // namespace net |
OLD | NEW |