From 5bf9def61b916477898dd1d33f0f34b2c8df7056 Mon Sep 17 00:00:00 2001 From: hbos Date: Mon, 20 Mar 2017 03:14:14 -0700 Subject: [PATCH] RTCStatsCollector: Remove closed channels from opened set. This is a problem if a data channel is re-opened or a new data channel occupies the same space in memory as a previously closed data channel. Unittest updated to cover this (failed before fix, now passes). BUG=webrtc:7181 Review-Url: https://codereview.webrtc.org/2746393003 Cr-Commit-Position: refs/heads/master@{#17304} --- webrtc/pc/rtcstatscollector.cc | 5 ++-- webrtc/pc/rtcstatscollector_unittest.cc | 33 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/webrtc/pc/rtcstatscollector.cc b/webrtc/pc/rtcstatscollector.cc index 347bca53ea..d9da0729ed 100644 --- a/webrtc/pc/rtcstatscollector.cc +++ b/webrtc/pc/rtcstatscollector.cc @@ -1235,9 +1235,8 @@ void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) { RTC_DCHECK(signaling_thread_->IsCurrent()); // Only channels that have been fully opened (and have increased the // |data_channels_opened_| counter) increase the closed counter. - if (internal_record_.opened_data_channels.find( - reinterpret_cast(channel)) != - internal_record_.opened_data_channels.end()) { + if (internal_record_.opened_data_channels.erase( + reinterpret_cast(channel))) { ++internal_record_.data_channels_closed; } } diff --git a/webrtc/pc/rtcstatscollector_unittest.cc b/webrtc/pc/rtcstatscollector_unittest.cc index ea7b34a933..8fd00c6178 100644 --- a/webrtc/pc/rtcstatscollector_unittest.cc +++ b/webrtc/pc/rtcstatscollector_unittest.cc @@ -1463,6 +1463,39 @@ TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { report->Get("RTCPeerConnection")->cast_to< RTCPeerConnectionStats>()); } + + // Re-opening a data channel (or opening a new data channel that is re-using + // the same address in memory) should increase the opened count. + dummy_channel_b->SignalOpened(dummy_channel_b.get()); + + { + collector_->ClearCachedStatsReport(); + rtc::scoped_refptr report = GetStatsReport(); + RTCPeerConnectionStats expected("RTCPeerConnection", + report->timestamp_us()); + expected.data_channels_opened = 3; + expected.data_channels_closed = 1; + ASSERT_TRUE(report->Get("RTCPeerConnection")); + EXPECT_EQ(expected, + report->Get("RTCPeerConnection")->cast_to< + RTCPeerConnectionStats>()); + } + + dummy_channel_a->SignalClosed(dummy_channel_a.get()); + dummy_channel_b->SignalClosed(dummy_channel_b.get()); + + { + collector_->ClearCachedStatsReport(); + rtc::scoped_refptr report = GetStatsReport(); + RTCPeerConnectionStats expected("RTCPeerConnection", + report->timestamp_us()); + expected.data_channels_opened = 3; + expected.data_channels_closed = 3; + ASSERT_TRUE(report->Get("RTCPeerConnection")); + EXPECT_EQ(expected, + report->Get("RTCPeerConnection")->cast_to< + RTCPeerConnectionStats>()); + } } TEST_F(RTCStatsCollectorTest,