| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <utility> | 5 #include <utility> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 ~SyncMethodCommonTest() override {} | 260 ~SyncMethodCommonTest() override {} |
| 261 }; | 261 }; |
| 262 | 262 |
| 263 class SyncMethodAssociatedTest : public SyncMethodTest { | 263 class SyncMethodAssociatedTest : public SyncMethodTest { |
| 264 public: | 264 public: |
| 265 SyncMethodAssociatedTest() {} | 265 SyncMethodAssociatedTest() {} |
| 266 ~SyncMethodAssociatedTest() override {} | 266 ~SyncMethodAssociatedTest() override {} |
| 267 | 267 |
| 268 protected: | 268 protected: |
| 269 void SetUp() override { | 269 void SetUp() override { |
| 270 master_impl_.reset(new TestSyncMasterImpl(GetProxy(&master_ptr_))); | 270 master_impl_.reset(new TestSyncMasterImpl(MakeRequest(&master_ptr_))); |
| 271 | 271 |
| 272 master_ptr_.associated_group()->CreateAssociatedInterface( | 272 master_ptr_.associated_group()->CreateAssociatedInterface( |
| 273 AssociatedGroup::WILL_PASS_REQUEST, &asso_ptr_info_, &asso_request_); | 273 AssociatedGroup::WILL_PASS_REQUEST, &asso_ptr_info_, &asso_request_); |
| 274 master_ptr_.associated_group()->CreateAssociatedInterface( | 274 master_ptr_.associated_group()->CreateAssociatedInterface( |
| 275 AssociatedGroup::WILL_PASS_PTR, &opposite_asso_ptr_info_, | 275 AssociatedGroup::WILL_PASS_PTR, &opposite_asso_ptr_info_, |
| 276 &opposite_asso_request_); | 276 &opposite_asso_request_); |
| 277 | 277 |
| 278 master_impl_->set_send_interface_handler( | 278 master_impl_->set_send_interface_handler( |
| 279 [this](TestSyncAssociatedPtrInfo ptr) { | 279 [this](TestSyncAssociatedPtrInfo ptr) { |
| 280 opposite_asso_ptr_info_ = std::move(ptr); | 280 opposite_asso_ptr_info_ = std::move(ptr); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 return base::Bind(&CallAsyncEchoCallback<Func>, func); | 335 return base::Bind(&CallAsyncEchoCallback<Func>, func); |
| 336 } | 336 } |
| 337 | 337 |
| 338 // TestSync and TestSyncMaster exercise Router and MultiplexRouter, | 338 // TestSync and TestSyncMaster exercise Router and MultiplexRouter, |
| 339 // respectively. | 339 // respectively. |
| 340 using InterfaceTypes = testing::Types<TestSync, TestSyncMaster>; | 340 using InterfaceTypes = testing::Types<TestSync, TestSyncMaster>; |
| 341 TYPED_TEST_CASE(SyncMethodCommonTest, InterfaceTypes); | 341 TYPED_TEST_CASE(SyncMethodCommonTest, InterfaceTypes); |
| 342 | 342 |
| 343 TYPED_TEST(SyncMethodCommonTest, CallSyncMethodAsynchronously) { | 343 TYPED_TEST(SyncMethodCommonTest, CallSyncMethodAsynchronously) { |
| 344 InterfacePtr<TypeParam> ptr; | 344 InterfacePtr<TypeParam> ptr; |
| 345 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 345 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 346 | 346 |
| 347 base::RunLoop run_loop; | 347 base::RunLoop run_loop; |
| 348 ptr->Echo(123, base::Bind(&ExpectValueAndRunClosure, 123, | 348 ptr->Echo(123, base::Bind(&ExpectValueAndRunClosure, 123, |
| 349 run_loop.QuitClosure())); | 349 run_loop.QuitClosure())); |
| 350 run_loop.Run(); | 350 run_loop.Run(); |
| 351 } | 351 } |
| 352 | 352 |
| 353 TYPED_TEST(SyncMethodCommonTest, BasicSyncCalls) { | 353 TYPED_TEST(SyncMethodCommonTest, BasicSyncCalls) { |
| 354 InterfacePtr<TypeParam> ptr; | 354 InterfacePtr<TypeParam> ptr; |
| 355 | 355 |
| 356 TestSyncServiceThread<TypeParam> service_thread; | 356 TestSyncServiceThread<TypeParam> service_thread; |
| 357 service_thread.thread()->task_runner()->PostTask( | 357 service_thread.thread()->task_runner()->PostTask( |
| 358 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::SetUp, | 358 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::SetUp, |
| 359 base::Unretained(&service_thread), | 359 base::Unretained(&service_thread), |
| 360 base::Passed(GetProxy(&ptr)))); | 360 base::Passed(MakeRequest(&ptr)))); |
| 361 ASSERT_TRUE(ptr->Ping()); | 361 ASSERT_TRUE(ptr->Ping()); |
| 362 ASSERT_TRUE(service_thread.ping_called()); | 362 ASSERT_TRUE(service_thread.ping_called()); |
| 363 | 363 |
| 364 int32_t output_value = -1; | 364 int32_t output_value = -1; |
| 365 ASSERT_TRUE(ptr->Echo(42, &output_value)); | 365 ASSERT_TRUE(ptr->Echo(42, &output_value)); |
| 366 ASSERT_EQ(42, output_value); | 366 ASSERT_EQ(42, output_value); |
| 367 | 367 |
| 368 base::RunLoop run_loop; | 368 base::RunLoop run_loop; |
| 369 service_thread.thread()->task_runner()->PostTaskAndReply( | 369 service_thread.thread()->task_runner()->PostTaskAndReply( |
| 370 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::TearDown, | 370 FROM_HERE, base::Bind(&TestSyncServiceThread<TypeParam>::TearDown, |
| 371 base::Unretained(&service_thread)), | 371 base::Unretained(&service_thread)), |
| 372 run_loop.QuitClosure()); | 372 run_loop.QuitClosure()); |
| 373 run_loop.Run(); | 373 run_loop.Run(); |
| 374 } | 374 } |
| 375 | 375 |
| 376 TYPED_TEST(SyncMethodCommonTest, ReenteredBySyncMethodBinding) { | 376 TYPED_TEST(SyncMethodCommonTest, ReenteredBySyncMethodBinding) { |
| 377 // Test that an interface pointer waiting for a sync call response can be | 377 // Test that an interface pointer waiting for a sync call response can be |
| 378 // reentered by a binding serving sync methods on the same thread. | 378 // reentered by a binding serving sync methods on the same thread. |
| 379 | 379 |
| 380 InterfacePtr<TypeParam> ptr; | 380 InterfacePtr<TypeParam> ptr; |
| 381 // The binding lives on the same thread as the interface pointer. | 381 // The binding lives on the same thread as the interface pointer. |
| 382 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 382 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 383 int32_t output_value = -1; | 383 int32_t output_value = -1; |
| 384 ASSERT_TRUE(ptr->Echo(42, &output_value)); | 384 ASSERT_TRUE(ptr->Echo(42, &output_value)); |
| 385 EXPECT_EQ(42, output_value); | 385 EXPECT_EQ(42, output_value); |
| 386 } | 386 } |
| 387 | 387 |
| 388 TYPED_TEST(SyncMethodCommonTest, InterfacePtrDestroyedDuringSyncCall) { | 388 TYPED_TEST(SyncMethodCommonTest, InterfacePtrDestroyedDuringSyncCall) { |
| 389 // Test that it won't result in crash or hang if an interface pointer is | 389 // Test that it won't result in crash or hang if an interface pointer is |
| 390 // destroyed while it is waiting for a sync call response. | 390 // destroyed while it is waiting for a sync call response. |
| 391 | 391 |
| 392 InterfacePtr<TypeParam> ptr; | 392 InterfacePtr<TypeParam> ptr; |
| 393 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 393 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 394 impl.set_ping_handler([&ptr](const TestSync::PingCallback& callback) { | 394 impl.set_ping_handler([&ptr](const TestSync::PingCallback& callback) { |
| 395 ptr.reset(); | 395 ptr.reset(); |
| 396 callback.Run(); | 396 callback.Run(); |
| 397 }); | 397 }); |
| 398 ASSERT_FALSE(ptr->Ping()); | 398 ASSERT_FALSE(ptr->Ping()); |
| 399 } | 399 } |
| 400 | 400 |
| 401 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) { | 401 TYPED_TEST(SyncMethodCommonTest, BindingDestroyedDuringSyncCall) { |
| 402 // Test that it won't result in crash or hang if a binding is | 402 // Test that it won't result in crash or hang if a binding is |
| 403 // closed (and therefore the message pipe handle is closed) while the | 403 // closed (and therefore the message pipe handle is closed) while the |
| 404 // corresponding interface pointer is waiting for a sync call response. | 404 // corresponding interface pointer is waiting for a sync call response. |
| 405 | 405 |
| 406 InterfacePtr<TypeParam> ptr; | 406 InterfacePtr<TypeParam> ptr; |
| 407 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 407 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 408 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) { | 408 impl.set_ping_handler([&impl](const TestSync::PingCallback& callback) { |
| 409 impl.binding()->Close(); | 409 impl.binding()->Close(); |
| 410 callback.Run(); | 410 callback.Run(); |
| 411 }); | 411 }); |
| 412 ASSERT_FALSE(ptr->Ping()); | 412 ASSERT_FALSE(ptr->Ping()); |
| 413 } | 413 } |
| 414 | 414 |
| 415 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithInOrderResponses) { | 415 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithInOrderResponses) { |
| 416 // Test that we can call a sync method on an interface ptr, while there is | 416 // Test that we can call a sync method on an interface ptr, while there is |
| 417 // already a sync call ongoing. The responses arrive in order. | 417 // already a sync call ongoing. The responses arrive in order. |
| 418 | 418 |
| 419 InterfacePtr<TypeParam> ptr; | 419 InterfacePtr<TypeParam> ptr; |
| 420 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 420 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 421 | 421 |
| 422 // The same variable is used to store the output of the two sync calls, in | 422 // The same variable is used to store the output of the two sync calls, in |
| 423 // order to test that responses are handled in the correct order. | 423 // order to test that responses are handled in the correct order. |
| 424 int32_t result_value = -1; | 424 int32_t result_value = -1; |
| 425 | 425 |
| 426 bool first_call = true; | 426 bool first_call = true; |
| 427 impl.set_echo_handler([&first_call, &ptr, &result_value]( | 427 impl.set_echo_handler([&first_call, &ptr, &result_value]( |
| 428 int32_t value, const TestSync::EchoCallback& callback) { | 428 int32_t value, const TestSync::EchoCallback& callback) { |
| 429 if (first_call) { | 429 if (first_call) { |
| 430 first_call = false; | 430 first_call = false; |
| 431 ASSERT_TRUE(ptr->Echo(456, &result_value)); | 431 ASSERT_TRUE(ptr->Echo(456, &result_value)); |
| 432 EXPECT_EQ(456, result_value); | 432 EXPECT_EQ(456, result_value); |
| 433 } | 433 } |
| 434 callback.Run(value); | 434 callback.Run(value); |
| 435 }); | 435 }); |
| 436 | 436 |
| 437 ASSERT_TRUE(ptr->Echo(123, &result_value)); | 437 ASSERT_TRUE(ptr->Echo(123, &result_value)); |
| 438 EXPECT_EQ(123, result_value); | 438 EXPECT_EQ(123, result_value); |
| 439 } | 439 } |
| 440 | 440 |
| 441 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) { | 441 TYPED_TEST(SyncMethodCommonTest, NestedSyncCallsWithOutOfOrderResponses) { |
| 442 // Test that we can call a sync method on an interface ptr, while there is | 442 // Test that we can call a sync method on an interface ptr, while there is |
| 443 // already a sync call ongoing. The responses arrive out of order. | 443 // already a sync call ongoing. The responses arrive out of order. |
| 444 | 444 |
| 445 InterfacePtr<TypeParam> ptr; | 445 InterfacePtr<TypeParam> ptr; |
| 446 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 446 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 447 | 447 |
| 448 // The same variable is used to store the output of the two sync calls, in | 448 // The same variable is used to store the output of the two sync calls, in |
| 449 // order to test that responses are handled in the correct order. | 449 // order to test that responses are handled in the correct order. |
| 450 int32_t result_value = -1; | 450 int32_t result_value = -1; |
| 451 | 451 |
| 452 bool first_call = true; | 452 bool first_call = true; |
| 453 impl.set_echo_handler([&first_call, &ptr, &result_value]( | 453 impl.set_echo_handler([&first_call, &ptr, &result_value]( |
| 454 int32_t value, const TestSync::EchoCallback& callback) { | 454 int32_t value, const TestSync::EchoCallback& callback) { |
| 455 callback.Run(value); | 455 callback.Run(value); |
| 456 if (first_call) { | 456 if (first_call) { |
| 457 first_call = false; | 457 first_call = false; |
| 458 ASSERT_TRUE(ptr->Echo(456, &result_value)); | 458 ASSERT_TRUE(ptr->Echo(456, &result_value)); |
| 459 EXPECT_EQ(456, result_value); | 459 EXPECT_EQ(456, result_value); |
| 460 } | 460 } |
| 461 }); | 461 }); |
| 462 | 462 |
| 463 ASSERT_TRUE(ptr->Echo(123, &result_value)); | 463 ASSERT_TRUE(ptr->Echo(123, &result_value)); |
| 464 EXPECT_EQ(123, result_value); | 464 EXPECT_EQ(123, result_value); |
| 465 } | 465 } |
| 466 | 466 |
| 467 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) { | 467 TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) { |
| 468 // Test that while an interface pointer is waiting for the response to a sync | 468 // Test that while an interface pointer is waiting for the response to a sync |
| 469 // call, async responses are queued until the sync call completes. | 469 // call, async responses are queued until the sync call completes. |
| 470 | 470 |
| 471 InterfacePtr<TypeParam> ptr; | 471 InterfacePtr<TypeParam> ptr; |
| 472 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 472 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 473 | 473 |
| 474 int32_t async_echo_request_value = -1; | 474 int32_t async_echo_request_value = -1; |
| 475 TestSync::AsyncEchoCallback async_echo_request_callback; | 475 TestSync::AsyncEchoCallback async_echo_request_callback; |
| 476 base::RunLoop run_loop1; | 476 base::RunLoop run_loop1; |
| 477 impl.set_async_echo_handler( | 477 impl.set_async_echo_handler( |
| 478 [&async_echo_request_value, &async_echo_request_callback, &run_loop1]( | 478 [&async_echo_request_value, &async_echo_request_callback, &run_loop1]( |
| 479 int32_t value, const TestSync::AsyncEchoCallback& callback) { | 479 int32_t value, const TestSync::AsyncEchoCallback& callback) { |
| 480 async_echo_request_value = value; | 480 async_echo_request_value = value; |
| 481 async_echo_request_callback = callback; | 481 async_echo_request_callback = callback; |
| 482 run_loop1.Quit(); | 482 run_loop1.Quit(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 518 | 518 |
| 519 EXPECT_TRUE(async_echo_response_dispatched); | 519 EXPECT_TRUE(async_echo_response_dispatched); |
| 520 } | 520 } |
| 521 | 521 |
| 522 TYPED_TEST(SyncMethodCommonTest, AsyncRequestQueuedDuringSyncCall) { | 522 TYPED_TEST(SyncMethodCommonTest, AsyncRequestQueuedDuringSyncCall) { |
| 523 // Test that while an interface pointer is waiting for the response to a sync | 523 // Test that while an interface pointer is waiting for the response to a sync |
| 524 // call, async requests for a binding running on the same thread are queued | 524 // call, async requests for a binding running on the same thread are queued |
| 525 // until the sync call completes. | 525 // until the sync call completes. |
| 526 | 526 |
| 527 InterfacePtr<TypeParam> ptr; | 527 InterfacePtr<TypeParam> ptr; |
| 528 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 528 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 529 | 529 |
| 530 bool async_echo_request_dispatched = false; | 530 bool async_echo_request_dispatched = false; |
| 531 impl.set_async_echo_handler([&async_echo_request_dispatched]( | 531 impl.set_async_echo_handler([&async_echo_request_dispatched]( |
| 532 int32_t value, const TestSync::AsyncEchoCallback& callback) { | 532 int32_t value, const TestSync::AsyncEchoCallback& callback) { |
| 533 async_echo_request_dispatched = true; | 533 async_echo_request_dispatched = true; |
| 534 callback.Run(value); | 534 callback.Run(value); |
| 535 }); | 535 }); |
| 536 | 536 |
| 537 bool async_echo_response_dispatched = false; | 537 bool async_echo_response_dispatched = false; |
| 538 base::RunLoop run_loop; | 538 base::RunLoop run_loop; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 569 } | 569 } |
| 570 | 570 |
| 571 TYPED_TEST(SyncMethodCommonTest, | 571 TYPED_TEST(SyncMethodCommonTest, |
| 572 QueuedMessagesProcessedBeforeErrorNotification) { | 572 QueuedMessagesProcessedBeforeErrorNotification) { |
| 573 // Test that while an interface pointer is waiting for the response to a sync | 573 // Test that while an interface pointer is waiting for the response to a sync |
| 574 // call, async responses are queued. If the message pipe is disconnected | 574 // call, async responses are queued. If the message pipe is disconnected |
| 575 // before the queued messages are processed, the connection error | 575 // before the queued messages are processed, the connection error |
| 576 // notification is delayed until all the queued messages are processed. | 576 // notification is delayed until all the queued messages are processed. |
| 577 | 577 |
| 578 InterfacePtr<TypeParam> ptr; | 578 InterfacePtr<TypeParam> ptr; |
| 579 typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); | 579 typename ImplTraits<TypeParam>::Type impl(MakeRequest(&ptr)); |
| 580 | 580 |
| 581 int32_t async_echo_request_value = -1; | 581 int32_t async_echo_request_value = -1; |
| 582 TestSync::AsyncEchoCallback async_echo_request_callback; | 582 TestSync::AsyncEchoCallback async_echo_request_callback; |
| 583 base::RunLoop run_loop1; | 583 base::RunLoop run_loop1; |
| 584 impl.set_async_echo_handler( | 584 impl.set_async_echo_handler( |
| 585 [&async_echo_request_value, &async_echo_request_callback, &run_loop1]( | 585 [&async_echo_request_value, &async_echo_request_callback, &run_loop1]( |
| 586 int32_t value, const TestSync::AsyncEchoCallback& callback) { | 586 int32_t value, const TestSync::AsyncEchoCallback& callback) { |
| 587 async_echo_request_value = value; | 587 async_echo_request_value = value; |
| 588 async_echo_request_callback = callback; | 588 async_echo_request_callback = callback; |
| 589 run_loop1.Quit(); | 589 run_loop1.Quit(); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 int32_t result_value = -1; | 733 int32_t result_value = -1; |
| 734 ASSERT_TRUE(master_ptr_->Echo(456, &result_value)); | 734 ASSERT_TRUE(master_ptr_->Echo(456, &result_value)); |
| 735 EXPECT_EQ(456, result_value); | 735 EXPECT_EQ(456, result_value); |
| 736 } | 736 } |
| 737 | 737 |
| 738 // TODO(yzshen): Add more tests related to associated interfaces. | 738 // TODO(yzshen): Add more tests related to associated interfaces. |
| 739 | 739 |
| 740 } // namespace | 740 } // namespace |
| 741 } // namespace test | 741 } // namespace test |
| 742 } // namespace mojo | 742 } // namespace mojo |
| OLD | NEW |