blob: 8205ebedc095dba460baaf6f3932634915ea9000 [file] [log] [blame]
Henrik Boströmb6199362018-03-12 09:27:551/*
2 * Copyright 2018 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
Steve Anton10542f22019-01-11 17:11:0011#include "pc/rtc_stats_traversal.h"
Henrik Boströmb6199362018-03-12 09:27:5512
13#include <memory>
Henrik Boströmb6199362018-03-12 09:27:5514#include <vector>
15
16#include "api/stats/rtcstats_objects.h"
Yves Gerey3e707812018-11-28 15:47:4917#include "test/gtest.h"
Henrik Boströmb6199362018-03-12 09:27:5518
19// This file contains tests for TakeReferencedStats().
20// GetStatsNeighborIds() is tested in rtcstats_integrationtest.cc.
21
22namespace webrtc {
23
Mirko Bonadei6a489f22019-04-09 13:11:1224class RTCStatsTraversalTest : public ::testing::Test {
Henrik Boströmb6199362018-03-12 09:27:5525 public:
26 RTCStatsTraversalTest() {
Philipp Hanckeb81823a2023-01-04 14:17:4227 transport_ = new RTCTransportStats("transport", Timestamp::Zero());
28 candidate_pair_ =
29 new RTCIceCandidatePairStats("candidate-pair", Timestamp::Zero());
30 local_candidate_ =
31 new RTCLocalIceCandidateStats("local-candidate", Timestamp::Zero());
32 remote_candidate_ =
33 new RTCRemoteIceCandidateStats("remote-candidate", Timestamp::Zero());
Philipp Hancke036b3fd2022-10-12 17:12:2334 initial_report_ = RTCStatsReport::Create(Timestamp::Zero());
Henrik Boströmb6199362018-03-12 09:27:5535 initial_report_->AddStats(std::unique_ptr<const RTCStats>(transport_));
36 initial_report_->AddStats(std::unique_ptr<const RTCStats>(candidate_pair_));
37 initial_report_->AddStats(
38 std::unique_ptr<const RTCStats>(local_candidate_));
39 initial_report_->AddStats(
40 std::unique_ptr<const RTCStats>(remote_candidate_));
Philipp Hancke036b3fd2022-10-12 17:12:2341 result_ = RTCStatsReport::Create(Timestamp::Zero());
Henrik Boströmb6199362018-03-12 09:27:5542 }
43
44 void TakeReferencedStats(std::vector<const RTCStats*> start_nodes) {
45 std::vector<std::string> start_ids;
Mirko Bonadei649a4c22019-01-29 09:11:5346 start_ids.reserve(start_nodes.size());
Henrik Boströmb6199362018-03-12 09:27:5547 for (const RTCStats* start_node : start_nodes) {
48 start_ids.push_back(start_node->id());
49 }
Harald Alvestranda6544372023-11-13 09:33:5650 result_ = ::webrtc::TakeReferencedStats(initial_report_, start_ids);
Henrik Boströmb6199362018-03-12 09:27:5551 }
52
53 void EXPECT_VISITED(const RTCStats* stats) {
54 EXPECT_FALSE(initial_report_->Get(stats->id()))
55 << '"' << stats->id()
56 << "\" should be visited but it was not removed from initial report.";
57 EXPECT_TRUE(result_->Get(stats->id()))
58 << '"' << stats->id()
59 << "\" should be visited but it was not added to the resulting report.";
60 }
61
62 void EXPECT_UNVISITED(const RTCStats* stats) {
63 EXPECT_TRUE(initial_report_->Get(stats->id()))
64 << '"' << stats->id()
65 << "\" should not be visited but it was removed from initial report.";
66 EXPECT_FALSE(result_->Get(stats->id()))
67 << '"' << stats->id()
68 << "\" should not be visited but it was added to the resulting report.";
69 }
70
71 protected:
72 rtc::scoped_refptr<RTCStatsReport> initial_report_;
73 rtc::scoped_refptr<RTCStatsReport> result_;
74 // Raw pointers to stats owned by the reports.
75 RTCTransportStats* transport_;
76 RTCIceCandidatePairStats* candidate_pair_;
77 RTCIceCandidateStats* local_candidate_;
78 RTCIceCandidateStats* remote_candidate_;
79};
80
81TEST_F(RTCStatsTraversalTest, NoReachableConnections) {
82 // Everything references transport but transport doesn't reference anything.
83 //
84 // candidate-pair
85 // | | |
86 // v | v
87 // local-candidate | remote-candidate
88 // | | |
89 // v v v
90 // start:transport
91 candidate_pair_->transport_id = "transport";
92 candidate_pair_->local_candidate_id = "local-candidate";
93 candidate_pair_->remote_candidate_id = "remote-candidate";
94 local_candidate_->transport_id = "transport";
95 remote_candidate_->transport_id = "transport";
96 TakeReferencedStats({transport_});
97 EXPECT_VISITED(transport_);
98 EXPECT_UNVISITED(candidate_pair_);
99 EXPECT_UNVISITED(local_candidate_);
100 EXPECT_UNVISITED(remote_candidate_);
101}
102
103TEST_F(RTCStatsTraversalTest, SelfReference) {
104 transport_->rtcp_transport_stats_id = "transport";
105 TakeReferencedStats({transport_});
106 EXPECT_VISITED(transport_);
107 EXPECT_UNVISITED(candidate_pair_);
108 EXPECT_UNVISITED(local_candidate_);
109 EXPECT_UNVISITED(remote_candidate_);
110}
111
112TEST_F(RTCStatsTraversalTest, BogusReference) {
113 transport_->rtcp_transport_stats_id = "bogus-reference";
114 TakeReferencedStats({transport_});
115 EXPECT_VISITED(transport_);
116 EXPECT_UNVISITED(candidate_pair_);
117 EXPECT_UNVISITED(local_candidate_);
118 EXPECT_UNVISITED(remote_candidate_);
119}
120
121TEST_F(RTCStatsTraversalTest, Tree) {
122 // start:candidate-pair
123 // | |
124 // v v
125 // local-candidate remote-candidate
126 // |
127 // v
128 // transport
129 candidate_pair_->local_candidate_id = "local-candidate";
130 candidate_pair_->remote_candidate_id = "remote-candidate";
131 local_candidate_->transport_id = "transport";
132 TakeReferencedStats({candidate_pair_});
133 EXPECT_VISITED(transport_);
134 EXPECT_VISITED(candidate_pair_);
135 EXPECT_VISITED(local_candidate_);
136 EXPECT_VISITED(remote_candidate_);
137}
138
139TEST_F(RTCStatsTraversalTest, MultiplePathsToSameNode) {
140 // start:candidate-pair
141 // | |
142 // v v
143 // local-candidate remote-candidate
144 // | |
145 // v v
146 // transport
147 candidate_pair_->local_candidate_id = "local-candidate";
148 candidate_pair_->remote_candidate_id = "remote-candidate";
149 local_candidate_->transport_id = "transport";
150 remote_candidate_->transport_id = "transport";
151 TakeReferencedStats({candidate_pair_});
152 EXPECT_VISITED(transport_);
153 EXPECT_VISITED(candidate_pair_);
154 EXPECT_VISITED(local_candidate_);
155 EXPECT_VISITED(remote_candidate_);
156}
157
158TEST_F(RTCStatsTraversalTest, CyclicGraph) {
159 // candidate-pair
160 // | ^
161 // v |
162 // start:local-candidate | remote-candidate
163 // | |
164 // v |
165 // transport
166 local_candidate_->transport_id = "transport";
167 transport_->selected_candidate_pair_id = "candidate-pair";
168 candidate_pair_->local_candidate_id = "local-candidate";
169 TakeReferencedStats({local_candidate_});
170 EXPECT_VISITED(transport_);
171 EXPECT_VISITED(candidate_pair_);
172 EXPECT_VISITED(local_candidate_);
173 EXPECT_UNVISITED(remote_candidate_);
174}
175
176TEST_F(RTCStatsTraversalTest, MultipleStarts) {
177 // start:candidate-pair
178 // |
179 // v
180 // local-candidate remote-candidate
181 // |
182 // v
183 // start:transport
184 candidate_pair_->remote_candidate_id = "remote-candidate";
185 local_candidate_->transport_id = "transport";
186 TakeReferencedStats({candidate_pair_, transport_});
187 EXPECT_VISITED(transport_);
188 EXPECT_VISITED(candidate_pair_);
189 EXPECT_UNVISITED(local_candidate_);
190 EXPECT_VISITED(remote_candidate_);
191}
192
193TEST_F(RTCStatsTraversalTest, MultipleStartsLeadingToSameNode) {
194 // candidate-pair
195 //
196 //
197 // start:local-candidate start:remote-candidate
198 // | |
199 // v v
200 // transport
201 local_candidate_->transport_id = "transport";
202 remote_candidate_->transport_id = "transport";
203 TakeReferencedStats({local_candidate_, remote_candidate_});
204 EXPECT_VISITED(transport_);
205 EXPECT_UNVISITED(candidate_pair_);
206 EXPECT_VISITED(local_candidate_);
207 EXPECT_VISITED(remote_candidate_);
208}
209
210} // namespace webrtc