forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork_quality_estimator_manager_unittest.cc
301 lines (264 loc) · 12.4 KB
/
network_quality_estimator_manager_unittest.cc
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2018 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 "services/network/network_quality_estimator_manager.h"
#include <algorithm>
#include <utility>
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "base/time/time.h"
#include "net/log/test_net_log.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator.h"
#include "services/network/public/mojom/network_quality_estimator_manager.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace network {
namespace {
class TestNetworkQualityEstimatorManagerClient
: public mojom::NetworkQualityEstimatorManagerClient {
public:
explicit TestNetworkQualityEstimatorManagerClient(
NetworkQualityEstimatorManager* network_quality_estimator_manager)
: network_quality_estimator_manager_(network_quality_estimator_manager),
num_network_quality_changed_(0),
run_loop_(std::make_unique<base::RunLoop>()),
effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
http_rtt_(base::TimeDelta()),
transport_rtt_(base::TimeDelta()),
downlink_bandwidth_kbps_(INT32_MAX),
binding_(this) {
mojom::NetworkQualityEstimatorManagerPtr manager_ptr;
mojom::NetworkQualityEstimatorManagerRequest request(
mojo::MakeRequest(&manager_ptr));
network_quality_estimator_manager_->AddRequest(std::move(request));
mojom::NetworkQualityEstimatorManagerClientPtr client_ptr;
mojom::NetworkQualityEstimatorManagerClientRequest client_request(
mojo::MakeRequest(&client_ptr));
binding_.Bind(std::move(client_request));
manager_ptr->RequestNotifications(std::move(client_ptr));
}
~TestNetworkQualityEstimatorManagerClient() override {}
void OnNetworkQualityChanged(net::EffectiveConnectionType type,
base::TimeDelta http_rtt,
base::TimeDelta transport_rtt,
int32_t downlink_bandwidth_kbps) override {
num_network_quality_changed_++;
effective_connection_type_ = type;
http_rtt_ = http_rtt;
transport_rtt_ = transport_rtt;
downlink_bandwidth_kbps_ = downlink_bandwidth_kbps;
if (run_loop_wait_effective_connection_type_ == type)
run_loop_->Quit();
}
// Returns the number of OnNetworkQualityChanged() notifications. Note that
// the number may change based on the order in which underlying network
// quality estimator provides notifications when effective connection
// type, RTT and downlink estimates change simultaneously.
size_t num_network_quality_changed() const {
return num_network_quality_changed_;
}
void WaitForNotification(
net::EffectiveConnectionType effective_connection_type) {
run_loop_wait_effective_connection_type_ = effective_connection_type;
run_loop_->Run();
run_loop_.reset(new base::RunLoop());
}
net::EffectiveConnectionType effective_connection_type() const {
return effective_connection_type_;
}
base::TimeDelta http_rtt() const { return http_rtt_; }
base::TimeDelta transport_rtt() const { return transport_rtt_; }
int32_t downlink_bandwidth_kbps() const { return downlink_bandwidth_kbps_; }
private:
NetworkQualityEstimatorManager* network_quality_estimator_manager_;
size_t num_network_quality_changed_;
std::unique_ptr<base::RunLoop> run_loop_;
net::EffectiveConnectionType run_loop_wait_effective_connection_type_;
net::EffectiveConnectionType effective_connection_type_;
base::TimeDelta http_rtt_;
base::TimeDelta transport_rtt_;
int32_t downlink_bandwidth_kbps_;
mojo::Binding<mojom::NetworkQualityEstimatorManagerClient> binding_;
DISALLOW_COPY_AND_ASSIGN(TestNetworkQualityEstimatorManagerClient);
};
} // namespace
class NetworkQualityEstimatorManagerTest : public testing::Test {
public:
NetworkQualityEstimatorManagerTest()
: net_log_(std::make_unique<net::BoundTestNetLog>()),
network_quality_estimator_manager_(
std::make_unique<NetworkQualityEstimatorManager>(
net_log_->bound().net_log())) {
// Change the network quality to UNKNOWN to prevent any spurious
// notifications.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
network_quality_estimator_manager_client_ =
std::make_unique<TestNetworkQualityEstimatorManagerClient>(
network_quality_estimator_manager_.get());
}
~NetworkQualityEstimatorManagerTest() override {}
TestNetworkQualityEstimatorManagerClient*
network_quality_estimator_manager_client() {
return network_quality_estimator_manager_client_.get();
}
NetworkQualityEstimatorManager* network_quality_estimator_manager() const {
return network_quality_estimator_manager_.get();
}
void SimulateNetworkQualityChange(net::EffectiveConnectionType type) {
network_quality_estimator_manager_->GetNetworkQualityEstimator()
->SimulateNetworkQualityChangeForTesting(type);
}
private:
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<net::BoundTestNetLog> net_log_;
std::unique_ptr<NetworkQualityEstimatorManager>
network_quality_estimator_manager_;
std::unique_ptr<TestNetworkQualityEstimatorManagerClient>
network_quality_estimator_manager_client_;
DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimatorManagerTest);
};
TEST_F(NetworkQualityEstimatorManagerTest, ClientNotified) {
// Simulate a new network quality change.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_3G);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_3G);
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_3G,
network_quality_estimator_manager_client()->effective_connection_type());
base::RunLoop().RunUntilIdle();
// Verify that not more than 2 notifications were received.
EXPECT_GE(2u, network_quality_estimator_manager_client()
->num_network_quality_changed());
// Typical RTT and downlink values when effective connection type is 3G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(450),
network_quality_estimator_manager_client()->http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(400),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
400,
network_quality_estimator_manager_client()->downlink_bandwidth_kbps());
}
// Test that when the network quality is unavailable, network quality estimator
// manager reports the estimated network quality values as negative.
TEST_F(NetworkQualityEstimatorManagerTest,
ClientNotifiedUnknownNetworkQuality) {
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
network_quality_estimator_manager_client()->effective_connection_type());
base::RunLoop().RunUntilIdle();
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_3G);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_3G);
base::RunLoop().RunUntilIdle();
// Typical RTT and downlink values when effective connection type is 3G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(450),
network_quality_estimator_manager_client()->http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(400),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
400,
network_quality_estimator_manager_client()->downlink_bandwidth_kbps());
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
base::RunLoop().RunUntilIdle();
EXPECT_GT(base::TimeDelta(),
network_quality_estimator_manager_client()->http_rtt());
EXPECT_GT(base::TimeDelta(),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_GT(
0, network_quality_estimator_manager_client()->downlink_bandwidth_kbps());
}
TEST_F(NetworkQualityEstimatorManagerTest, OneClientPipeBroken) {
auto network_quality_estimator_manager_client2 =
std::make_unique<TestNetworkQualityEstimatorManagerClient>(
network_quality_estimator_manager());
// Simulate a network quality change.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_4G);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_4G);
network_quality_estimator_manager_client2->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_4G);
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_4G,
network_quality_estimator_manager_client()->effective_connection_type());
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_4G,
network_quality_estimator_manager_client2->effective_connection_type());
// Typical RTT and downlink values when effective connection type is 4G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(175),
network_quality_estimator_manager_client2->http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(125),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
1600,
network_quality_estimator_manager_client2->downlink_bandwidth_kbps());
base::RunLoop().RunUntilIdle();
EXPECT_GE(2u, network_quality_estimator_manager_client()
->num_network_quality_changed());
EXPECT_GE(
2u,
network_quality_estimator_manager_client2->num_network_quality_changed());
network_quality_estimator_manager_client2.reset();
base::RunLoop().RunUntilIdle();
// Simulate a second network quality change, and the remaining client should
// be notified.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_2G);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_2G,
network_quality_estimator_manager_client()->effective_connection_type());
EXPECT_GE(3u, network_quality_estimator_manager_client()
->num_network_quality_changed());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1800),
network_quality_estimator_manager_client()->http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1500),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
75,
network_quality_estimator_manager_client()->downlink_bandwidth_kbps());
}
TEST_F(NetworkQualityEstimatorManagerTest,
NewClientReceivesCurrentEffectiveType) {
// Simulate a network quality change.
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_2G);
network_quality_estimator_manager_client()->WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_2G,
network_quality_estimator_manager_client()->effective_connection_type());
base::RunLoop().RunUntilIdle();
// Typical RTT and downlink values when effective connection type is 2G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1800),
network_quality_estimator_manager_client()->http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1500),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
75,
network_quality_estimator_manager_client()->downlink_bandwidth_kbps());
// Register a new client after the network quality change and it should
// receive the up-to-date effective connection type.
TestNetworkQualityEstimatorManagerClient
network_quality_estimator_manager_client2(
network_quality_estimator_manager());
network_quality_estimator_manager_client2.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_2G);
EXPECT_EQ(
net::EFFECTIVE_CONNECTION_TYPE_2G,
network_quality_estimator_manager_client2.effective_connection_type());
// Typical RTT and downlink values when when effective connection type is 2G.
// Taken from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1800),
network_quality_estimator_manager_client2.http_rtt());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(1500),
network_quality_estimator_manager_client()->transport_rtt());
EXPECT_EQ(
75, network_quality_estimator_manager_client2.downlink_bandwidth_kbps());
}
} // namespace network