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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 // Although the AsyncEcho request is sent before the Echo request, it | 338 // Although the AsyncEcho request is sent before the Echo request, it |
339 // shouldn't be dispatched yet. | 339 // shouldn't be dispatched yet. |
340 EXPECT_FALSE(async_echo_request_dispatched); | 340 EXPECT_FALSE(async_echo_request_dispatched); |
341 | 341 |
342 // Run until the AsyncEcho response is dispatched. | 342 // Run until the AsyncEcho response is dispatched. |
343 run_loop.Run(); | 343 run_loop.Run(); |
344 | 344 |
345 EXPECT_TRUE(async_echo_response_dispatched); | 345 EXPECT_TRUE(async_echo_response_dispatched); |
346 } | 346 } |
347 | 347 |
| 348 TEST_F(SyncMethodTest, QueuedMessagesProcessedBeforeErrorNotification) { |
| 349 // Test that while an interface pointer is waiting for the response to a sync |
| 350 // call, async responses are queued. If the message pipe is disconnected |
| 351 // before the queued messages are processed, the connection error |
| 352 // notification is delayed until all the queued messages are processed. |
| 353 |
| 354 TestSyncPtr ptr; |
| 355 TestSyncImpl impl(GetProxy(&ptr)); |
| 356 |
| 357 int32_t async_echo_request_value = -1; |
| 358 TestSync::AsyncEchoCallback async_echo_request_callback; |
| 359 base::RunLoop run_loop1; |
| 360 impl.set_async_echo_handler( |
| 361 [&async_echo_request_value, &async_echo_request_callback, &run_loop1]( |
| 362 int32_t value, const TestSync::AsyncEchoCallback& callback) { |
| 363 async_echo_request_value = value; |
| 364 async_echo_request_callback = callback; |
| 365 run_loop1.Quit(); |
| 366 }); |
| 367 |
| 368 bool async_echo_response_dispatched = false; |
| 369 base::RunLoop run_loop2; |
| 370 ptr->AsyncEcho(123, |
| 371 [&async_echo_response_dispatched, &run_loop2](int32_t result) { |
| 372 async_echo_response_dispatched = true; |
| 373 EXPECT_EQ(123, result); |
| 374 run_loop2.Quit(); |
| 375 }); |
| 376 // Run until the AsyncEcho request reaches the service side. |
| 377 run_loop1.Run(); |
| 378 |
| 379 impl.set_echo_handler( |
| 380 [&impl, &async_echo_request_value, &async_echo_request_callback]( |
| 381 int32_t value, const TestSync::EchoCallback& callback) { |
| 382 // Send back the async response first. |
| 383 EXPECT_FALSE(async_echo_request_callback.is_null()); |
| 384 async_echo_request_callback.Run(async_echo_request_value); |
| 385 |
| 386 impl.binding()->Close(); |
| 387 }); |
| 388 |
| 389 bool connection_error_dispatched = false; |
| 390 base::RunLoop run_loop3; |
| 391 ptr.set_connection_error_handler( |
| 392 [&connection_error_dispatched, &run_loop3]() { |
| 393 connection_error_dispatched = true; |
| 394 run_loop3.Quit(); |
| 395 }); |
| 396 |
| 397 int32_t result_value = -1; |
| 398 ASSERT_FALSE(ptr->Echo(456, &result_value)); |
| 399 EXPECT_EQ(-1, result_value); |
| 400 ASSERT_FALSE(connection_error_dispatched); |
| 401 EXPECT_FALSE(ptr.encountered_error()); |
| 402 |
| 403 // Although the AsyncEcho response arrives before the Echo response, it should |
| 404 // be queued and not yet dispatched. |
| 405 EXPECT_FALSE(async_echo_response_dispatched); |
| 406 |
| 407 // Run until the AsyncEcho response is dispatched. |
| 408 run_loop2.Run(); |
| 409 |
| 410 EXPECT_TRUE(async_echo_response_dispatched); |
| 411 ASSERT_FALSE(connection_error_dispatched); |
| 412 EXPECT_FALSE(ptr.encountered_error()); |
| 413 |
| 414 // Run until the error notification is dispatched. |
| 415 run_loop3.Run(); |
| 416 |
| 417 ASSERT_TRUE(connection_error_dispatched); |
| 418 EXPECT_TRUE(ptr.encountered_error()); |
| 419 } |
| 420 |
| 421 TEST_F(SyncMethodTest, InvalidMessageDuringSyncCall) { |
| 422 // Test that while an interface pointer is waiting for the response to a sync |
| 423 // call, an invalid incoming message will disconnect the message pipe, cause |
| 424 // the sync call to return false, and run the connection error handler |
| 425 // asynchronously. |
| 426 |
| 427 MessagePipe pipe; |
| 428 |
| 429 TestSyncPtr ptr; |
| 430 ptr.Bind(TestSyncPtrInfo(std::move(pipe.handle0), 0u)); |
| 431 |
| 432 MessagePipeHandle raw_binding_handle = pipe.handle1.get(); |
| 433 TestSyncImpl impl(MakeRequest<TestSync>(std::move(pipe.handle1))); |
| 434 |
| 435 impl.set_echo_handler([&raw_binding_handle]( |
| 436 int32_t value, const TestSync::EchoCallback& callback) { |
| 437 // Write a 1-byte message, which is considered invalid. |
| 438 char invalid_message = 0; |
| 439 MojoResult result = |
| 440 WriteMessageRaw(raw_binding_handle, &invalid_message, 1u, nullptr, 0u, |
| 441 MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 442 ASSERT_EQ(MOJO_RESULT_OK, result); |
| 443 callback.Run(value); |
| 444 }); |
| 445 |
| 446 bool connection_error_dispatched = false; |
| 447 base::RunLoop run_loop; |
| 448 ptr.set_connection_error_handler([&connection_error_dispatched, &run_loop]() { |
| 449 connection_error_dispatched = true; |
| 450 run_loop.Quit(); |
| 451 }); |
| 452 |
| 453 int32_t result_value = -1; |
| 454 ASSERT_FALSE(ptr->Echo(456, &result_value)); |
| 455 EXPECT_EQ(-1, result_value); |
| 456 ASSERT_FALSE(connection_error_dispatched); |
| 457 |
| 458 run_loop.Run(); |
| 459 ASSERT_TRUE(connection_error_dispatched); |
| 460 } |
| 461 |
348 } // namespace | 462 } // namespace |
349 } // namespace test | 463 } // namespace test |
350 } // namespace mojo | 464 } // namespace mojo |
OLD | NEW |