blob: 422bd27b686f38d466c3a8936caf65990d5a7004 [file] [log] [blame]
stefan@webrtc.org82462aa2014-10-23 11:57:05 +00001/*
2 * Copyright (c) 2014 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 <limits>
12
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webrtc/modules/pacing/bitrate_prober.h"
15
16namespace webrtc {
17
18TEST(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) {
19 BitrateProber prober;
20 EXPECT_FALSE(prober.IsProbing());
21 int64_t now_ms = 0;
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000022 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000023
philipeleb680ea2016-08-17 11:11:59 +020024 prober.CreateProbeCluster(900000, 6);
25 prober.CreateProbeCluster(1800000, 5);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000026 EXPECT_FALSE(prober.IsProbing());
27
philipel4a1ec1e2016-08-15 11:51:06 -070028 prober.OnIncomingPacket(1000);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000029 EXPECT_TRUE(prober.IsProbing());
philipeldd324862016-05-06 17:06:14 +020030 EXPECT_EQ(0, prober.CurrentClusterId());
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000031
Peter Boström0453ef82016-02-16 16:23:08 +010032 // First packet should probe as soon as possible.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000033 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
34 prober.PacketSent(now_ms, 1000);
35
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000036 for (int i = 0; i < 5; ++i) {
stefan@webrtc.orgd839e0a2014-11-04 19:33:55 +000037 EXPECT_EQ(8, prober.TimeUntilNextProbe(now_ms));
38 now_ms += 4;
39 EXPECT_EQ(4, prober.TimeUntilNextProbe(now_ms));
40 now_ms += 4;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000041 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipeldd324862016-05-06 17:06:14 +020042 EXPECT_EQ(0, prober.CurrentClusterId());
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000043 prober.PacketSent(now_ms, 1000);
44 }
45 for (int i = 0; i < 5; ++i) {
46 EXPECT_EQ(4, prober.TimeUntilNextProbe(now_ms));
47 now_ms += 4;
48 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipeldd324862016-05-06 17:06:14 +020049 EXPECT_EQ(1, prober.CurrentClusterId());
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000050 prober.PacketSent(now_ms, 1000);
51 }
52
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000053 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000054 EXPECT_FALSE(prober.IsProbing());
55}
Peter Boström0453ef82016-02-16 16:23:08 +010056
57TEST(BitrateProberTest, DoesntProbeWithoutRecentPackets) {
58 BitrateProber prober;
59 EXPECT_FALSE(prober.IsProbing());
60 int64_t now_ms = 0;
61 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
62
philipeleb680ea2016-08-17 11:11:59 +020063 prober.CreateProbeCluster(900000, 6);
Peter Boström0453ef82016-02-16 16:23:08 +010064 EXPECT_FALSE(prober.IsProbing());
65
philipel4a1ec1e2016-08-15 11:51:06 -070066 prober.OnIncomingPacket(1000);
Peter Boström0453ef82016-02-16 16:23:08 +010067 EXPECT_TRUE(prober.IsProbing());
68 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
Irfan Sheriff6e11efa2016-08-02 12:57:37 -070069 prober.PacketSent(now_ms, 1000);
Peter Boström0453ef82016-02-16 16:23:08 +010070 // Let time pass, no large enough packets put into prober.
71 now_ms += 6000;
72 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
73 // Insert a small packet, not a candidate for probing.
philipel4a1ec1e2016-08-15 11:51:06 -070074 prober.OnIncomingPacket(100);
Peter Boström0453ef82016-02-16 16:23:08 +010075 prober.PacketSent(now_ms, 100);
76 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
77 // Insert a large-enough packet after downtime while probing should reset to
78 // perform a new probe since the requested one didn't finish.
philipel4a1ec1e2016-08-15 11:51:06 -070079 prober.OnIncomingPacket(1000);
Peter Boström0453ef82016-02-16 16:23:08 +010080 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
81 prober.PacketSent(now_ms, 1000);
82 // Next packet should be part of new probe and be sent with non-zero delay.
philipel4a1ec1e2016-08-15 11:51:06 -070083 prober.OnIncomingPacket(1000);
Peter Boström0453ef82016-02-16 16:23:08 +010084 EXPECT_GT(prober.TimeUntilNextProbe(now_ms), 0);
85}
86
87TEST(BitrateProberTest, DoesntInitializeProbingForSmallPackets) {
88 BitrateProber prober;
89 prober.SetEnabled(true);
90 EXPECT_FALSE(prober.IsProbing());
91
philipel4a1ec1e2016-08-15 11:51:06 -070092 prober.OnIncomingPacket(100);
Peter Boström0453ef82016-02-16 16:23:08 +010093 EXPECT_FALSE(prober.IsProbing());
94}
95
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000096} // namespace webrtc