Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <utility> | |
| 16 #include <vector> | |
| 17 | |
| 18 #include "webrtc/api/jsepsessiondescription.h" | |
| 19 #include "webrtc/api/rtcstats_objects.h" | |
| 20 #include "webrtc/api/rtcstatsreport.h" | |
| 21 #include "webrtc/api/test/mock_datachannel.h" | |
| 22 #include "webrtc/api/test/mock_peerconnection.h" | |
| 23 #include "webrtc/api/test/mock_webrtcsession.h" | |
| 24 #include "webrtc/base/checks.h" | |
| 25 #include "webrtc/base/gunit.h" | |
| 26 #include "webrtc/base/logging.h" | |
| 27 #include "webrtc/base/test/faketiming.h" | |
| 28 #include "webrtc/media/base/fakemediaengine.h" | |
| 29 | |
| 30 using testing::Return; | |
| 31 using testing::ReturnRef; | |
| 32 | |
| 33 namespace webrtc { | |
| 34 | |
| 35 class RTCStatsCollectorTester : public SetSessionDescriptionObserver { | |
| 36 public: | |
| 37 RTCStatsCollectorTester() | |
| 38 : worker_thread_(rtc::Thread::Current()), | |
| 39 network_thread_(rtc::Thread::Current()), | |
| 40 media_engine_(new cricket::FakeMediaEngine()), | |
| 41 channel_manager_(new cricket::ChannelManager(media_engine_, | |
| 42 worker_thread_, | |
| 43 network_thread_)), | |
| 44 media_controller_( | |
| 45 MediaControllerInterface::Create(cricket::MediaConfig(), | |
| 46 worker_thread_, | |
| 47 channel_manager_.get())), | |
| 48 session_(media_controller_.get()), | |
| 49 pc_() { | |
| 50 EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); | |
| 51 EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( | |
| 52 ReturnRef(data_channels_)); | |
| 53 } | |
| 54 | |
| 55 MockWebRtcSession& session() { return session_; } | |
| 56 MockPeerConnection& pc() { return pc_; } | |
| 57 std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { | |
| 58 return data_channels_; | |
| 59 } | |
| 60 | |
| 61 // SetSessionDescriptionObserver overrides. | |
| 62 void OnSuccess() override {} | |
| 63 void OnFailure(const std::string& error) override { | |
| 64 RTC_NOTREACHED() << error; | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 rtc::Thread* const worker_thread_; | |
| 69 rtc::Thread* const network_thread_; | |
| 70 cricket::FakeMediaEngine* media_engine_; | |
| 71 std::unique_ptr<cricket::ChannelManager> channel_manager_; | |
| 72 std::unique_ptr<webrtc::MediaControllerInterface> media_controller_; | |
| 73 MockWebRtcSession session_; | |
| 74 MockPeerConnection pc_; | |
| 75 | |
| 76 std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; | |
| 77 }; | |
| 78 | |
| 79 class RTCStatsCollectorTest : public testing::Test { | |
| 80 public: | |
| 81 RTCStatsCollectorTest() | |
| 82 : test_(new rtc::RefCountedObject<RTCStatsCollectorTester>()), | |
| 83 timing_(new rtc::FakeTiming()), | |
| 84 collector_(&test_->pc(), 50.0, std::unique_ptr<rtc::Timing>(timing_)) { | |
| 85 } | |
| 86 | |
| 87 protected: | |
| 88 rtc::scoped_refptr<RTCStatsCollectorTester> test_; | |
| 89 rtc::FakeTiming* timing_; // Owned by |collector_|. | |
| 90 RTCStatsCollector collector_; | |
| 91 }; | |
| 92 | |
| 93 TEST_F(RTCStatsCollectorTest, CachedStatsReport) { | |
| 94 // Caching should ensure |a| and |b| are the same report. | |
| 95 rtc::scoped_refptr<const RTCStatsReport> a = collector_.GetStatsReport(); | |
| 96 rtc::scoped_refptr<const RTCStatsReport> b = collector_.GetStatsReport(); | |
| 97 EXPECT_EQ(a.get(), b.get()); | |
|
hta-webrtc
2016/08/24 13:02:16
Should you add EXPECT_TRUE(a.get()) too? This set
hbos
2016/08/24 14:28:00
Done.
| |
| 98 // Invalidate cache by clearing it. | |
| 99 collector_.ClearCachedStatsReport(); | |
| 100 rtc::scoped_refptr<const RTCStatsReport> c = collector_.GetStatsReport(); | |
| 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_NE(c.get(), d.get()); | |
| 106 } | |
| 107 | |
| 108 TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { | |
| 109 rtc::scoped_refptr<const RTCStatsReport> report = collector_.GetStatsReport(); | |
| 110 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), | |
| 111 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; | |
| 112 const RTCStats* stats = (*report)["peerConnectionId"]; | |
| 113 EXPECT_TRUE(stats); | |
| 114 { | |
| 115 // Expected stats with no data channels | |
| 116 const RTCPeerConnectionStats& pcstats = | |
| 117 stats->cast_to<RTCPeerConnectionStats>(); | |
| 118 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(0)); | |
| 119 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(0)); | |
| 120 } | |
| 121 | |
| 122 test_->data_channels().push_back( | |
| 123 new MockDataChannel(DataChannelInterface::kConnecting)); | |
| 124 test_->data_channels().push_back( | |
| 125 new MockDataChannel(DataChannelInterface::kOpen)); | |
| 126 test_->data_channels().push_back( | |
| 127 new MockDataChannel(DataChannelInterface::kClosing)); | |
| 128 test_->data_channels().push_back( | |
| 129 new MockDataChannel(DataChannelInterface::kClosed)); | |
| 130 | |
| 131 collector_.ClearCachedStatsReport(); | |
| 132 report = collector_.GetStatsReport(); | |
| 133 EXPECT_EQ(report->GetStatsOfType<RTCPeerConnectionStats>().size(), | |
| 134 static_cast<size_t>(1)) << "Expecting 1 RTCPeerConnectionStats."; | |
| 135 stats = (*report)["peerConnectionId"]; | |
| 136 EXPECT_TRUE(stats); | |
| 137 { | |
| 138 // Expected stats with the above four data channels | |
| 139 const RTCPeerConnectionStats& pcstats = | |
| 140 stats->cast_to<RTCPeerConnectionStats>(); | |
| 141 EXPECT_EQ(*pcstats.data_channels_opened, static_cast<uint32_t>(1)); | |
|
hta-webrtc
2016/08/24 13:02:16
I'd expect "opened" to be larger than "closed", si
hbos
2016/08/24 14:28:00
Done.
| |
| 142 EXPECT_EQ(*pcstats.data_channels_closed, static_cast<uint32_t>(3)); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 } // namespace webrtc | |
| OLD | NEW |