Bug 1833854 - Part 6: Round requested nursery before checking range when changing...
[gecko.git] / third_party / libwebrtc / api / test / network_emulation_manager.cc
blob236e2f0e174a4020c69d30855f2ff6aabe7a1f9c
1 /*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include "api/test/network_emulation_manager.h"
12 #include <utility>
14 #include "call/simulated_network.h"
15 #include "rtc_base/checks.h"
17 namespace webrtc {
19 bool AbslParseFlag(absl::string_view text, TimeMode* mode, std::string* error) {
20 if (text == "realtime") {
21 *mode = TimeMode::kRealTime;
22 return true;
24 if (text == "simulated") {
25 *mode = TimeMode::kSimulated;
26 return true;
28 *error =
29 "Unknown value for TimeMode enum. Options are 'realtime' or 'simulated'";
30 return false;
33 std::string AbslUnparseFlag(TimeMode mode) {
34 switch (mode) {
35 case TimeMode::kRealTime:
36 return "realtime";
37 case TimeMode::kSimulated:
38 return "simulated";
40 RTC_CHECK_NOTREACHED();
41 return "unknown";
44 NetworkEmulationManager::SimulatedNetworkNode::Builder&
45 NetworkEmulationManager::SimulatedNetworkNode::Builder::config(
46 BuiltInNetworkBehaviorConfig config) {
47 config_ = config;
48 return *this;
51 NetworkEmulationManager::SimulatedNetworkNode::Builder&
52 NetworkEmulationManager::SimulatedNetworkNode::Builder::delay_ms(
53 int queue_delay_ms) {
54 config_.queue_delay_ms = queue_delay_ms;
55 return *this;
58 NetworkEmulationManager::SimulatedNetworkNode::Builder&
59 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_kbps(
60 int link_capacity_kbps) {
61 config_.link_capacity_kbps = link_capacity_kbps;
62 return *this;
65 NetworkEmulationManager::SimulatedNetworkNode::Builder&
66 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_Mbps(
67 int link_capacity_Mbps) {
68 config_.link_capacity_kbps = link_capacity_Mbps * 1000;
69 return *this;
72 NetworkEmulationManager::SimulatedNetworkNode::Builder&
73 NetworkEmulationManager::SimulatedNetworkNode::Builder::loss(double loss_rate) {
74 config_.loss_percent = std::round(loss_rate * 100);
75 return *this;
78 NetworkEmulationManager::SimulatedNetworkNode::Builder&
79 NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length(
80 int max_queue_length_in_packets) {
81 config_.queue_length_packets = max_queue_length_in_packets;
82 return *this;
85 NetworkEmulationManager::SimulatedNetworkNode
86 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
87 uint64_t random_seed) const {
88 RTC_CHECK(net_);
89 return Build(net_, random_seed);
92 NetworkEmulationManager::SimulatedNetworkNode
93 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
94 NetworkEmulationManager* net,
95 uint64_t random_seed) const {
96 RTC_CHECK(net);
97 RTC_CHECK(net_ == nullptr || net_ == net);
98 SimulatedNetworkNode res;
99 auto behavior = std::make_unique<SimulatedNetwork>(config_, random_seed);
100 res.simulation = behavior.get();
101 res.node = net->CreateEmulatedNode(std::move(behavior));
102 return res;
105 std::pair<EmulatedNetworkManagerInterface*, EmulatedNetworkManagerInterface*>
106 NetworkEmulationManager::CreateEndpointPairWithTwoWayRoutes(
107 const BuiltInNetworkBehaviorConfig& config) {
108 auto* alice_node = CreateEmulatedNode(config);
109 auto* bob_node = CreateEmulatedNode(config);
111 auto* alice_endpoint = CreateEndpoint(EmulatedEndpointConfig());
112 auto* bob_endpoint = CreateEndpoint(EmulatedEndpointConfig());
114 CreateRoute(alice_endpoint, {alice_node}, bob_endpoint);
115 CreateRoute(bob_endpoint, {bob_node}, alice_endpoint);
117 return {
118 CreateEmulatedNetworkManagerInterface({alice_endpoint}),
119 CreateEmulatedNetworkManagerInterface({bob_endpoint}),
122 } // namespace webrtc