OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 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 <algorithm> | |
12 #include <memory> | |
13 | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "webrtc/base/rate_limiter.h" | |
16 #include "webrtc/system_wrappers/include/clock.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 class RateLimitTest : public ::testing::Test { | |
21 public: | |
22 RateLimitTest() | |
23 : clock_(0), rate_limiter(new RateLimiter(&clock_, kWindowSize)) {} | |
24 virtual ~RateLimitTest() {} | |
25 | |
26 void SetUp() override { rate_limiter->SetMaxRate(kMaxRateBps); } | |
27 | |
28 protected: | |
29 static const int64_t kWindowSize = 1000; | |
danilchap
2016/07/04 12:22:57
constexpr kWindowSizeMs
sprang_webrtc
2016/07/04 12:47:00
Done.
| |
30 static const uint32_t kMaxRateBps = 100000; | |
danilchap
2016/07/04 12:22:57
constexpr
sprang_webrtc
2016/07/04 12:47:00
Done.
| |
31 SimulatedClock clock_; | |
32 std::unique_ptr<RateLimiter> rate_limiter; | |
33 }; | |
34 | |
35 TEST_F(RateLimitTest, IncreasingMaxRate) { | |
36 // Fill rate, extend window to full size. | |
37 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
danilchap
2016/07/04 12:22:57
Can you expand this line to avoid thinking that Tr
sprang_webrtc
2016/07/04 12:47:00
Done.
| |
38 clock_.AdvanceTimeMilliseconds(kWindowSize - 1); | |
39 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
40 | |
41 // All rate consumed. | |
42 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
43 | |
44 // Double the available rate and fill that too. | |
45 rate_limiter->SetMaxRate(kMaxRateBps * 2); | |
46 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 8)); | |
47 | |
48 // All rate consumed again. | |
49 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
50 } | |
51 | |
52 TEST_F(RateLimitTest, DecreasingMaxRate) { | |
53 // Fill rate, extend window to full size. | |
54 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
danilchap
2016/07/04 12:22:57
same here (which hints constant with packet size t
sprang_webrtc
2016/07/04 12:47:00
Done.
| |
55 clock_.AdvanceTimeMilliseconds(kWindowSize - 1); | |
56 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
57 | |
58 // All rate consumed. | |
59 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
60 | |
61 // Halve the available rate and move window so half of the data falls out. | |
62 rate_limiter->SetMaxRate(kMaxRateBps / 2); | |
63 clock_.AdvanceTimeMilliseconds(1); | |
64 | |
65 // All rate still consumed. | |
66 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
67 } | |
68 | |
69 TEST_F(RateLimitTest, ChangingWindowSize) { | |
70 // Fill rate, extend window to full size. | |
71 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
72 clock_.AdvanceTimeMilliseconds(kWindowSize - 1); | |
73 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
74 | |
75 // All rate consumed. | |
76 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
77 | |
78 // Decrease window size so half of the data falls out. | |
79 rate_limiter->SetWindowSize(kWindowSize / 2); | |
80 // Average rate should still be the same, so rate is still all consumed. | |
81 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
82 | |
83 // Increase window size again. Now the rate is only half used (removed data | |
84 // points don't come back to life). | |
85 rate_limiter->SetWindowSize(kWindowSize); | |
86 EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); | |
87 | |
88 // All rate consumed again. | |
89 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | |
90 } | |
91 | |
92 TEST_F(RateLimitTest, SingleUsageAlwaysOk) { | |
93 // Using more bytes than can fit in a window is OK for a single packet. | |
94 EXPECT_TRUE(rate_limiter->TryUseRate((kMaxRateBps / 8) + 1)); | |
95 } | |
96 | |
97 } // namespace webrtc | |
OLD | NEW |