diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc index 557f2e6899..11004ba743 100644 --- a/pc/peer_connection.cc +++ b/pc/peer_connection.cc @@ -2000,6 +2000,7 @@ void PeerConnection::OnIceCandidate( return; } ReportIceCandidateCollected(candidate->candidate()); + ClearStatsCache(); Observer()->OnIceCandidate(candidate.get()); } diff --git a/pc/peer_connection_integrationtest.cc b/pc/peer_connection_integrationtest.cc index 9811392bbd..d6ac0c82c9 100644 --- a/pc/peer_connection_integrationtest.cc +++ b/pc/peer_connection_integrationtest.cc @@ -2418,6 +2418,38 @@ TEST_P(PeerConnectionIntegrationTestWithFakeClock, ClosePeerConnections(); } +TEST_P(PeerConnectionIntegrationTestWithFakeClock, + OnIceCandidateFlushesGetStatsCache) { + ASSERT_TRUE(CreatePeerConnectionWrappers()); + ConnectFakeSignaling(); + caller()->AddAudioTrack(); + + // Call getStats, assert there are no candidates. + rtc::scoped_refptr first_report = + caller()->NewGetStats(); + ASSERT_TRUE(first_report); + auto first_candidate_stats = + first_report->GetStatsOfType(); + ASSERT_EQ(first_candidate_stats.size(), 0u); + + // Start candidate gathering and wait for it to complete. + caller()->CreateAndSetAndSignalOffer(); + ASSERT_TRUE_SIMULATED_WAIT(caller()->IceGatheringStateComplete(), + kDefaultTimeout, FakeClock()); + + // Call getStats again, assert there are candidates now. + rtc::scoped_refptr second_report = + caller()->NewGetStats(); + ASSERT_TRUE(second_report); + auto second_candidate_stats = + second_report->GetStatsOfType(); + ASSERT_NE(second_candidate_stats.size(), 0u); + + // The fake clock ensures that no time has passed so the cache must have been + // explicitly invalidated. + EXPECT_EQ(first_report->timestamp_us(), second_report->timestamp_us()); +} + #endif // !defined(THREAD_SANITIZER) // Verify that a TurnCustomizer passed in through RTCConfiguration diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h index 53df9fc478..460148c559 100644 --- a/pc/test/integration_test_helpers.h +++ b/pc/test/integration_test_helpers.h @@ -410,6 +410,11 @@ class PeerConnectionIntegrationWrapper : public webrtc::PeerConnectionObserver, return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable; } + bool IceGatheringStateComplete() { + return pc()->ice_gathering_state() == + webrtc::PeerConnectionInterface::kIceGatheringComplete; + } + void CreateDataChannel() { CreateDataChannel(nullptr); } void CreateDataChannel(const webrtc::DataChannelInit* init) {