Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: webrtc/p2p/base/p2ptransportchannel_unittest.cc

Issue 2069493002: Do not switch best connection on the controlled side too frequently (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Add tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2009 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2009 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 1959 matching lines...) Expand 10 before | Expand all | Expand 10 after
1970 1970
1971 int SendData(cricket::TransportChannel& channel, 1971 int SendData(cricket::TransportChannel& channel,
1972 const char* data, 1972 const char* data,
1973 size_t len, 1973 size_t len,
1974 int packet_id) { 1974 int packet_id) {
1975 rtc::PacketOptions options; 1975 rtc::PacketOptions options;
1976 options.packet_id = packet_id; 1976 options.packet_id = packet_id;
1977 return channel.SendPacket(data, len, options, 0); 1977 return channel.SendPacket(data, len, options, 0);
1978 } 1978 }
1979 1979
1980 cricket::Connection* CreateConnectionWithCandidate(
1981 cricket::P2PTransportChannel& channel,
1982 const std::string& ip_addr,
1983 int port,
1984 int priority,
1985 bool writable) {
1986 channel.AddRemoteCandidate(CreateHostCandidate(ip_addr, port, priority));
1987 cricket::Connection* conn = WaitForConnectionTo(&channel, ip_addr, port);
1988 if (conn && writable) {
1989 conn->ReceivedPingResponse(); // make it writable
1990 }
1991 return conn;
1992 }
1993
1994 void NominateConnection(cricket::Connection* conn) {
1995 conn->set_nominated(true);
1996 conn->SignalNominated(conn);
1997 }
1998
1980 void OnSelectedCandidatePairChanged( 1999 void OnSelectedCandidatePairChanged(
1981 cricket::TransportChannel* transport_channel, 2000 cricket::TransportChannel* transport_channel,
1982 cricket::CandidatePairInterface* selected_candidate_pair, 2001 cricket::CandidatePairInterface* selected_candidate_pair,
1983 int last_sent_packet_id) { 2002 int last_sent_packet_id) {
1984 last_selected_candidate_pair_ = selected_candidate_pair; 2003 last_selected_candidate_pair_ = selected_candidate_pair;
1985 last_sent_packet_id_ = last_sent_packet_id; 2004 last_sent_packet_id_ = last_sent_packet_id;
2005 ++num_selected_candidate_pair_switch_;
pthatcher1 2016/06/15 21:59:53 switch_ => switches_ And you can probably drop "n
honghaiz3 2016/06/16 23:03:49 Done.
1986 } 2006 }
1987 2007
1988 void ReceivePingOnConnection(cricket::Connection* conn, 2008 void ReceivePingOnConnection(cricket::Connection* conn,
1989 const std::string& remote_ufrag, 2009 const std::string& remote_ufrag,
1990 int priority) { 2010 int priority) {
1991 cricket::IceMessage msg; 2011 cricket::IceMessage msg;
1992 msg.SetType(cricket::STUN_BINDING_REQUEST); 2012 msg.SetType(cricket::STUN_BINDING_REQUEST);
1993 msg.AddAttribute(new cricket::StunByteStringAttribute( 2013 msg.AddAttribute(new cricket::StunByteStringAttribute(
1994 cricket::STUN_ATTR_USERNAME, 2014 cricket::STUN_ATTR_USERNAME,
1995 conn->local_candidate().username() + ":" + remote_ufrag)); 2015 conn->local_candidate().username() + ":" + remote_ufrag));
(...skipping 15 matching lines...) Expand all
2011 channel_state_ = channel->GetState(); 2031 channel_state_ = channel->GetState();
2012 } 2032 }
2013 2033
2014 cricket::CandidatePairInterface* last_selected_candidate_pair() { 2034 cricket::CandidatePairInterface* last_selected_candidate_pair() {
2015 return last_selected_candidate_pair_; 2035 return last_selected_candidate_pair_;
2016 } 2036 }
2017 int last_sent_packet_id() { return last_sent_packet_id_; } 2037 int last_sent_packet_id() { return last_sent_packet_id_; }
2018 bool channel_ready_to_send() { return channel_ready_to_send_; } 2038 bool channel_ready_to_send() { return channel_ready_to_send_; }
2019 void reset_channel_ready_to_send() { channel_ready_to_send_ = false; } 2039 void reset_channel_ready_to_send() { channel_ready_to_send_ = false; }
2020 cricket::TransportChannelState channel_state() { return channel_state_; } 2040 cricket::TransportChannelState channel_state() { return channel_state_; }
2041 int num_selected_candidate_pair_switch() {
2042 return num_selected_candidate_pair_switch_;
2043 }
2044 void reset_num_selected_candidate_pair_switch() {
2045 num_selected_candidate_pair_switch_ = 0;
2046 }
2021 2047
2022 private: 2048 private:
2023 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 2049 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
2024 std::unique_ptr<rtc::VirtualSocketServer> vss_; 2050 std::unique_ptr<rtc::VirtualSocketServer> vss_;
2025 rtc::SocketServerScope ss_scope_; 2051 rtc::SocketServerScope ss_scope_;
2026 cricket::CandidatePairInterface* last_selected_candidate_pair_ = nullptr; 2052 cricket::CandidatePairInterface* last_selected_candidate_pair_ = nullptr;
2053 int num_selected_candidate_pair_switch_ = 0;
2027 int last_sent_packet_id_ = -1; 2054 int last_sent_packet_id_ = -1;
2028 bool channel_ready_to_send_ = false; 2055 bool channel_ready_to_send_ = false;
2029 cricket::TransportChannelState channel_state_ = cricket::STATE_INIT; 2056 cricket::TransportChannelState channel_state_ = cricket::STATE_INIT;
2030 }; 2057 };
2031 2058
2032 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { 2059 TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) {
2033 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2060 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2034 cricket::P2PTransportChannel ch("trigger checks", 1, &pa); 2061 cricket::P2PTransportChannel ch("trigger checks", 1, &pa);
2035 PrepareChannel(&ch); 2062 PrepareChannel(&ch);
2036 ch.Connect(); 2063 ch.Connect();
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
2466 EXPECT_EQ(conn3, ch.best_connection()); 2493 EXPECT_EQ(conn3, ch.best_connection());
2467 2494
2468 // Now another data packet will not switch the best connection because the 2495 // Now another data packet will not switch the best connection because the
2469 // best connection was nominated by the controlling side. 2496 // best connection was nominated by the controlling side.
2470 conn2->ReceivedPing(); 2497 conn2->ReceivedPing();
2471 conn2->ReceivedPingResponse(); 2498 conn2->ReceivedPingResponse();
2472 conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0)); 2499 conn2->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0));
2473 EXPECT_EQ(conn3, ch.best_connection()); 2500 EXPECT_EQ(conn3, ch.best_connection());
2474 } 2501 }
2475 2502
2503 TEST_F(P2PTransportChannelPingTest, TestDonotSwitchConnectionTooFrequently) {
2504 rtc::ScopedFakeClock clock;
2505 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2506 cricket::P2PTransportChannel ch("receiving state change", 1, &pa);
2507 PrepareChannel(&ch);
2508 ch.SetIceRole(cricket::ICEROLE_CONTROLLED);
2509 ch.Connect();
2510 ch.MaybeStartGathering();
2511 // The connections have decreasing priority.
2512 cricket::Connection* conn1 =
2513 CreateConnectionWithCandidate(ch, "1.1.1.1", 1, 10, true);
2514 ASSERT_TRUE(conn1 != nullptr);
2515 cricket::Connection* conn2 =
2516 CreateConnectionWithCandidate(ch, "2.2.2.2", 2, 9, true);
2517 ASSERT_TRUE(conn2 != nullptr);
2518 cricket::Connection* conn3 =
2519 CreateConnectionWithCandidate(ch, "3.3.3.3", 3, 8, true);
2520 ASSERT_TRUE(conn3 != nullptr);
2521 cricket::Connection* conn4 =
2522 CreateConnectionWithCandidate(ch, "4.4.4.4", 4, 7, false);
2523 ASSERT_TRUE(conn4 != nullptr);
2524
2525 // Simulate the aggressive nomination on the controlling side.
pthatcher1 2016/06/15 21:59:53 the aggressive nomination => aggressive nomination
honghaiz3 2016/06/16 23:03:49 Done.
2526 reset_num_selected_candidate_pair_switch();
2527 NominateConnection(conn2);
2528 EXPECT_EQ(1, num_selected_candidate_pair_switch());
2529 EXPECT_EQ(conn2, last_selected_candidate_pair());
2530 reset_num_selected_candidate_pair_switch();
pthatcher1 2016/06/15 21:59:53 Why not have reset return the last value. Then yo
honghaiz3 2016/06/17 19:18:18 Done.
2531
2532 // Selected candidate pair will switch because conn1 has higher priority.
2533 NominateConnection(conn1);
2534 EXPECT_EQ(1, num_selected_candidate_pair_switch());
2535 EXPECT_EQ(conn1, last_selected_candidate_pair());
2536 reset_num_selected_candidate_pair_switch();
2537
2538 // Selected candidate pair will not switch because conn2 has lower priority.
2539 NominateConnection(conn2);
2540 EXPECT_EQ(0, num_selected_candidate_pair_switch());
2541 reset_num_selected_candidate_pair_switch();
2542
2543 // Selected candidate pair will switch if it is nominated again.
pthatcher1 2016/06/15 21:59:53 I don't think we should do this one, because an ag
honghaiz3 2016/06/16 01:13:08 If we do not do this, how do we handle the case th
2544 NominateConnection(conn2);
2545 EXPECT_EQ(1, num_selected_candidate_pair_switch());
2546 EXPECT_EQ(conn2, last_selected_candidate_pair());
2547 reset_num_selected_candidate_pair_switch();
2548
2549 // Selected candidate pair won't switch because conn3 has lower priority
pthatcher1 2016/06/15 21:59:53 Can you add "until conn3 receives data"?
honghaiz3 2016/06/16 23:03:49 Done.
2550 NominateConnection(conn3);
2551 EXPECT_EQ(0, num_selected_candidate_pair_switch());
2552 // Will switch if conn3 receives data.
2553 conn3->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0));
2554 EXPECT_EQ(1, num_selected_candidate_pair_switch());
2555 EXPECT_EQ(conn3, last_selected_candidate_pair());
2556 reset_num_selected_candidate_pair_switch();
2557
2558 // Selected candidate pair won't switch even if it is nominated and
2559 // received data because it is not writable.
2560 NominateConnection(conn4);
2561 conn4->OnReadPacket("XYZ", 3, rtc::CreatePacketTime(0));
2562 EXPECT_EQ(0, num_selected_candidate_pair_switch());
2563 // It will switch if it becomes writable
2564 conn4->ReceivedPingResponse();
2565 EXPECT_EQ(1, num_selected_candidate_pair_switch());
2566 EXPECT_EQ(conn4, last_selected_candidate_pair());
2567 reset_num_selected_candidate_pair_switch();
2568
2569 // Make sure sorting won't reselect candidate pair.
2570 SIMULATED_WAIT(false, 10, clock);
2571 EXPECT_EQ(0, num_selected_candidate_pair_switch());
2572 }
2573
2476 // Test that if a new remote candidate has the same address and port with 2574 // Test that if a new remote candidate has the same address and port with
2477 // an old one, it will be used to create a new connection. 2575 // an old one, it will be used to create a new connection.
2478 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { 2576 TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) {
2479 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr); 2577 cricket::FakePortAllocator pa(rtc::Thread::Current(), nullptr);
2480 cricket::P2PTransportChannel ch("candidate reuse", 1, &pa); 2578 cricket::P2PTransportChannel ch("candidate reuse", 1, &pa);
2481 PrepareChannel(&ch); 2579 PrepareChannel(&ch);
2482 ch.Connect(); 2580 ch.Connect();
2483 ch.MaybeStartGathering(); 2581 ch.MaybeStartGathering();
2484 const std::string host_address = "1.1.1.1"; 2582 const std::string host_address = "1.1.1.1";
2485 const int port_num = 1; 2583 const int port_num = 1;
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after
2916 3014
2917 // TCP Relay/Relay is the next. 3015 // TCP Relay/Relay is the next.
2918 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE, 3016 VerifyNextPingableConnection(cricket::RELAY_PORT_TYPE,
2919 cricket::RELAY_PORT_TYPE, 3017 cricket::RELAY_PORT_TYPE,
2920 cricket::TCP_PROTOCOL_NAME); 3018 cricket::TCP_PROTOCOL_NAME);
2921 3019
2922 // Finally, Local/Relay will be pinged. 3020 // Finally, Local/Relay will be pinged.
2923 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE, 3021 VerifyNextPingableConnection(cricket::LOCAL_PORT_TYPE,
2924 cricket::RELAY_PORT_TYPE); 3022 cricket::RELAY_PORT_TYPE);
2925 } 3023 }
OLDNEW
« webrtc/p2p/base/p2ptransportchannel.cc ('K') | « webrtc/p2p/base/p2ptransportchannel.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698