1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <string>
#include "base/callback_helpers.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/net/blimp_message_pump.h"
#include "blimp/net/common.h"
#include "blimp/net/connection_error_observer.h"
#include "blimp/net/test_common.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::DoAll;
using testing::InSequence;
using testing::NotNull;
using testing::Return;
using testing::SaveArg;
namespace blimp {
namespace {
class BlimpMessagePumpTest : public testing::Test {
public:
BlimpMessagePumpTest()
: message1_(new BlimpMessage), message2_(new BlimpMessage) {
message1_->set_type(BlimpMessage::INPUT);
message2_->set_type(BlimpMessage::CONTROL);
message_pump_.reset(new BlimpMessagePump(&reader_));
message_pump_->set_error_observer(&error_observer_);
}
~BlimpMessagePumpTest() override {}
protected:
scoped_ptr<BlimpMessage> message1_;
scoped_ptr<BlimpMessage> message2_;
testing::StrictMock<MockPacketReader> reader_;
testing::StrictMock<MockConnectionErrorObserver> error_observer_;
testing::StrictMock<MockBlimpMessageProcessor> receiver_;
scoped_ptr<BlimpMessagePump> message_pump_;
};
// Reader completes reading one packet synchronously.
TEST_F(BlimpMessagePumpTest, SyncPacketRead) {
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _));
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
Return(message1_->ByteSize())));
message_pump_->SetMessageProcessor(&receiver_);
}
// Reader completes reading two packets synchronously.
TEST_F(BlimpMessagePumpTest, SyncTwoPacketsRead) {
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
Return(message1_->ByteSize())))
.WillOnce(DoAll(FillBufferFromMessage<0>(message2_.get()),
Return(message2_->ByteSize())));
net::CompletionCallback process_msg_cb;
{
InSequence s;
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _))
.WillOnce(SaveArg<1>(&process_msg_cb))
.RetiresOnSaturation();
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message2_), _));
}
message_pump_->SetMessageProcessor(&receiver_);
// Trigger next packet read
process_msg_cb.Run(net::OK);
}
// Reader completes reading one packet asynchronously.
TEST_F(BlimpMessagePumpTest, AsyncPacketRead) {
net::CompletionCallback read_packet_cb;
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
SaveArg<1>(&read_packet_cb), Return(net::ERR_IO_PENDING)))
.WillOnce(Return(net::ERR_IO_PENDING));
net::CompletionCallback process_msg_cb;
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _))
.WillOnce(SaveArg<1>(&process_msg_cb));
message_pump_->SetMessageProcessor(&receiver_);
read_packet_cb.Run(message1_->ByteSize());
process_msg_cb.Run(net::OK);
}
// Reader completes reading two packets asynchronously.
TEST_F(BlimpMessagePumpTest, AsyncTwoPacketsRead) {
net::CompletionCallback read_packet_cb;
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
SaveArg<1>(&read_packet_cb), Return(net::ERR_IO_PENDING)))
.WillOnce(DoAll(FillBufferFromMessage<0>(message2_.get()),
SaveArg<1>(&read_packet_cb),
Return(net::ERR_IO_PENDING)));
net::CompletionCallback process_msg_cb;
{
InSequence s;
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _))
.WillOnce(SaveArg<1>(&process_msg_cb))
.RetiresOnSaturation();
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message2_), _));
}
message_pump_->SetMessageProcessor(&receiver_);
read_packet_cb.Run(message1_->ByteSize());
// Trigger next packet read
process_msg_cb.Run(net::OK);
read_packet_cb.Run(message2_->ByteSize());
}
// Reader completes reading two packets asynchronously.
// The first read succeeds, and the second fails.
TEST_F(BlimpMessagePumpTest, AsyncTwoPacketsReadWithError) {
net::CompletionCallback read_packet_cb;
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
SaveArg<1>(&read_packet_cb), Return(net::ERR_IO_PENDING)))
.WillOnce(DoAll(FillBufferFromMessage<0>(message2_.get()),
SaveArg<1>(&read_packet_cb),
Return(net::ERR_IO_PENDING)));
net::CompletionCallback process_msg_cb;
{
InSequence s;
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _))
.WillOnce(SaveArg<1>(&process_msg_cb));
EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));
}
message_pump_->SetMessageProcessor(&receiver_);
read_packet_cb.Run(message1_->ByteSize());
// Trigger next packet read
process_msg_cb.Run(net::OK);
read_packet_cb.Run(net::ERR_FAILED);
}
// Reader completes reading one packet synchronously, but packet is invalid
TEST_F(BlimpMessagePumpTest, InvalidPacket) {
std::string test_msg("msg");
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromString<0>(test_msg), Return(1)));
EXPECT_CALL(error_observer_, OnConnectionError(net::ERR_FAILED));
message_pump_->SetMessageProcessor(&receiver_);
}
TEST_F(BlimpMessagePumpTest, ClearMessageProcessorAfterRead) {
net::CompletionCallback read_packet_cb;
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
SaveArg<1>(&read_packet_cb),
Return(net::ERR_IO_PENDING)));
net::CompletionCallback process_msg_cb;
EXPECT_CALL(receiver_, MockableProcessMessage(EqualsProto(*message1_), _))
.WillOnce(SaveArg<1>(&process_msg_cb));
message_pump_->SetMessageProcessor(&receiver_);
base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize());
message_pump_->SetMessageProcessor(nullptr);
// Completing message processing will not trigger next packet read.
base::ResetAndReturn(&process_msg_cb).Run(net::OK);
}
TEST_F(BlimpMessagePumpTest, ClearMessageProcessorDuringRead) {
net::CompletionCallback read_packet_cb;
EXPECT_CALL(reader_, ReadPacket(NotNull(), _))
.WillOnce(DoAll(FillBufferFromMessage<0>(message1_.get()),
SaveArg<1>(&read_packet_cb),
Return(net::ERR_IO_PENDING)));
// Receiver will not get any message.
message_pump_->SetMessageProcessor(&receiver_);
message_pump_->SetMessageProcessor(nullptr);
base::ResetAndReturn(&read_packet_cb).Run(message1_->ByteSize());
}
} // namespace
} // namespace blimp
|