OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/net/blimp_message_checkpointer.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/test/test_mock_time_task_runner.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "blimp/common/create_blimp_message.h" | |
12 #include "blimp/net/blimp_message_checkpoint_observer.h" | |
13 #include "blimp/net/test_common.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/test_completion_callback.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using testing::_; | |
20 using testing::SaveArg; | |
21 | |
22 namespace blimp { | |
23 | |
24 namespace { | |
25 class MockCheckpointObserver : public BlimpMessageCheckpointObserver { | |
26 public: | |
27 MockCheckpointObserver() {} | |
28 ~MockCheckpointObserver() override {} | |
29 | |
30 MOCK_METHOD1(OnMessageCheckpoint, void(int64_t)); | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 class BlimpMessageCheckpointerTest : public testing::Test { | |
36 public: | |
37 BlimpMessageCheckpointerTest() | |
38 : runner_(new base::TestMockTimeTaskRunner), runner_handle_(runner_) {} | |
39 | |
40 ~BlimpMessageCheckpointerTest() override {} | |
41 | |
42 int64_t SimulateIncomingMessage() { | |
43 InputMessage* input = nullptr; | |
44 std::unique_ptr<BlimpMessage> message(CreateBlimpMessage(&input)); | |
45 message->set_message_id(++message_id_); | |
46 checkpointer_->ProcessMessage( | |
47 std::move(message), | |
48 base::Bind(&BlimpMessageCheckpointerTest::IncomingCompletionCallback, | |
49 base::Unretained(this))); | |
50 return message_id_; | |
51 } | |
52 | |
53 void SetUp() override { | |
54 checkpointer_ = base::MakeUnique<BlimpMessageCheckpointer>( | |
55 &incoming_processor_, &outgoing_processor_, &checkpoint_observer_); | |
56 } | |
57 | |
58 MOCK_METHOD1(IncomingCompletionCallback, void(int)); | |
59 | |
60 protected: | |
61 // Used to verify timing-specific behaviours. | |
62 scoped_refptr<base::TestMockTimeTaskRunner> runner_; | |
63 base::ThreadTaskRunnerHandle runner_handle_; | |
64 | |
65 int64_t message_id_ = 0; | |
66 | |
67 testing::StrictMock<MockBlimpMessageProcessor> incoming_processor_; | |
68 testing::StrictMock<MockBlimpMessageProcessor> outgoing_processor_; | |
69 testing::StrictMock<MockCheckpointObserver> checkpoint_observer_; | |
70 net::CompletionCallback captured_cb_; | |
71 | |
72 std::unique_ptr<BlimpMessageCheckpointer> checkpointer_; | |
73 }; | |
74 | |
75 TEST_F(BlimpMessageCheckpointerTest, CallbackPropagates) { | |
76 EXPECT_CALL(incoming_processor_, MockableProcessMessage(_, _)) | |
77 .WillOnce(SaveArg<1>(&captured_cb_)); | |
78 EXPECT_CALL(*this, IncomingCompletionCallback(_)); | |
79 | |
80 // Simulate an incoming message. | |
81 SimulateIncomingMessage(); | |
82 | |
83 // Verify TestCompletionCallback called on completion. | |
84 captured_cb_.Run(net::OK); | |
85 } | |
86 | |
87 TEST_F(BlimpMessageCheckpointerTest, DeleteWhileProcessing) { | |
88 EXPECT_CALL(incoming_processor_, MockableProcessMessage(_, _)) | |
89 .WillOnce(SaveArg<1>(&captured_cb_)); | |
90 EXPECT_CALL(*this, IncomingCompletionCallback(_)).Times(0); | |
91 | |
92 // Simulate an incoming message. | |
93 SimulateIncomingMessage(); | |
94 | |
95 // Delete the checkpointer, then simulate completion of processing. | |
96 // TestCompletionCallback should not fire, so no expectations. | |
97 checkpointer_ = nullptr; | |
98 captured_cb_.Run(net::OK); | |
99 } | |
100 | |
101 TEST_F(BlimpMessageCheckpointerTest, SingleMessageAck) { | |
102 EXPECT_CALL(incoming_processor_, MockableProcessMessage(_, _)) | |
103 .WillOnce(SaveArg<1>(&captured_cb_)); | |
104 std::unique_ptr<BlimpMessage> expected_ack = CreateCheckpointAckMessage(1); | |
105 EXPECT_CALL(outgoing_processor_, | |
106 MockableProcessMessage(EqualsProto(*expected_ack), _)); | |
107 EXPECT_CALL(*this, IncomingCompletionCallback(net::OK)); | |
108 | |
109 // Simulate an incoming message. | |
110 SimulateIncomingMessage(); | |
111 | |
112 // Fast-forward time to verify that an ACK message is sent. | |
113 captured_cb_.Run(net::OK); | |
114 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | |
115 } | |
116 | |
117 TEST_F(BlimpMessageCheckpointerTest, BatchMessageAck) { | |
118 EXPECT_CALL(incoming_processor_, MockableProcessMessage(_, _)) | |
119 .Times(10) | |
120 .WillRepeatedly(SaveArg<1>(&captured_cb_)); | |
121 std::unique_ptr<BlimpMessage> expected_ack = CreateCheckpointAckMessage(10); | |
122 EXPECT_CALL(outgoing_processor_, | |
123 MockableProcessMessage(EqualsProto(*expected_ack), _)); | |
124 EXPECT_CALL(*this, IncomingCompletionCallback(net::OK)).Times(10); | |
125 | |
126 // Simulate ten incoming messages. | |
127 for (int i = 0; i < 10; ++i) { | |
128 SimulateIncomingMessage(); | |
129 captured_cb_.Run(net::OK); | |
130 } | |
131 | |
132 // Fast-forward time to verify that only a single ACK message is sent. | |
133 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | |
134 } | |
135 | |
136 TEST_F(BlimpMessageCheckpointerTest, MultipleAcks) { | |
137 EXPECT_CALL(incoming_processor_, MockableProcessMessage(_, _)) | |
138 .Times(2) | |
139 .WillRepeatedly(SaveArg<1>(&captured_cb_)); | |
140 std::unique_ptr<BlimpMessage> expected_ack1 = CreateCheckpointAckMessage(1); | |
141 EXPECT_CALL(outgoing_processor_, | |
142 MockableProcessMessage(EqualsProto(*expected_ack1), _)); | |
143 std::unique_ptr<BlimpMessage> expected_ack2 = CreateCheckpointAckMessage(2); | |
144 EXPECT_CALL(outgoing_processor_, | |
145 MockableProcessMessage(EqualsProto(*expected_ack2), _)); | |
146 EXPECT_CALL(*this, IncomingCompletionCallback(net::OK)).Times(2); | |
147 | |
148 // Simulate an incoming messages and fast-forward to get the ACK. | |
149 SimulateIncomingMessage(); | |
150 captured_cb_.Run(net::OK); | |
151 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | |
152 | |
153 // And do it again to verify we get a fresh ACK. | |
154 SimulateIncomingMessage(); | |
155 captured_cb_.Run(net::OK); | |
156 runner_->FastForwardBy(base::TimeDelta::FromSeconds(2)); | |
157 } | |
158 | |
159 TEST_F(BlimpMessageCheckpointerTest, IncomingAckMessage) { | |
160 EXPECT_CALL(*this, IncomingCompletionCallback(net::OK)); | |
161 EXPECT_CALL(checkpoint_observer_, OnMessageCheckpoint(10)); | |
162 | |
163 // Simulate an incoming message. | |
164 std::unique_ptr<BlimpMessage> ack_message = CreateCheckpointAckMessage(10); | |
165 checkpointer_->ProcessMessage( | |
166 std::move(ack_message), | |
167 base::Bind(&BlimpMessageCheckpointerTest::IncomingCompletionCallback, | |
168 base::Unretained(this))); | |
169 } | |
170 | |
171 } // namespace blimp | |
OLD | NEW |