diff --git a/api/test/network_emulation_manager.cc b/api/test/network_emulation_manager.cc index 602c90aac1..9c148a069b 100644 --- a/api/test/network_emulation_manager.cc +++ b/api/test/network_emulation_manager.cc @@ -56,18 +56,20 @@ NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length( } NetworkEmulationManager::SimulatedNetworkNode -NetworkEmulationManager::SimulatedNetworkNode::Builder::Build() const { +NetworkEmulationManager::SimulatedNetworkNode::Builder::Build( + uint64_t random_seed) const { RTC_CHECK(net_); - return Build(net_); + return Build(net_, random_seed); } NetworkEmulationManager::SimulatedNetworkNode NetworkEmulationManager::SimulatedNetworkNode::Builder::Build( - NetworkEmulationManager* net) const { + NetworkEmulationManager* net, + uint64_t random_seed) const { RTC_CHECK(net); RTC_CHECK(net_ == nullptr || net_ == net); SimulatedNetworkNode res; - auto behavior = std::make_unique(config_); + auto behavior = std::make_unique(config_, random_seed); res.simulation = behavior.get(); res.node = net->CreateEmulatedNode(std::move(behavior)); return res; diff --git a/api/test/network_emulation_manager.h b/api/test/network_emulation_manager.h index 8619f36307..80efb0e7d8 100644 --- a/api/test/network_emulation_manager.h +++ b/api/test/network_emulation_manager.h @@ -152,8 +152,9 @@ class NetworkEmulationManager { Builder& capacity_Mbps(int link_capacity_Mbps); Builder& loss(double loss_rate); Builder& packet_queue_length(int max_queue_length_in_packets); - SimulatedNetworkNode Build() const; - SimulatedNetworkNode Build(NetworkEmulationManager* net) const; + SimulatedNetworkNode Build(uint64_t random_seed = 1) const; + SimulatedNetworkNode Build(NetworkEmulationManager* net, + uint64_t random_seed = 1) const; private: NetworkEmulationManager* const net_; @@ -165,9 +166,15 @@ class NetworkEmulationManager { virtual TimeController* time_controller() = 0; // Creates an emulated network node, which represents single network in - // the emulated network layer. + // the emulated network layer. Uses default implementation on network behavior + // which can be configured with |config|. |random_seed| can be provided to + // alter randomization behavior. virtual EmulatedNetworkNode* CreateEmulatedNode( - BuiltInNetworkBehaviorConfig config) = 0; + BuiltInNetworkBehaviorConfig config, + uint64_t random_seed = 1) = 0; + // Creates an emulated network node, which represents single network in + // the emulated network layer. |network_behavior| determines how created node + // will forward incoming packets to the next receiver. virtual EmulatedNetworkNode* CreateEmulatedNode( std::unique_ptr network_behavior) = 0; diff --git a/rtc_base/random.h b/rtc_base/random.h index 0e2d103cb6..b3b9fd1608 100644 --- a/rtc_base/random.h +++ b/rtc_base/random.h @@ -66,7 +66,8 @@ class Random { double Exponential(double lambda); private: - // Outputs a nonzero 64-bit random number. + // Outputs a nonzero 64-bit random number using Xorshift algorithm. + // https://en.wikipedia.org/wiki/Xorshift uint64_t NextOutput() { state_ ^= state_ >> 12; state_ ^= state_ << 25; diff --git a/test/network/network_emulation_manager.cc b/test/network/network_emulation_manager.cc index e9656fa8e0..57706fc782 100644 --- a/test/network/network_emulation_manager.cc +++ b/test/network/network_emulation_manager.cc @@ -63,8 +63,10 @@ NetworkEmulationManagerImpl::~NetworkEmulationManagerImpl() { } EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode( - BuiltInNetworkBehaviorConfig config) { - return CreateEmulatedNode(std::make_unique(config)); + BuiltInNetworkBehaviorConfig config, + uint64_t random_seed) { + return CreateEmulatedNode( + std::make_unique(config, random_seed)); } EmulatedNetworkNode* NetworkEmulationManagerImpl::CreateEmulatedNode( diff --git a/test/network/network_emulation_manager.h b/test/network/network_emulation_manager.h index 7b954e7759..b2b41b34a2 100644 --- a/test/network/network_emulation_manager.h +++ b/test/network/network_emulation_manager.h @@ -44,8 +44,8 @@ class NetworkEmulationManagerImpl : public NetworkEmulationManager { explicit NetworkEmulationManagerImpl(TimeMode mode); ~NetworkEmulationManagerImpl(); - EmulatedNetworkNode* CreateEmulatedNode( - BuiltInNetworkBehaviorConfig config) override; + EmulatedNetworkNode* CreateEmulatedNode(BuiltInNetworkBehaviorConfig config, + uint64_t random_seed = 1) override; EmulatedNetworkNode* CreateEmulatedNode( std::unique_ptr network_behavior) override;