| 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 <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "mojo/edk/embedder/simple_platform_support.h" | 14 #include "mojo/edk/embedder/simple_platform_support.h" |
| 15 #include "mojo/edk/platform/platform_pipe.h" | 15 #include "mojo/edk/platform/platform_pipe.h" |
| 16 #include "mojo/edk/system/channel.h" | 16 #include "mojo/edk/system/channel.h" |
| 17 #include "mojo/edk/system/channel_endpoint.h" | 17 #include "mojo/edk/system/channel_endpoint.h" |
| 18 #include "mojo/edk/system/data_pipe.h" | 18 #include "mojo/edk/system/data_pipe.h" |
| 19 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" | 19 #include "mojo/edk/system/data_pipe_consumer_dispatcher.h" |
| 20 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" | 20 #include "mojo/edk/system/data_pipe_producer_dispatcher.h" |
| 21 #include "mojo/edk/system/handle.h" |
| 21 #include "mojo/edk/system/handle_transport.h" | 22 #include "mojo/edk/system/handle_transport.h" |
| 22 #include "mojo/edk/system/memory.h" | 23 #include "mojo/edk/system/memory.h" |
| 23 #include "mojo/edk/system/message_pipe.h" | 24 #include "mojo/edk/system/message_pipe.h" |
| 24 #include "mojo/edk/system/raw_channel.h" | 25 #include "mojo/edk/system/raw_channel.h" |
| 25 #include "mojo/edk/system/test/test_io_thread.h" | 26 #include "mojo/edk/system/test/test_io_thread.h" |
| 26 #include "mojo/edk/system/test/timeouts.h" | 27 #include "mojo/edk/system/test/timeouts.h" |
| 27 #include "mojo/edk/system/waiter.h" | 28 #include "mojo/edk/system/waiter.h" |
| 28 #include "mojo/edk/util/ref_ptr.h" | 29 #include "mojo/edk/util/ref_ptr.h" |
| 29 #include "mojo/public/cpp/system/macros.h" | 30 #include "mojo/public/cpp/system/macros.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 DispatcherVector read_dispatchers; | 170 DispatcherVector read_dispatchers; |
| 170 uint32_t read_num_dispatchers = 10; // Maximum to get. | 171 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 171 Waiter waiter; | 172 Waiter waiter; |
| 172 HandleSignalsState hss; | 173 HandleSignalsState hss; |
| 173 uint32_t context = 0; | 174 uint32_t context = 0; |
| 174 | 175 |
| 175 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 176 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
| 176 // This is the consumer dispatcher we'll send. | 177 // This is the consumer dispatcher we'll send. |
| 177 auto consumer = DataPipeConsumerDispatcher::Create(); | 178 auto consumer = DataPipeConsumerDispatcher::Create(); |
| 178 consumer->Init(dp.Clone()); | 179 consumer->Init(dp.Clone()); |
| 180 Handle consumer_handle(std::move(consumer), MOJO_HANDLE_RIGHT_TRANSFER | |
| 181 MOJO_HANDLE_RIGHT_READ | |
| 182 MOJO_HANDLE_RIGHT_WRITE); |
| 179 | 183 |
| 180 // Write to the producer and close it, before sending the consumer. | 184 // Write to the producer and close it, before sending the consumer. |
| 181 int32_t elements[10] = {123}; | 185 int32_t elements[10] = {123}; |
| 182 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); | 186 uint32_t num_bytes = static_cast<uint32_t>(1u * sizeof(elements[0])); |
| 183 EXPECT_EQ(MOJO_RESULT_OK, | 187 EXPECT_EQ(MOJO_RESULT_OK, |
| 184 dp->ProducerWriteData(UserPointer<const void>(elements), | 188 dp->ProducerWriteData(UserPointer<const void>(elements), |
| 185 MakeUserPointer(&num_bytes), false)); | 189 MakeUserPointer(&num_bytes), false)); |
| 186 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); | 190 EXPECT_EQ(1u * sizeof(elements[0]), num_bytes); |
| 187 dp->ProducerClose(); | 191 dp->ProducerClose(); |
| 188 | 192 |
| 189 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). | 193 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). |
| 190 // (Add the waiter first, to avoid any handling the case where it's already | 194 // (Add the waiter first, to avoid any handling the case where it's already |
| 191 // readable.) | 195 // readable.) |
| 192 waiter.Init(); | 196 waiter.Init(); |
| 193 ASSERT_EQ(MOJO_RESULT_OK, | 197 ASSERT_EQ(MOJO_RESULT_OK, |
| 194 message_pipe(1)->AddAwakable( | 198 message_pipe(1)->AddAwakable( |
| 195 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); | 199 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); |
| 196 { | 200 { |
| 197 DispatcherTransport transport( | 201 DispatcherTransport transport( |
| 198 test::DispatcherTryStartTransport(consumer.get())); | 202 test::HandleTryStartTransport(consumer_handle)); |
| 199 EXPECT_TRUE(transport.is_valid()); | 203 EXPECT_TRUE(transport.is_valid()); |
| 200 | 204 |
| 201 std::vector<DispatcherTransport> transports; | 205 std::vector<DispatcherTransport> transports; |
| 202 transports.push_back(transport); | 206 transports.push_back(transport); |
| 203 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( | 207 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( |
| 204 0, NullUserPointer(), 0, &transports, | 208 0, NullUserPointer(), 0, &transports, |
| 205 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 209 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 206 transport.End(); | 210 transport.End(); |
| 207 | 211 |
| 208 // |consumer| should have been closed. This is |DCHECK()|ed when it is | 212 // |consumer_handle.dispatcher| should have been closed. This is |
| 209 // destroyed. | 213 // |DCHECK()|ed when it is destroyed. |
| 210 EXPECT_TRUE(consumer->HasOneRef()); | 214 EXPECT_TRUE(consumer_handle.dispatcher->HasOneRef()); |
| 211 consumer = nullptr; | 215 consumer_handle.reset(); |
| 212 } | 216 } |
| 213 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); | 217 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); |
| 214 EXPECT_EQ(123u, context); | 218 EXPECT_EQ(123u, context); |
| 215 hss = HandleSignalsState(); | 219 hss = HandleSignalsState(); |
| 216 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); | 220 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); |
| 217 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, | 221 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 218 hss.satisfied_signals); | 222 hss.satisfied_signals); |
| 219 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | | 223 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | |
| 220 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 224 MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 221 hss.satisfiable_signals); | 225 hss.satisfiable_signals); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 DispatcherVector read_dispatchers; | 294 DispatcherVector read_dispatchers; |
| 291 uint32_t read_num_dispatchers = 10; // Maximum to get. | 295 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 292 Waiter waiter; | 296 Waiter waiter; |
| 293 HandleSignalsState hss; | 297 HandleSignalsState hss; |
| 294 uint32_t context = 0; | 298 uint32_t context = 0; |
| 295 | 299 |
| 296 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 300 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
| 297 // This is the consumer dispatcher we'll send. | 301 // This is the consumer dispatcher we'll send. |
| 298 auto consumer = DataPipeConsumerDispatcher::Create(); | 302 auto consumer = DataPipeConsumerDispatcher::Create(); |
| 299 consumer->Init(dp.Clone()); | 303 consumer->Init(dp.Clone()); |
| 304 Handle consumer_handle(std::move(consumer), MOJO_HANDLE_RIGHT_TRANSFER | |
| 305 MOJO_HANDLE_RIGHT_READ | |
| 306 MOJO_HANDLE_RIGHT_WRITE); |
| 300 | 307 |
| 301 void* write_ptr = nullptr; | 308 void* write_ptr = nullptr; |
| 302 uint32_t num_bytes = 0u; | 309 uint32_t num_bytes = 0u; |
| 303 EXPECT_EQ(MOJO_RESULT_OK, | 310 EXPECT_EQ(MOJO_RESULT_OK, |
| 304 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 311 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 305 MakeUserPointer(&num_bytes))); | 312 MakeUserPointer(&num_bytes))); |
| 306 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); | 313 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); |
| 307 | 314 |
| 308 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). | 315 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). |
| 309 // (Add the waiter first, to avoid any handling the case where it's already | 316 // (Add the waiter first, to avoid any handling the case where it's already |
| 310 // readable.) | 317 // readable.) |
| 311 waiter.Init(); | 318 waiter.Init(); |
| 312 ASSERT_EQ(MOJO_RESULT_OK, | 319 ASSERT_EQ(MOJO_RESULT_OK, |
| 313 message_pipe(1)->AddAwakable( | 320 message_pipe(1)->AddAwakable( |
| 314 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); | 321 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); |
| 315 { | 322 { |
| 316 DispatcherTransport transport( | 323 DispatcherTransport transport( |
| 317 test::DispatcherTryStartTransport(consumer.get())); | 324 test::HandleTryStartTransport(consumer_handle)); |
| 318 EXPECT_TRUE(transport.is_valid()); | 325 EXPECT_TRUE(transport.is_valid()); |
| 319 | 326 |
| 320 std::vector<DispatcherTransport> transports; | 327 std::vector<DispatcherTransport> transports; |
| 321 transports.push_back(transport); | 328 transports.push_back(transport); |
| 322 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( | 329 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( |
| 323 0, NullUserPointer(), 0, &transports, | 330 0, NullUserPointer(), 0, &transports, |
| 324 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 331 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 325 transport.End(); | 332 transport.End(); |
| 326 | 333 |
| 327 // |consumer| should have been closed. This is |DCHECK()|ed when it is | 334 // |consumer_handle.dispatcher| should have been closed. This is |
| 328 // destroyed. | 335 // |DCHECK()|ed when it is destroyed. |
| 329 EXPECT_TRUE(consumer->HasOneRef()); | 336 EXPECT_TRUE(consumer_handle.dispatcher->HasOneRef()); |
| 330 consumer = nullptr; | 337 consumer_handle.reset(); |
| 331 } | 338 } |
| 332 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); | 339 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); |
| 333 EXPECT_EQ(123u, context); | 340 EXPECT_EQ(123u, context); |
| 334 hss = HandleSignalsState(); | 341 hss = HandleSignalsState(); |
| 335 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); | 342 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); |
| 336 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, | 343 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 337 hss.satisfied_signals); | 344 hss.satisfied_signals); |
| 338 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | | 345 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | |
| 339 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 346 MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 340 hss.satisfiable_signals); | 347 hss.satisfiable_signals); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 DispatcherVector read_dispatchers; | 410 DispatcherVector read_dispatchers; |
| 404 uint32_t read_num_dispatchers = 10; // Maximum to get. | 411 uint32_t read_num_dispatchers = 10; // Maximum to get. |
| 405 Waiter waiter; | 412 Waiter waiter; |
| 406 HandleSignalsState hss; | 413 HandleSignalsState hss; |
| 407 uint32_t context = 0; | 414 uint32_t context = 0; |
| 408 | 415 |
| 409 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); | 416 RefPtr<DataPipe> dp(CreateLocal(sizeof(int32_t), 1000)); |
| 410 // This is the consumer dispatcher we'll send. | 417 // This is the consumer dispatcher we'll send. |
| 411 auto consumer = DataPipeConsumerDispatcher::Create(); | 418 auto consumer = DataPipeConsumerDispatcher::Create(); |
| 412 consumer->Init(dp.Clone()); | 419 consumer->Init(dp.Clone()); |
| 420 Handle consumer_handle(std::move(consumer), MOJO_HANDLE_RIGHT_TRANSFER | |
| 421 MOJO_HANDLE_RIGHT_READ | |
| 422 MOJO_HANDLE_RIGHT_WRITE); |
| 413 | 423 |
| 414 void* write_ptr = nullptr; | 424 void* write_ptr = nullptr; |
| 415 uint32_t num_bytes = 0u; | 425 uint32_t num_bytes = 0u; |
| 416 EXPECT_EQ(MOJO_RESULT_OK, | 426 EXPECT_EQ(MOJO_RESULT_OK, |
| 417 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 427 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 418 MakeUserPointer(&num_bytes))); | 428 MakeUserPointer(&num_bytes))); |
| 419 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); | 429 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); |
| 420 *static_cast<int32_t*>(write_ptr) = 123456; | 430 *static_cast<int32_t*>(write_ptr) = 123456; |
| 421 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( | 431 EXPECT_EQ(MOJO_RESULT_OK, dp->ProducerEndWriteData( |
| 422 static_cast<uint32_t>(1u * sizeof(int32_t)))); | 432 static_cast<uint32_t>(1u * sizeof(int32_t)))); |
| 423 | 433 |
| 424 write_ptr = nullptr; | 434 write_ptr = nullptr; |
| 425 num_bytes = 0u; | 435 num_bytes = 0u; |
| 426 EXPECT_EQ(MOJO_RESULT_OK, | 436 EXPECT_EQ(MOJO_RESULT_OK, |
| 427 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), | 437 dp->ProducerBeginWriteData(MakeUserPointer(&write_ptr), |
| 428 MakeUserPointer(&num_bytes))); | 438 MakeUserPointer(&num_bytes))); |
| 429 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); | 439 ASSERT_GE(num_bytes, 1u * sizeof(int32_t)); |
| 430 | 440 |
| 431 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). | 441 // Write the consumer to MP 0 (port 0). Wait and receive on MP 1 (port 0). |
| 432 // (Add the waiter first, to avoid any handling the case where it's already | 442 // (Add the waiter first, to avoid any handling the case where it's already |
| 433 // readable.) | 443 // readable.) |
| 434 waiter.Init(); | 444 waiter.Init(); |
| 435 ASSERT_EQ(MOJO_RESULT_OK, | 445 ASSERT_EQ(MOJO_RESULT_OK, |
| 436 message_pipe(1)->AddAwakable( | 446 message_pipe(1)->AddAwakable( |
| 437 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); | 447 0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); |
| 438 { | 448 { |
| 439 DispatcherTransport transport( | 449 DispatcherTransport transport( |
| 440 test::DispatcherTryStartTransport(consumer.get())); | 450 test::HandleTryStartTransport(consumer_handle)); |
| 441 EXPECT_TRUE(transport.is_valid()); | 451 EXPECT_TRUE(transport.is_valid()); |
| 442 | 452 |
| 443 std::vector<DispatcherTransport> transports; | 453 std::vector<DispatcherTransport> transports; |
| 444 transports.push_back(transport); | 454 transports.push_back(transport); |
| 445 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( | 455 EXPECT_EQ(MOJO_RESULT_OK, message_pipe(0)->WriteMessage( |
| 446 0, NullUserPointer(), 0, &transports, | 456 0, NullUserPointer(), 0, &transports, |
| 447 MOJO_WRITE_MESSAGE_FLAG_NONE)); | 457 MOJO_WRITE_MESSAGE_FLAG_NONE)); |
| 448 transport.End(); | 458 transport.End(); |
| 449 | 459 |
| 450 // |consumer| should have been closed. This is |DCHECK()|ed when it is | 460 // |consumer_handle.dispatcher| should have been closed. This is |
| 451 // destroyed. | 461 // |DCHECK()|ed when it is destroyed. |
| 452 EXPECT_TRUE(consumer->HasOneRef()); | 462 EXPECT_TRUE(consumer_handle.dispatcher->HasOneRef()); |
| 453 consumer = nullptr; | 463 consumer_handle.reset(); |
| 454 } | 464 } |
| 455 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); | 465 EXPECT_EQ(MOJO_RESULT_OK, waiter.Wait(test::ActionTimeout(), &context)); |
| 456 EXPECT_EQ(123u, context); | 466 EXPECT_EQ(123u, context); |
| 457 hss = HandleSignalsState(); | 467 hss = HandleSignalsState(); |
| 458 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); | 468 message_pipe(1)->RemoveAwakable(0, &waiter, &hss); |
| 459 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, | 469 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE, |
| 460 hss.satisfied_signals); | 470 hss.satisfied_signals); |
| 461 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | | 471 EXPECT_EQ(MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_WRITABLE | |
| 462 MOJO_HANDLE_SIGNAL_PEER_CLOSED, | 472 MOJO_HANDLE_SIGNAL_PEER_CLOSED, |
| 463 hss.satisfiable_signals); | 473 hss.satisfiable_signals); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 EXPECT_EQ(123456, elements[0]); | 525 EXPECT_EQ(123456, elements[0]); |
| 516 EXPECT_EQ(789012, elements[1]); | 526 EXPECT_EQ(789012, elements[1]); |
| 517 EXPECT_EQ(0, elements[2]); | 527 EXPECT_EQ(0, elements[2]); |
| 518 | 528 |
| 519 consumer->Close(); | 529 consumer->Close(); |
| 520 } | 530 } |
| 521 | 531 |
| 522 } // namespace | 532 } // namespace |
| 523 } // namespace system | 533 } // namespace system |
| 524 } // namespace mojo | 534 } // namespace mojo |
| OLD | NEW |