OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/common/data_pipe_utils.h" | 5 #include "mojo/common/data_pipe_utils.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/synchronization/condition_variable.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "base/time/time.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
8 | 15 |
9 namespace mojo { | 16 namespace mojo { |
10 namespace common { | 17 namespace common { |
| 18 |
11 namespace { | 19 namespace { |
12 | 20 |
| 21 void TransferBooleanValueAndExecute(const base::Closure& closure, |
| 22 bool* to_write, |
| 23 bool value) { |
| 24 *to_write = value; |
| 25 if (!closure.is_null()) { |
| 26 closure.Run(); |
| 27 } |
| 28 } |
| 29 |
| 30 TEST(DataPipeUtilsTest, AsyncFileTransfer) { |
| 31 const char kData[] = "Hello world."; |
| 32 base::ScopedTempDir temp_dir; |
| 33 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 34 base::FilePath input; |
| 35 base::FilePath output; |
| 36 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &input)); |
| 37 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &output)); |
| 38 ASSERT_TRUE(base::WriteFile(input, kData, arraysize(kData))); |
| 39 base::MessageLoop loop; |
| 40 scoped_refptr<base::SequencedWorkerPool> blocking_pool = |
| 41 new base::SequencedWorkerPool(2, "blocking_pool"); |
| 42 |
| 43 bool write_succeded = false; |
| 44 bool read_succeded = false; |
| 45 |
| 46 DataPipe pipes; |
| 47 CopyFromFile(input, |
| 48 pipes.producer_handle.Pass(), |
| 49 0, |
| 50 blocking_pool.get(), |
| 51 base::Bind(&TransferBooleanValueAndExecute, |
| 52 base::Closure(), |
| 53 base::Unretained(&write_succeded))); |
| 54 CopyToFile(pipes.consumer_handle.Pass(), |
| 55 output, |
| 56 blocking_pool.get(), |
| 57 base::Bind(&TransferBooleanValueAndExecute, |
| 58 base::MessageLoop::QuitClosure(), |
| 59 base::Unretained(&read_succeded))); |
| 60 loop.Run(); |
| 61 |
| 62 EXPECT_TRUE(write_succeded); |
| 63 EXPECT_TRUE(read_succeded); |
| 64 EXPECT_TRUE(base::ContentsEqual(input, output)); |
| 65 |
| 66 blocking_pool->Shutdown(); |
| 67 } |
| 68 |
13 /* | 69 /* |
14 TEST(DataPipePeek, PeekNBytes) { | 70 TEST(DataPipePeek, PeekNBytes) { |
15 DataPipe data_pipe; | 71 DataPipe data_pipe; |
16 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); | 72 DataPipeConsumerHandle consumer(data_pipe.consumer_handle.get()); |
17 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); | 73 DataPipeProducerHandle producer(data_pipe.producer_handle.get()); |
18 | 74 |
19 // Inialize the pipe with 4 bytes. | 75 // Inialize the pipe with 4 bytes. |
20 | 76 |
21 const char* s4 = "1234"; | 77 const char* s4 = "1234"; |
22 uint32_t num_bytes4 = 4; | 78 uint32_t num_bytes4 = 4; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 153 EXPECT_TRUE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
98 EXPECT_EQ(str, std::string(s4) + "\n"); | 154 EXPECT_EQ(str, std::string(s4) + "\n"); |
99 | 155 |
100 // If the max_line_length parameter is less than the length of the | 156 // If the max_line_length parameter is less than the length of the |
101 // newline terminated string, then peek should fail. | 157 // newline terminated string, then peek should fail. |
102 | 158 |
103 max_str_length = 3; | 159 max_str_length = 3; |
104 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); | 160 EXPECT_FALSE(BlockingPeekLine(consumer, &str, max_str_length, timeout)); |
105 } | 161 } |
106 */ | 162 */ |
| 163 |
107 } // namespace | 164 } // namespace |
| 165 |
108 } // namespace common | 166 } // namespace common |
109 } // namespace mojo | 167 } // namespace mojo |
OLD | NEW |