Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Unified Diff: webrtc/call/rtp_demuxer_unittest.cc

Issue 2902823004: Create unit tests for RtpDemuxer (Closed)
Patch Set: . Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« webrtc/call/rtp_demuxer.cc ('K') | « webrtc/call/rtp_demuxer.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/call/rtp_demuxer_unittest.cc
diff --git a/webrtc/call/rtp_demuxer_unittest.cc b/webrtc/call/rtp_demuxer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d5afc5778be0151ce261342353ae2843a55f2c02
--- /dev/null
+++ b/webrtc/call/rtp_demuxer_unittest.cc
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/call/rtp_demuxer.h"
+
+#include <memory>
+
+#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
danilchap 2017/05/24 17:14:02 put it inbetween webrtc/base and webrtc/test (alph
eladalon 2017/05/25 08:00:05 I will explain my reasoning, and if you still pref
danilchap 2017/05/29 08:59:53 I'm not sure why introduce own convention when the
eladalon 2017/05/30 14:23:37 Done.
+
+#include "webrtc/base/arraysize.h"
+#include "webrtc/base/checks.h"
+#include "webrtc/test/gmock.h"
+#include "webrtc/test/gtest.h"
+
+namespace webrtc {
+
+namespace {
+
+class MockRtpPacketSink : public RtpPacketSinkInterface {
+ public:
+ MOCK_METHOD1(OnRtpPacket, void(const RtpPacketReceived&));
+};
+
+constexpr uint32_t kSsrcs[] = {101, 202, 303};
+
+MATCHER_P(RtpPacketReceivedSsrcMatcher, other, "") {
+ return arg.Ssrc() == other.Ssrc();
+}
+
+std::unique_ptr<RtpPacketReceived> GetRtpPacketReceived(uint32_t ssrc) {
danilchap 2017/05/24 17:14:02 may be call it Create...
eladalon 2017/05/25 08:00:05 Done.
+ std::unique_ptr<RtpPacketReceived> packet(new RtpPacketReceived());
danilchap 2017/05/24 17:14:02 may be to avoid mentioning RtpPacketReceived twice
eladalon 2017/05/25 08:00:05 Thank you for bringing this utility function to my
danilchap 2017/05/29 08:59:53 The way I see it, MakeUnique tells 'nothing odd go
eladalon 2017/05/30 14:23:37 Thanks for explaining your rationale for preferrin
nisse-webrtc 2017/05/30 14:53:23 MakeUnique was added recently, and I think the int
+ packet->SetSsrc(ssrc);
+ return packet;
+}
+
+class RtpDemuxerTest : public ::testing::Test {
+ protected:
+ RtpDemuxerTest() {
+ for (size_t i = 0; i < kNumOfSinks; i++) {
+ demuxer.AddSink(kSsrcs[i], &sinks[i]);
+ }
+ }
+
+ virtual ~RtpDemuxerTest() {
danilchap 2017/05/24 17:14:02 override instead of virtual
eladalon 2017/05/25 08:00:05 Done.
+ for (size_t i = 0; i < kNumOfSinks; i++) {
+ EXPECT_EQ(demuxer.RemoveSink(&sinks[i]), 1u);
+ }
+ }
+
+ RtpDemuxer demuxer;
+ static constexpr size_t kNumOfSinks = 3;
danilchap 2017/05/24 17:14:02 prefer constants above constructor. https://google
eladalon 2017/05/25 08:00:05 I thought you were okay with putting it here? htt
danilchap 2017/05/29 08:59:53 One of the worries: in your tests you rely on fact
+ MockRtpPacketSink sinks[kNumOfSinks];
+};
+
+TEST_F(RtpDemuxerTest, OnRtpPacketCalledOnCorrectSink) {
+ for (size_t i = 0; i < kNumOfSinks; i++) {
+ auto packet = GetRtpPacketReceived(kSsrcs[i]);
+ EXPECT_CALL(sinks[i], OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
danilchap 2017/05/24 17:14:02 which line you think would be faster to understand
eladalon 2017/05/25 08:00:05 I see what you mean. Sure, let's go with SsrcSameA
+ demuxer.OnRtpPacket(*packet);
+ testing::Mock::VerifyAndClearExpectations(&sinks[i]);
+ }
+}
+
+TEST_F(RtpDemuxerTest, MultipleSinksMappedToSameSsrc) {
+ // |sinks| associated with different SSRCs each. Add a few additional sinks
+ // that are all associated with one new, distinct SSRC.
+ constexpr size_t kNumOfSameSsrcSinks = 3;
+ MockRtpPacketSink same_ssrc_sinks[kNumOfSameSsrcSinks];
+ constexpr uint32_t kSharedSsrc = 404;
+ for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
+ demuxer.AddSink(kSharedSsrc, &same_ssrc_sinks[i]);
+ }
+
+ // Reception of an RTP packet associated with the shared SSRC triggers the
+ // callback on all of the interfaces associated with it.
+ auto packet = GetRtpPacketReceived(kSharedSsrc);
+ for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
+ EXPECT_CALL(same_ssrc_sinks[i],
+ OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
+ }
+ demuxer.OnRtpPacket(*packet);
+
+ // Test-specific tear-down
+ for (size_t i = 0; i < kNumOfSameSsrcSinks; i++) {
+ EXPECT_EQ(demuxer.RemoveSink(&same_ssrc_sinks[i]), 1u);
+ }
+}
+
+TEST_F(RtpDemuxerTest, SinkMappedToMultipleSsrcs) {
+ // |sinks| associated with different SSRCs each. We set one of them to also
+ // be mapped to additional SSRCs.
+ constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606};
+ MockRtpPacketSink multi_ssrc_sink;
+ for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
+ demuxer.AddSink(ssrc, &multi_ssrc_sink);
+ }
+
+ // The sink which is associated with multiple SSRCs gets the callback
+ // triggered for each of those SSRCs.
+ for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
+ auto packet = GetRtpPacketReceived(ssrc);
+ EXPECT_CALL(multi_ssrc_sink,
+ OnRtpPacket(RtpPacketReceivedSsrcMatcher(*packet)));
+ demuxer.OnRtpPacket(*packet);
+ testing::Mock::VerifyAndClearExpectations(&multi_ssrc_sink);
+ }
+
+ // Test-specific tear-down
+ EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink),
+ arraysize(kSsrcsOfMultiSsrcSink));
+}
+
+TEST_F(RtpDemuxerTest, SinkRemovalSanity) {
danilchap 2017/05/24 17:14:02 prefer to name test describing what it is tesing,
eladalon 2017/05/25 08:00:05 Good point. I've gone with "OnRtpPacketNotCalledOn
+ // |sinks| associated with different SSRCs each. We set one of them to also
+ // be mapped to additional SSRCs.
+ constexpr uint32_t kSsrcsOfMultiSsrcSink[] = {404, 505, 606};
+ MockRtpPacketSink multi_ssrc_sink;
+ for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
+ demuxer.AddSink(ssrc, &multi_ssrc_sink);
+ }
+
+ // Remove the sink.
+ EXPECT_EQ(demuxer.RemoveSink(&multi_ssrc_sink),
+ arraysize(kSsrcsOfMultiSsrcSink));
+
+ // The removed sink does not get callbacks triggered for any of the SSRCs
+ // with which it was previously associated.
danilchap 2017/05/24 17:14:03 check it with StrictMock<MockRtpPacketSink> or wit
eladalon 2017/05/25 08:00:05 I'm not sure why this would be needed. If you move
danilchap 2017/05/29 08:59:53 I tried to move RemoveSink after the loop - test s
eladalon 2017/05/30 14:23:37 Sorry, I mistook a warning for an error. Thank you
+ for (uint32_t ssrc : kSsrcsOfMultiSsrcSink) {
+ auto packet = GetRtpPacketReceived(ssrc);
+ demuxer.OnRtpPacket(*packet);
+ }
+}
+
+#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
+TEST_F(RtpDemuxerTest, RepeatedAssociationsForbidden) {
+ // Set-up already associated sinks[0] with kSsrcs[0]. Repeating the
+ // association is an error.
+ EXPECT_DEATH(demuxer.AddSink(kSsrcs[0], &sinks[0]), "");
+}
+
+TEST_F(RtpDemuxerTest, SinksMustBeRemovedBeforeDestruction) {
+ std::unique_ptr<RtpDemuxer> bad_demuxer(new RtpDemuxer());
+ MockRtpPacketSink sink;
+ constexpr uint32_t ssrc = 111;
+ bad_demuxer->AddSink(ssrc, &sink);
+ EXPECT_DEATH(bad_demuxer.reset(), "");
+ EXPECT_EQ(bad_demuxer->RemoveSink(&sink), 1u);
+}
+#endif
+
+} // namespace
+} // namespace webrtc
« webrtc/call/rtp_demuxer.cc ('K') | « webrtc/call/rtp_demuxer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698