hbos | d565b73 | 2016-08-30 14:04:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/stats/rtcstatscollector.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "webrtc/api/jsepsessiondescription.h" |
| 18 | #include "webrtc/api/rtcstats_objects.h" |
| 19 | #include "webrtc/api/rtcstatsreport.h" |
| 20 | #include "webrtc/api/test/mock_datachannel.h" |
| 21 | #include "webrtc/api/test/mock_peerconnection.h" |
| 22 | #include "webrtc/api/test/mock_webrtcsession.h" |
| 23 | #include "webrtc/base/checks.h" |
| 24 | #include "webrtc/base/gunit.h" |
| 25 | #include "webrtc/base/logging.h" |
| 26 | #include "webrtc/base/test/faketiming.h" |
| 27 | #include "webrtc/media/base/fakemediaengine.h" |
| 28 | |
| 29 | using testing::Return; |
| 30 | using testing::ReturnRef; |
| 31 | |
| 32 | namespace webrtc { |
| 33 | |
| 34 | class RTCStatsCollectorTester : public SetSessionDescriptionObserver { |
| 35 | public: |
| 36 | RTCStatsCollectorTester() |
| 37 | : worker_thread_(rtc::Thread::Current()), |
| 38 | network_thread_(rtc::Thread::Current()), |
| 39 | channel_manager_(new cricket::ChannelManager( |
| 40 | new cricket::FakeMediaEngine(), |
| 41 | worker_thread_, |
| 42 | network_thread_)), |
| 43 | media_controller_( |
| 44 | MediaControllerInterface::Create(cricket::MediaConfig(), |
| 45 | worker_thread_, |
| 46 | channel_manager_.get())), |
| 47 | session_(media_controller_.get()), |
| 48 | pc_() { |
| 49 | EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); |
| 50 | EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( |
| 51 | ReturnRef(data_channels_)); |
| 52 | } |
| 53 | |
| 54 | MockWebRtcSession& session() { return session_; } |
| 55 | MockPeerConnection& pc() { return pc_; } |
| 56 | std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { |
| 57 | return data_channels_; |
| 58 | } |
| 59 | |
| 60 | // SetSessionDescriptionObserver overrides. |
| 61 | void OnSuccess() override {} |
| 62 | void OnFailure(const std::string& error) override { |
| 63 | RTC_NOTREACHED() << error; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | rtc::Thread* const worker_thread_; |
| 68 | rtc::Thread* const network_thread_; |
| 69 | std::unique_ptr<cricket::ChannelManager> channel_manager_; |
| 70 | std::unique_ptr<webrtc::MediaControllerInterface> media_controller_; |
| 71 | MockWebRtcSession session_; |
| 72 | MockPeerConnection pc_; |
| 73 | |
| 74 | std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; |
| 75 | }; |
| 76 | |
| 77 | class RTCStatsCollectorTest : public testing::Test { |
| 78 | public: |
| 79 | RTCStatsCollectorTest() |
| 80 | : test_(new rtc::RefCountedObject<RTCStatsCollectorTester>()), |
| 81 | timing_(new rtc::FakeTiming()), |
| 82 | collector_(&test_->pc(), 0.05, std::unique_ptr<rtc::Timing>(timing_)) { |
| 83 | } |
| 84 | |
| 85 | protected: |
| 86 | rtc::scoped_refptr<RTCStatsCollectorTester> test_; |
| 87 | rtc::FakeTiming* timing_; // Owned by |collector_|. |
| 88 | RTCStatsCollector collector_; |
| 89 | }; |
| 90 | |
| 91 | TEST_F(RTCStatsCollectorTest, CachedStatsReport) { |
| 92 | // Caching should ensure |a| and |b| are the same report. |
| 93 | rtc::scoped_refptr<const RTCStatsReport> a = collector_.GetStatsReport(); |
| 94 | rtc::scoped_refptr<const RTCStatsReport> b = collector_.GetStatsReport(); |
| 95 | EXPECT_TRUE(a); |
| 96 | EXPECT_EQ(a.get(), b.get()); |
| 97 | // Invalidate cache by clearing it. |
| 98 | collector_.ClearCachedStatsReport(); |
| 99 | rtc::scoped_refptr<const RTCStatsReport> c = collector_.GetStatsReport(); |
| 100 | EXPECT_TRUE(c); |
| 101 | EXPECT_NE(b.get(), c.get()); |
| 102 | // Invalidate cache by advancing time. |
| 103 | timing_->AdvanceTimeMillisecs(51.0); |
| 104 | rtc::scoped_refptr<const RTCStatsReport> d = collector_.GetStatsReport(); |
| 105 | EXPECT_TRUE(d); |
| 106 | EXPECT_NE(c.get(), d.get()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
| 110 | rtc::scoped_refptr<const RTCStatsReport> report = collector_.GetStatsReport(); |
| 111 | EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), |
| 112 | static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; |
| 113 | const RTCStats* stats = report->Get("RTCPeerConnection"); |
| 114 | EXPECT_TRUE(stats); |
| 115 | EXPECT_EQ(stats->timestamp(), timing_->TimerNow()); |
| 116 | { |
| 117 | // Expected stats with no data channels |
| 118 | const RTCPeerConnectionStats& pcstats = |
| 119 | stats->cast_to<RTCPeerConnectionStats>(); |
| 120 | EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0)); |
| 121 | EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0)); |
| 122 | } |
| 123 | |
| 124 | test_->data_channels().push_back( |
| 125 | new MockDataChannel(DataChannelInterface::kConnecting)); |
| 126 | test_->data_channels().push_back( |
| 127 | new MockDataChannel(DataChannelInterface::kOpen)); |
| 128 | test_->data_channels().push_back( |
| 129 | new MockDataChannel(DataChannelInterface::kClosing)); |
| 130 | test_->data_channels().push_back( |
| 131 | new MockDataChannel(DataChannelInterface::kClosed)); |
| 132 | |
| 133 | collector_.ClearCachedStatsReport(); |
| 134 | report = collector_.GetStatsReport(); |
| 135 | EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), |
| 136 | static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; |
| 137 | stats = report->Get("RTCPeerConnection"); |
| 138 | EXPECT_TRUE(stats); |
| 139 | { |
| 140 | // Expected stats with the above four data channels |
| 141 | // TODO(hbos): When the |RTCPeerConnectionStats| is the number of data |
| 142 | // channels that have been opened and closed, not the numbers currently |
| 143 | // open/closed, we would expect opened >= closed and (opened - closed) to be |
| 144 | // the number currently open. crbug.com/636818. |
| 145 | const RTCPeerConnectionStats& pcstats = |
| 146 | stats->cast_to<RTCPeerConnectionStats>(); |
| 147 | EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1)); |
| 148 | EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } // namespace webrtc |