| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2016 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 |
| 11 #include "webrtc/stats/rtcstatscollector.h" | 11 #include "webrtc/stats/rtcstatscollector.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "webrtc/api/jsepsessiondescription.h" | 17 #include "webrtc/api/jsepsessiondescription.h" |
| 18 #include "webrtc/api/rtcstats_objects.h" | 18 #include "webrtc/api/rtcstats_objects.h" |
| 19 #include "webrtc/api/rtcstatsreport.h" | 19 #include "webrtc/api/rtcstatsreport.h" |
| 20 #include "webrtc/api/test/mock_datachannel.h" | 20 #include "webrtc/api/test/mock_datachannel.h" |
| 21 #include "webrtc/api/test/mock_peerconnection.h" | 21 #include "webrtc/api/test/mock_peerconnection.h" |
| 22 #include "webrtc/api/test/mock_webrtcsession.h" | 22 #include "webrtc/api/test/mock_webrtcsession.h" |
| 23 #include "webrtc/base/checks.h" | 23 #include "webrtc/base/checks.h" |
| 24 #include "webrtc/base/fakeclock.h" |
| 24 #include "webrtc/base/gunit.h" | 25 #include "webrtc/base/gunit.h" |
| 25 #include "webrtc/base/logging.h" | 26 #include "webrtc/base/logging.h" |
| 26 #include "webrtc/base/test/faketiming.h" | 27 #include "webrtc/base/timedelta.h" |
| 28 #include "webrtc/base/timeutils.h" |
| 29 #include "webrtc/base/timing.h" |
| 27 #include "webrtc/media/base/fakemediaengine.h" | 30 #include "webrtc/media/base/fakemediaengine.h" |
| 28 | 31 |
| 29 using testing::Return; | 32 using testing::Return; |
| 30 using testing::ReturnRef; | 33 using testing::ReturnRef; |
| 31 | 34 |
| 32 namespace webrtc { | 35 namespace webrtc { |
| 33 | 36 |
| 34 class RTCStatsCollectorTester : public SetSessionDescriptionObserver { | 37 class RTCStatsCollectorTester : public SetSessionDescriptionObserver { |
| 35 public: | 38 public: |
| 36 RTCStatsCollectorTester() | 39 RTCStatsCollectorTester() |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 MockWebRtcSession session_; | 74 MockWebRtcSession session_; |
| 72 MockPeerConnection pc_; | 75 MockPeerConnection pc_; |
| 73 | 76 |
| 74 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; | 77 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; |
| 75 }; | 78 }; |
| 76 | 79 |
| 77 class RTCStatsCollectorTest : public testing::Test { | 80 class RTCStatsCollectorTest : public testing::Test { |
| 78 public: | 81 public: |
| 79 RTCStatsCollectorTest() | 82 RTCStatsCollectorTest() |
| 80 : test_(new rtc::RefCountedObject<RTCStatsCollectorTester>()), | 83 : test_(new rtc::RefCountedObject<RTCStatsCollectorTester>()), |
| 81 timing_(new rtc::FakeTiming()), | 84 collector_(&test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec) { |
| 82 collector_(&test_->pc(), 0.05, std::unique_ptr<rtc::Timing>(timing_)) { | |
| 83 } | 85 } |
| 84 | 86 |
| 85 protected: | 87 protected: |
| 86 rtc::scoped_refptr<RTCStatsCollectorTester> test_; | 88 rtc::scoped_refptr<RTCStatsCollectorTester> test_; |
| 87 rtc::FakeTiming* timing_; // Owned by |collector_|. | |
| 88 RTCStatsCollector collector_; | 89 RTCStatsCollector collector_; |
| 89 }; | 90 }; |
| 90 | 91 |
| 91 TEST_F(RTCStatsCollectorTest, CachedStatsReport) { | 92 TEST_F(RTCStatsCollectorTest, CachedStatsReport) { |
| 93 rtc::ScopedFakeClock fake_clock; |
| 92 // Caching should ensure |a| and |b| are the same report. | 94 // Caching should ensure |a| and |b| are the same report. |
| 93 rtc::scoped_refptr<const RTCStatsReport> a = collector_.GetStatsReport(); | 95 rtc::scoped_refptr<const RTCStatsReport> a = collector_.GetStatsReport(); |
| 94 rtc::scoped_refptr<const RTCStatsReport> b = collector_.GetStatsReport(); | 96 rtc::scoped_refptr<const RTCStatsReport> b = collector_.GetStatsReport(); |
| 95 EXPECT_TRUE(a); | 97 EXPECT_TRUE(a); |
| 96 EXPECT_EQ(a.get(), b.get()); | 98 EXPECT_EQ(a.get(), b.get()); |
| 97 // Invalidate cache by clearing it. | 99 // Invalidate cache by clearing it. |
| 98 collector_.ClearCachedStatsReport(); | 100 collector_.ClearCachedStatsReport(); |
| 99 rtc::scoped_refptr<const RTCStatsReport> c = collector_.GetStatsReport(); | 101 rtc::scoped_refptr<const RTCStatsReport> c = collector_.GetStatsReport(); |
| 100 EXPECT_TRUE(c); | 102 EXPECT_TRUE(c); |
| 101 EXPECT_NE(b.get(), c.get()); | 103 EXPECT_NE(b.get(), c.get()); |
| 102 // Invalidate cache by advancing time. | 104 // Invalidate cache by advancing time. |
| 103 timing_->AdvanceTimeMillisecs(51.0); | 105 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
| 104 rtc::scoped_refptr<const RTCStatsReport> d = collector_.GetStatsReport(); | 106 rtc::scoped_refptr<const RTCStatsReport> d = collector_.GetStatsReport(); |
| 105 EXPECT_TRUE(d); | 107 EXPECT_TRUE(d); |
| 106 EXPECT_NE(c.get(), d.get()); | 108 EXPECT_NE(c.get(), d.get()); |
| 107 } | 109 } |
| 108 | 110 |
| 109 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { | 111 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
| 112 int64_t before = static_cast<int64_t>( |
| 113 rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); |
| 110 rtc::scoped_refptr<const RTCStatsReport> report = collector_.GetStatsReport(); | 114 rtc::scoped_refptr<const RTCStatsReport> report = collector_.GetStatsReport(); |
| 115 int64_t after = static_cast<int64_t>( |
| 116 rtc::Timing::WallTimeNow() * rtc::kNumMicrosecsPerSec); |
| 111 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), | 117 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), |
| 112 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; | 118 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; |
| 113 const RTCStats* stats = report->Get("RTCPeerConnection"); | 119 const RTCStats* stats = report->Get("RTCPeerConnection"); |
| 114 EXPECT_TRUE(stats); | 120 EXPECT_TRUE(stats); |
| 115 EXPECT_EQ(stats->timestamp(), timing_->TimerNow()); | 121 EXPECT_LE(before, stats->timestamp_us()); |
| 122 EXPECT_LE(stats->timestamp_us(), after); |
| 116 { | 123 { |
| 117 // Expected stats with no data channels | 124 // Expected stats with no data channels |
| 118 const RTCPeerConnectionStats& pcstats = | 125 const RTCPeerConnectionStats& pcstats = |
| 119 stats->cast_to<RTCPeerConnectionStats>(); | 126 stats->cast_to<RTCPeerConnectionStats>(); |
| 120 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0)); | 127 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0)); |
| 121 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0)); | 128 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0)); |
| 122 } | 129 } |
| 123 | 130 |
| 124 test_->data_channels().push_back( | 131 test_->data_channels().push_back( |
| 125 new MockDataChannel(DataChannelInterface::kConnecting)); | 132 new MockDataChannel(DataChannelInterface::kConnecting)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 143 // open/closed, we would expect opened >= closed and (opened - closed) to be | 150 // open/closed, we would expect opened >= closed and (opened - closed) to be |
| 144 // the number currently open. crbug.com/636818. | 151 // the number currently open. crbug.com/636818. |
| 145 const RTCPeerConnectionStats& pcstats = | 152 const RTCPeerConnectionStats& pcstats = |
| 146 stats->cast_to<RTCPeerConnectionStats>(); | 153 stats->cast_to<RTCPeerConnectionStats>(); |
| 147 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1)); | 154 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1)); |
| 148 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3)); | 155 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3)); |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 | 158 |
| 152 } // namespace webrtc | 159 } // namespace webrtc |
| OLD | NEW |