OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/public/cpp/bindings/lib/connector.h" | 5 #include "mojo/public/cpp/bindings/lib/connector.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <utility> | 10 #include <utility> |
(...skipping 10 matching lines...) Expand all Loading... |
21 namespace test { | 21 namespace test { |
22 namespace { | 22 namespace { |
23 | 23 |
24 class MessageAccumulator : public MessageReceiver { | 24 class MessageAccumulator : public MessageReceiver { |
25 public: | 25 public: |
26 MessageAccumulator() {} | 26 MessageAccumulator() {} |
27 explicit MessageAccumulator(const Closure& closure) : closure_(closure) {} | 27 explicit MessageAccumulator(const Closure& closure) : closure_(closure) {} |
28 | 28 |
29 bool Accept(Message* message) override { | 29 bool Accept(Message* message) override { |
30 queue_.Push(message); | 30 queue_.Push(message); |
31 if (!closure_.is_null()) | 31 if (!closure_.is_null()) { |
32 closure_.Run(); | 32 Closure closure = closure_; |
33 closure_.reset(); | 33 closure_.reset(); |
| 34 closure.Run(); |
| 35 } |
34 return true; | 36 return true; |
35 } | 37 } |
36 | 38 |
37 bool IsEmpty() const { return queue_.IsEmpty(); } | 39 bool IsEmpty() const { return queue_.IsEmpty(); } |
38 | 40 |
39 void Pop(Message* message) { queue_.Pop(message); } | 41 void Pop(Message* message) { queue_.Pop(message); } |
40 | 42 |
41 void set_closure(const base::Closure& closure) { | 43 void set_closure(const Closure& closure) { closure_ = closure; } |
42 closure_ = closure; | |
43 } | |
44 | 44 |
45 size_t size() const { return queue_.size(); } | 45 size_t size() const { return queue_.size(); } |
46 | 46 |
47 private: | 47 private: |
48 MessageQueue queue_; | 48 MessageQueue queue_; |
49 Closure closure_; | 49 Closure closure_; |
50 }; | 50 }; |
51 | 51 |
52 class ConnectorDeletingMessageAccumulator : public MessageAccumulator { | 52 class ConnectorDeletingMessageAccumulator : public MessageAccumulator { |
53 public: | 53 public: |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 }); | 519 }); |
520 connector1.set_incoming_receiver(&accumulator); | 520 connector1.set_incoming_receiver(&accumulator); |
521 | 521 |
522 run_loop.Run(); | 522 run_loop.Run(); |
523 | 523 |
524 // As we paused after the first message we should only have gotten one | 524 // As we paused after the first message we should only have gotten one |
525 // message. | 525 // message. |
526 ASSERT_EQ(1u, accumulator.size()); | 526 ASSERT_EQ(1u, accumulator.size()); |
527 } | 527 } |
528 | 528 |
| 529 TEST_F(ConnectorTest, ProcessWhenNested) { |
| 530 internal::Connector connector0(std::move(handle0_), |
| 531 internal::Connector::SINGLE_THREADED_SEND); |
| 532 internal::Connector connector1(std::move(handle1_), |
| 533 internal::Connector::SINGLE_THREADED_SEND); |
| 534 |
| 535 const char kText[] = "hello world"; |
| 536 |
| 537 Message message; |
| 538 AllocMessage(kText, &message); |
| 539 |
| 540 // Queue up two messages. |
| 541 connector0.Accept(&message); |
| 542 connector0.Accept(&message); |
| 543 |
| 544 base::RunLoop run_loop; |
| 545 MessageAccumulator accumulator; |
| 546 // When the accumulator gets the first message it spins a nested message |
| 547 // loop. The loop is quit when another message is received. |
| 548 accumulator.set_closure([&accumulator, &connector1, &run_loop]() { |
| 549 base::RunLoop nested_run_loop; |
| 550 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 551 base::MessageLoop::current()); |
| 552 accumulator.set_closure([&nested_run_loop]() { nested_run_loop.Quit(); }); |
| 553 nested_run_loop.Run(); |
| 554 run_loop.Quit(); |
| 555 }); |
| 556 connector1.set_incoming_receiver(&accumulator); |
| 557 |
| 558 run_loop.Run(); |
| 559 |
| 560 ASSERT_EQ(2u, accumulator.size()); |
| 561 } |
| 562 |
529 } // namespace | 563 } // namespace |
530 } // namespace test | 564 } // namespace test |
531 } // namespace mojo | 565 } // namespace mojo |
OLD | NEW |