OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // This file tests both |RemoteProducerDataPipeImpl| and | 5 // This file tests both |RemoteProducerDataPipeImpl| and |
6 // |RemoteConsumerDataPipeImpl|. | 6 // |RemoteConsumerDataPipeImpl|. |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 void TearDown() override { | 58 void TearDown() override { |
59 EnsureMessagePipeClosed(0); | 59 EnsureMessagePipeClosed(0); |
60 EnsureMessagePipeClosed(1); | 60 EnsureMessagePipeClosed(1); |
61 io_thread_.PostTaskAndWait( | 61 io_thread_.PostTaskAndWait( |
62 FROM_HERE, base::Bind(&RemoteDataPipeImplTest::TearDownOnIOThread, | 62 FROM_HERE, base::Bind(&RemoteDataPipeImplTest::TearDownOnIOThread, |
63 base::Unretained(this))); | 63 base::Unretained(this))); |
64 } | 64 } |
65 | 65 |
66 protected: | 66 protected: |
67 static DataPipe* CreateLocal(size_t element_size, size_t num_elements) { | 67 static RefPtr<DataPipe> CreateLocal(size_t element_size, |
| 68 size_t num_elements) { |
68 const MojoCreateDataPipeOptions options = { | 69 const MojoCreateDataPipeOptions options = { |
69 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)), | 70 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)), |
70 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | 71 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
71 static_cast<uint32_t>(element_size), | 72 static_cast<uint32_t>(element_size), |
72 static_cast<uint32_t>(num_elements * element_size)}; | 73 static_cast<uint32_t>(num_elements * element_size)}; |
73 MojoCreateDataPipeOptions validated_options = {}; | 74 MojoCreateDataPipeOptions validated_options = {}; |
74 CHECK_EQ(DataPipe::ValidateCreateOptions(MakeUserPointer(&options), | 75 CHECK_EQ(DataPipe::ValidateCreateOptions(MakeUserPointer(&options), |
75 &validated_options), | 76 &validated_options), |
76 MOJO_RESULT_OK); | 77 MOJO_RESULT_OK); |
77 return DataPipe::CreateLocal(validated_options); | 78 return DataPipe::CreateLocal(validated_options); |
78 } | 79 } |
79 | 80 |
80 scoped_refptr<MessagePipe> message_pipe(size_t i) { | 81 RefPtr<MessagePipe> message_pipe(size_t i) { return message_pipes_[i]; } |
81 return message_pipes_[i]; | |
82 } | |
83 | 82 |
84 void EnsureMessagePipeClosed(size_t i) { | 83 void EnsureMessagePipeClosed(size_t i) { |
85 if (!message_pipes_[i]) | 84 if (!message_pipes_[i]) |
86 return; | 85 return; |
87 message_pipes_[i]->Close(0); | 86 message_pipes_[i]->Close(0); |
88 message_pipes_[i] = nullptr; | 87 message_pipes_[i] = nullptr; |
89 } | 88 } |
90 | 89 |
91 private: | 90 private: |
92 // TODO(vtl): The arguments should be rvalue references, but that doesn't | 91 // TODO(vtl): The arguments should be rvalue references, but that doesn't |
(...skipping 20 matching lines...) Expand all Loading... |
113 } | 112 } |
114 if (channels_[1]) { | 113 if (channels_[1]) { |
115 channels_[1]->Shutdown(); | 114 channels_[1]->Shutdown(); |
116 channels_[1] = nullptr; | 115 channels_[1] = nullptr; |
117 } | 116 } |
118 } | 117 } |
119 | 118 |
120 embedder::SimplePlatformSupport platform_support_; | 119 embedder::SimplePlatformSupport platform_support_; |
121 mojo::test::TestIOThread io_thread_; | 120 mojo::test::TestIOThread io_thread_; |
122 RefPtr<Channel> channels_[2]; | 121 RefPtr<Channel> channels_[2]; |
123 scoped_refptr<MessagePipe> message_pipes_[2]; | 122 RefPtr<MessagePipe> message_pipes_[2]; |
124 | 123 |
125 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteDataPipeImplTest); | 124 MOJO_DISALLOW_COPY_AND_ASSIGN(RemoteDataPipeImplTest); |
126 }; | 125 }; |
127 | 126 |
128 // These tests are heavier-weight than ideal. They test remote data pipes by | 127 // These tests are heavier-weight than ideal. They test remote data pipes by |
129 // passing data pipe (producer/consumer) dispatchers over remote message pipes. | 128 // passing data pipe (producer/consumer) dispatchers over remote message pipes. |
130 // Make sure that the test fixture works properly (i.e., that the message pipe | 129 // Make sure that the test fixture works properly (i.e., that the message pipe |
131 // works properly, and that things are shut down correctly). | 130 // works properly, and that things are shut down correctly). |
132 // TODO(vtl): Make lighter-weight tests. Ideally, we'd have tests for remote | 131 // TODO(vtl): Make lighter-weight tests. Ideally, we'd have tests for remote |
133 // data pipes which don't involve message pipes (or even data pipe dispatchers). | 132 // data pipes which don't involve message pipes (or even data pipe dispatchers). |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 165 |
167 TEST_F(RemoteDataPipeImplTest, SendConsumerWithClosedProducer) { | 166 TEST_F(RemoteDataPipeImplTest, SendConsumerWithClosedProducer) { |
168 char read_buffer[100] = {}; | 167 char read_buffer[100] = {}; |
169 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); | 168 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
170 DispatcherVector read_dispatchers; | 169 DispatcherVector read_dispatchers; |
171 uint32_t read_num_dispatchers = 10; // Maximum to get. | 170 uint32_t read_num_dispatchers = 10; // Maximum to get. |
172 Waiter waiter; | 171 Waiter waiter; |
173 HandleSignalsState hss; | 172 HandleSignalsState hss; |
174 uint32_t context = 0; | 173 uint32_t context = 0; |
175 | 174 |
176 scoped_refptr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 175 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
177 // This is the consumer dispatcher we'll send. | 176 // This is the consumer dispatcher we'll send. |
178 scoped_refptr<DataPipeConsumerDispatcher> consumer = | 177 scoped_refptr<DataPipeConsumerDispatcher> consumer = |
179 DataPipeConsumerDispatcher::Create(); | 178 DataPipeConsumerDispatcher::Create(); |
180 consumer->Init(dp); | 179 consumer->Init(dp.Clone()); |
181 | 180 |
182 // Write to the producer and close it, before sending the consumer. | 181 // Write to the producer and close it, before sending the consumer. |
183 int32_t elements[10] = {123}; | 182 int32_t elements[10] = {123}; |
184 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 183 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
185 EXPECT_EQ(MOJO_RESULT_OK, | 184 EXPECT_EQ(MOJO_RESULT_OK, |
186 dp->ProducerWriteData(UserPointer<const void>(elements), | 185 dp->ProducerWriteData(UserPointer<const void>(elements), |
187 MakeUserPointer(&num_bytes), false)); | 186 MakeUserPointer(&num_bytes), false)); |
188 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); | 187 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); |
189 dp->ProducerClose(); | 188 dp->ProducerClose(); |
190 | 189 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 283 |
285 TEST_F(RemoteDataPipeImplTest, SendConsumerDuringTwoPhaseWrite) { | 284 TEST_F(RemoteDataPipeImplTest, SendConsumerDuringTwoPhaseWrite) { |
286 char read_buffer[100] = {}; | 285 char read_buffer[100] = {}; |
287 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); | 286 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
288 DispatcherVector read_dispatchers; | 287 DispatcherVector read_dispatchers; |
289 uint32_t read_num_dispatchers = 10; // Maximum to get. | 288 uint32_t read_num_dispatchers = 10; // Maximum to get. |
290 Waiter waiter; | 289 Waiter waiter; |
291 HandleSignalsState hss; | 290 HandleSignalsState hss; |
292 uint32_t context = 0; | 291 uint32_t context = 0; |
293 | 292 |
294 scoped_refptr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 293 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
295 // This is the consumer dispatcher we'll send. | 294 // This is the consumer dispatcher we'll send. |
296 scoped_refptr<DataPipeConsumerDispatcher> consumer = | 295 scoped_refptr<DataPipeConsumerDispatcher> consumer = |
297 DataPipeConsumerDispatcher::Create(); | 296 DataPipeConsumerDispatcher::Create(); |
298 consumer->Init(dp); | 297 consumer->Init(dp.Clone()); |
299 | 298 |
300 void* write_ptr = nullptr; | 299 void* write_ptr = nullptr; |
301 uint32_t num_bytes = 0u; | 300 uint32_t num_bytes = 0u; |
302 EXPECT_EQ(MOJO_RESULT_OK, | 301 EXPECT_EQ(MOJO_RESULT_OK, |
303 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 302 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
304 MakeUserPointer(&num_bytes))); | 303 MakeUserPointer(&num_bytes))); |
305 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); | 304 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); |
306 | 305 |
307 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). | 306 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). |
308 // (Add the waiter first, to avoid any handling the case where it's already | 307 // (Add the waiter first, to avoid any handling the case where it's already |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 // properly preserved). | 393 // properly preserved). |
395 TEST_F(RemoteDataPipeImplTest, SendConsumerDuringSecondTwoPhaseWrite) { | 394 TEST_F(RemoteDataPipeImplTest, SendConsumerDuringSecondTwoPhaseWrite) { |
396 char read_buffer[100] = {}; | 395 char read_buffer[100] = {}; |
397 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); | 396 uint32_t read_buffer_size = static_cast<uint32_t>(sizeof(read_buffer)); |
398 DispatcherVector read_dispatchers; | 397 DispatcherVector read_dispatchers; |
399 uint32_t read_num_dispatchers = 10; // Maximum to get. | 398 uint32_t read_num_dispatchers = 10; // Maximum to get. |
400 Waiter waiter; | 399 Waiter waiter; |
401 HandleSignalsState hss; | 400 HandleSignalsState hss; |
402 uint32_t context = 0; | 401 uint32_t context = 0; |
403 | 402 |
404 scoped_refptr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 403 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
405 // This is the consumer dispatcher we'll send. | 404 // This is the consumer dispatcher we'll send. |
406 scoped_refptr<DataPipeConsumerDispatcher> consumer = | 405 scoped_refptr<DataPipeConsumerDispatcher> consumer = |
407 DataPipeConsumerDispatcher::Create(); | 406 DataPipeConsumerDispatcher::Create(); |
408 consumer->Init(dp); | 407 consumer->Init(dp.Clone()); |
409 | 408 |
410 void* write_ptr = nullptr; | 409 void* write_ptr = nullptr; |
411 uint32_t num_bytes = 0u; | 410 uint32_t num_bytes = 0u; |
412 EXPECT_EQ(MOJO_RESULT_OK, | 411 EXPECT_EQ(MOJO_RESULT_OK, |
413 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 412 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
414 MakeUserPointer(&num_bytes))); | 413 MakeUserPointer(&num_bytes))); |
415 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); | 414 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); |
416 *static_cast<int32_t*>(write_ptr) = 123456; | 415 *static_cast<int32_t*>(write_ptr) = 123456; |
417 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( | 416 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( |
418 static_cast<uint32_t>(1u * sizeof(int32_t)))); | 417 static_cast<uint32_t>(1u * sizeof(int32_t)))); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 EXPECT_EQ(123456, elements[0]); | 506 EXPECT_EQ(123456, elements[0]); |
508 EXPECT_EQ(789012, elements[1]); | 507 EXPECT_EQ(789012, elements[1]); |
509 EXPECT_EQ(0, elements[2]); | 508 EXPECT_EQ(0, elements[2]); |
510 | 509 |
511 consumer->Close(); | 510 consumer->Close(); |
512 } | 511 } |
513 | 512 |
514 } // namespace | 513 } // namespace |
515 } // namespace system | 514 } // namespace system |
516 } // namespace mojo | 515 } // namespace mojo |
OLD | NEW |