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

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

Issue 2171183002: Remove ports that are not used by any channel after timeout (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: . Created 4 years, 4 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
« no previous file with comments | « webrtc/p2p/base/port.cc ('k') | webrtc/p2p/base/portallocator.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 366 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 relay_server_(main_, 377 relay_server_(main_,
378 kRelayUdpIntAddr, 378 kRelayUdpIntAddr,
379 kRelayUdpExtAddr, 379 kRelayUdpExtAddr,
380 kRelayTcpIntAddr, 380 kRelayTcpIntAddr,
381 kRelayTcpExtAddr, 381 kRelayTcpExtAddr,
382 kRelaySslTcpIntAddr, 382 kRelaySslTcpIntAddr,
383 kRelaySslTcpExtAddr), 383 kRelaySslTcpExtAddr),
384 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)), 384 username_(rtc::CreateRandomString(ICE_UFRAG_LENGTH)),
385 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)), 385 password_(rtc::CreateRandomString(ICE_PWD_LENGTH)),
386 role_conflict_(false), 386 role_conflict_(false),
387 destroyed_(false) { 387 ports_destroyed_(0) {
388 network_.AddIP(rtc::IPAddress(INADDR_ANY)); 388 network_.AddIP(rtc::IPAddress(INADDR_ANY));
389 } 389 }
390 390
391 protected: 391 protected:
392 void TestLocalToLocal() { 392 void TestLocalToLocal() {
393 Port* port1 = CreateUdpPort(kLocalAddr1); 393 Port* port1 = CreateUdpPort(kLocalAddr1);
394 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 394 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
395 Port* port2 = CreateUdpPort(kLocalAddr2); 395 Port* port2 = CreateUdpPort(kLocalAddr2);
396 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); 396 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
397 TestConnectivity("udp", port1, "udp", port2, true, true, true, true); 397 TestConnectivity("udp", port1, "udp", port2, true, true, true, true);
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 748
749 void OnRoleConflict(PortInterface* port) { 749 void OnRoleConflict(PortInterface* port) {
750 role_conflict_ = true; 750 role_conflict_ = true;
751 } 751 }
752 bool role_conflict() const { return role_conflict_; } 752 bool role_conflict() const { return role_conflict_; }
753 753
754 void ConnectToSignalDestroyed(PortInterface* port) { 754 void ConnectToSignalDestroyed(PortInterface* port) {
755 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed); 755 port->SignalDestroyed.connect(this, &PortTest::OnDestroyed);
756 } 756 }
757 757
758 void OnDestroyed(PortInterface* port) { 758 void OnDestroyed(PortInterface* port) { ++ports_destroyed_; }
759 destroyed_ = true; 759 int ports_destroyed() const { return ports_destroyed_; }
760 }
761 bool destroyed() const { return destroyed_; }
762 760
763 rtc::BasicPacketSocketFactory* nat_socket_factory1() { 761 rtc::BasicPacketSocketFactory* nat_socket_factory1() {
764 return &nat_socket_factory1_; 762 return &nat_socket_factory1_;
765 } 763 }
766 764
767 rtc::VirtualSocketServer* vss() { return ss_.get(); } 765 rtc::VirtualSocketServer* vss() { return ss_.get(); }
768 766
769 private: 767 private:
770 rtc::Thread* main_; 768 rtc::Thread* main_;
771 std::unique_ptr<rtc::PhysicalSocketServer> pss_; 769 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
772 std::unique_ptr<rtc::VirtualSocketServer> ss_; 770 std::unique_ptr<rtc::VirtualSocketServer> ss_;
773 rtc::SocketServerScope ss_scope_; 771 rtc::SocketServerScope ss_scope_;
774 rtc::Network network_; 772 rtc::Network network_;
775 rtc::BasicPacketSocketFactory socket_factory_; 773 rtc::BasicPacketSocketFactory socket_factory_;
776 std::unique_ptr<rtc::NATServer> nat_server1_; 774 std::unique_ptr<rtc::NATServer> nat_server1_;
777 std::unique_ptr<rtc::NATServer> nat_server2_; 775 std::unique_ptr<rtc::NATServer> nat_server2_;
778 rtc::NATSocketFactory nat_factory1_; 776 rtc::NATSocketFactory nat_factory1_;
779 rtc::NATSocketFactory nat_factory2_; 777 rtc::NATSocketFactory nat_factory2_;
780 rtc::BasicPacketSocketFactory nat_socket_factory1_; 778 rtc::BasicPacketSocketFactory nat_socket_factory1_;
781 rtc::BasicPacketSocketFactory nat_socket_factory2_; 779 rtc::BasicPacketSocketFactory nat_socket_factory2_;
782 std::unique_ptr<TestStunServer> stun_server_; 780 std::unique_ptr<TestStunServer> stun_server_;
783 TestTurnServer turn_server_; 781 TestTurnServer turn_server_;
784 TestRelayServer relay_server_; 782 TestRelayServer relay_server_;
785 std::string username_; 783 std::string username_;
786 std::string password_; 784 std::string password_;
787 bool role_conflict_; 785 bool role_conflict_;
788 bool destroyed_; 786 int ports_destroyed_;
789 }; 787 };
790 788
791 void PortTest::TestConnectivity(const char* name1, Port* port1, 789 void PortTest::TestConnectivity(const char* name1, Port* port1,
792 const char* name2, Port* port2, 790 const char* name2, Port* port2,
793 bool accept, bool same_addr1, 791 bool accept, bool same_addr1,
794 bool same_addr2, bool possible) { 792 bool same_addr2, bool possible) {
795 LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": "; 793 LOG(LS_INFO) << "Test: " << name1 << " to " << name2 << ": ";
796 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); 794 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
797 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); 795 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
798 796
(...skipping 1761 matching lines...) Expand 10 before | Expand all | Expand 10 after
2560 // message right after we send ping. 2558 // message right after we send ping.
2561 ice_full_port->Reset(); 2559 ice_full_port->Reset();
2562 // Send ping. This must have USE_CANDIDATE_ATTR. 2560 // Send ping. This must have USE_CANDIDATE_ATTR.
2563 ch1.Ping(); 2561 ch1.Ping();
2564 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000); 2562 ASSERT_TRUE_WAIT(ice_full_port->last_stun_msg() != NULL, 1000);
2565 msg = ice_full_port->last_stun_msg(); 2563 msg = ice_full_port->last_stun_msg();
2566 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL); 2564 EXPECT_TRUE(msg->GetByteString(STUN_ATTR_USE_CANDIDATE) != NULL);
2567 ch1.Stop(); 2565 ch1.Stop();
2568 } 2566 }
2569 2567
2570 // This test case verifies that the CONTROLLING port does not time out. 2568 // This test case verifies that both the controlling port and the controlled
2571 TEST_F(PortTest, TestControllingNoTimeout) { 2569 // port will time out after connectivity is lost, if they are not marked as
2570 // "keep alive until pruned."
2571 TEST_F(PortTest, TestPortTimeoutIfNotKeptAlive) {
2572 rtc::ScopedFakeClock clock;
2573 int timeout_delay = 100;
2572 UDPPort* port1 = CreateUdpPort(kLocalAddr1); 2574 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2573 ConnectToSignalDestroyed(port1); 2575 ConnectToSignalDestroyed(port1);
2574 port1->set_timeout_delay(10); // milliseconds 2576 port1->set_timeout_delay(timeout_delay); // milliseconds
2575 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 2577 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2576 port1->SetIceTiebreaker(kTiebreaker1); 2578 port1->SetIceTiebreaker(kTiebreaker1);
2577 2579
2578 UDPPort* port2 = CreateUdpPort(kLocalAddr2); 2580 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2581 ConnectToSignalDestroyed(port2);
2582 port2->set_timeout_delay(timeout_delay); // milliseconds
2579 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); 2583 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2580 port2->SetIceTiebreaker(kTiebreaker2); 2584 port2->SetIceTiebreaker(kTiebreaker2);
2581 2585
2582 // Set up channels and ensure both ports will be deleted. 2586 // Set up channels and ensure both ports will be deleted.
2583 TestChannel ch1(port1); 2587 TestChannel ch1(port1);
2584 TestChannel ch2(port2); 2588 TestChannel ch2(port2);
2585 2589
2586 // Simulate a connection that succeeds, and then is destroyed. 2590 // Simulate a connection that succeeds, and then is destroyed.
2587 StartConnectAndStopChannels(&ch1, &ch2); 2591 StartConnectAndStopChannels(&ch1, &ch2);
2588 2592 // After the connection is destroyed, the port will be destroyed because
2589 // After the connection is destroyed, the port should not be destroyed. 2593 // none of them is marked as "keep alive until pruned.
2590 rtc::Thread::Current()->ProcessMessages(kTimeout); 2594 EXPECT_EQ_SIMULATED_WAIT(2, ports_destroyed(), 110, clock);
2591 EXPECT_FALSE(destroyed());
2592 } 2595 }
2593 2596
2594 // This test case verifies that the CONTROLLED port does time out, but only 2597 // Test that if after all connection are destroyed, new connections are created
2595 // after connectivity is lost and no connection was created during the timeout 2598 // and destroyed again, ports won't be destroyed until a timeout period passes
2596 // period. 2599 // after the last set of connections are all destroyed.
2597 TEST_F(PortTest, TestControlledTimeout) { 2600 TEST_F(PortTest, TestPortTimeoutAfterNewConnectionCreatedAndDestroyed) {
2598 rtc::ScopedFakeClock clock; 2601 rtc::ScopedFakeClock clock;
2602 int timeout_delay = 100;
2599 UDPPort* port1 = CreateUdpPort(kLocalAddr1); 2603 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2604 ConnectToSignalDestroyed(port1);
2605 port1->set_timeout_delay(timeout_delay); // milliseconds
2600 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 2606 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2601 port1->SetIceTiebreaker(kTiebreaker1); 2607 port1->SetIceTiebreaker(kTiebreaker1);
2602 2608
2603 UDPPort* port2 = CreateUdpPort(kLocalAddr2); 2609 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2604 ConnectToSignalDestroyed(port2); 2610 ConnectToSignalDestroyed(port2);
2605 port2->set_timeout_delay(100); // milliseconds 2611 port2->set_timeout_delay(timeout_delay); // milliseconds
2612
2606 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); 2613 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2607 port2->SetIceTiebreaker(kTiebreaker2); 2614 port2->SetIceTiebreaker(kTiebreaker2);
2608 2615
2609 // The connection must not be destroyed before a connection is attempted.
2610 EXPECT_FALSE(destroyed());
2611
2612 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2613 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2614
2615 // Set up channels and ensure both ports will be deleted. 2616 // Set up channels and ensure both ports will be deleted.
2616 TestChannel ch1(port1); 2617 TestChannel ch1(port1);
2617 TestChannel ch2(port2); 2618 TestChannel ch2(port2);
2618 2619
2619 // Simulate a connection that succeeds, and then is destroyed. 2620 // Simulate a connection that succeeds, and then is destroyed.
2620 StartConnectAndStopChannels(&ch1, &ch2); 2621 StartConnectAndStopChannels(&ch1, &ch2);
2622 SIMULATED_WAIT(ports_destroyed() > 0, 80, clock);
2623 EXPECT_EQ(0, ports_destroyed());
2621 2624
2622 SIMULATED_WAIT(false, 80, clock); 2625 // Start the second set of connection and destroy them.
2626 ch1.CreateConnection(GetCandidate(ch2.port()));
2623 ch2.CreateConnection(GetCandidate(ch1.port())); 2627 ch2.CreateConnection(GetCandidate(ch1.port()));
2628 ch1.Stop();
2629 ch2.Stop();
2624 2630
2625 // ch2 creates a connection so it will not be destroyed. 2631 SIMULATED_WAIT(ports_destroyed() > 0, 80, clock);
2626 SIMULATED_WAIT(destroyed(), 80, clock); 2632 EXPECT_EQ(0, ports_destroyed());
2627 EXPECT_FALSE(destroyed());
2628 2633
2629 // Even if ch2 stops now, it won't be destroyed until 100ms after the 2634 // The ports on both sides should be destroyed after timeout.
2630 // connection is destroyed. 2635 EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 30, clock);
2631 ch2.Stop();
2632 SIMULATED_WAIT(destroyed(), 80, clock);
2633 EXPECT_FALSE(destroyed());
2634
2635 // The controlled port should be destroyed after timeout.
2636 EXPECT_TRUE_SIMULATED_WAIT(destroyed(), 30, clock);
2637 } 2636 }
2638 2637
2639 // This test case verifies that if the role of a port changes from controlled 2638 // This test case verifies that neither the controlling port nor the controlled
2640 // to controlling after all connections fail, the port will not be destroyed. 2639 // port will time out after connectivity is lost if they are marked as "keep
2641 TEST_F(PortTest, TestControlledToControllingNotDestroyed) { 2640 // alive until pruned". They will time out after they are pruned.
2641 TEST_F(PortTest, TestPortNotTimeoutUntilPruned) {
2642 rtc::ScopedFakeClock clock;
2643 int timeout_delay = 100;
2642 UDPPort* port1 = CreateUdpPort(kLocalAddr1); 2644 UDPPort* port1 = CreateUdpPort(kLocalAddr1);
2645 ConnectToSignalDestroyed(port1);
2646 port1->set_timeout_delay(timeout_delay); // milliseconds
2643 port1->SetIceRole(cricket::ICEROLE_CONTROLLING); 2647 port1->SetIceRole(cricket::ICEROLE_CONTROLLING);
2644 port1->SetIceTiebreaker(kTiebreaker1); 2648 port1->SetIceTiebreaker(kTiebreaker1);
2645 2649
2646 UDPPort* port2 = CreateUdpPort(kLocalAddr2); 2650 UDPPort* port2 = CreateUdpPort(kLocalAddr2);
2647 ConnectToSignalDestroyed(port2); 2651 ConnectToSignalDestroyed(port2);
2648 port2->set_timeout_delay(10); // milliseconds 2652 port2->set_timeout_delay(timeout_delay); // milliseconds
2649 port2->SetIceRole(cricket::ICEROLE_CONTROLLED); 2653 port2->SetIceRole(cricket::ICEROLE_CONTROLLED);
2650 port2->SetIceTiebreaker(kTiebreaker2); 2654 port2->SetIceTiebreaker(kTiebreaker2);
2651
2652 // The connection must not be destroyed before a connection is attempted. 2655 // The connection must not be destroyed before a connection is attempted.
2653 EXPECT_FALSE(destroyed()); 2656 EXPECT_EQ(0, ports_destroyed());
2654 2657
2655 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); 2658 port1->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2656 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT); 2659 port2->set_component(cricket::ICE_CANDIDATE_COMPONENT_DEFAULT);
2657 2660
2658 // Set up channels and ensure both ports will be deleted. 2661 // Set up channels and keep the port alive.
2659 TestChannel ch1(port1); 2662 TestChannel ch1(port1);
2660 TestChannel ch2(port2); 2663 TestChannel ch2(port2);
2664 // Simulate a connection that succeeds, and then is destroyed. But ports
2665 // are kept alive. Ports won't be destroyed.
2666 StartConnectAndStopChannels(&ch1, &ch2);
2667 port1->KeepAliveUntilPruned();
2668 port2->KeepAliveUntilPruned();
2669 SIMULATED_WAIT(ports_destroyed() > 0, 150, clock);
2670 EXPECT_EQ(0, ports_destroyed());
2661 2671
2662 // Simulate a connection that succeeds, and then is destroyed. 2672 // If they are pruned now, they will be destroyed right away.
2663 StartConnectAndStopChannels(&ch1, &ch2); 2673 port1->Prune();
2664 // Switch the role after all connections are destroyed. 2674 port2->Prune();
2665 EXPECT_TRUE_WAIT(ch2.conn() == nullptr, kTimeout); 2675 // The ports on both sides should be destroyed after timeout.
2666 port1->SetIceRole(cricket::ICEROLE_CONTROLLED); 2676 EXPECT_TRUE_SIMULATED_WAIT(ports_destroyed() == 2, 1, clock);
2667 port2->SetIceRole(cricket::ICEROLE_CONTROLLING);
2668
2669 // After the connection is destroyed, the port should not be destroyed.
2670 rtc::Thread::Current()->ProcessMessages(kTimeout);
2671 EXPECT_FALSE(destroyed());
2672 } 2677 }
2673 2678
2674 TEST_F(PortTest, TestSupportsProtocol) { 2679 TEST_F(PortTest, TestSupportsProtocol) {
2675 std::unique_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1)); 2680 std::unique_ptr<Port> udp_port(CreateUdpPort(kLocalAddr1));
2676 EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME)); 2681 EXPECT_TRUE(udp_port->SupportsProtocol(UDP_PROTOCOL_NAME));
2677 EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME)); 2682 EXPECT_FALSE(udp_port->SupportsProtocol(TCP_PROTOCOL_NAME));
2678 2683
2679 std::unique_ptr<Port> stun_port( 2684 std::unique_ptr<Port> stun_port(
2680 CreateStunPort(kLocalAddr1, nat_socket_factory1())); 2685 CreateStunPort(kLocalAddr1, nat_socket_factory1()));
2681 EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME)); 2686 EXPECT_TRUE(stun_port->SupportsProtocol(UDP_PROTOCOL_NAME));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2729 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE); 2734 port->CreateConnection(candidate, Port::ORIGIN_MESSAGE);
2730 EXPECT_NE(conn1, conn2); 2735 EXPECT_NE(conn1, conn2);
2731 conn_in_use = port->GetConnection(address); 2736 conn_in_use = port->GetConnection(address);
2732 EXPECT_EQ(conn2, conn_in_use); 2737 EXPECT_EQ(conn2, conn_in_use);
2733 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation()); 2738 EXPECT_EQ(2u, conn_in_use->remote_candidate().generation());
2734 2739
2735 // Make sure the new connection was not deleted. 2740 // Make sure the new connection was not deleted.
2736 rtc::Thread::Current()->ProcessMessages(300); 2741 rtc::Thread::Current()->ProcessMessages(300);
2737 EXPECT_TRUE(port->GetConnection(address) != nullptr); 2742 EXPECT_TRUE(port->GetConnection(address) != nullptr);
2738 } 2743 }
OLDNEW
« no previous file with comments | « webrtc/p2p/base/port.cc ('k') | webrtc/p2p/base/portallocator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698