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_, kWindowSizeMs)) {} |
| 24 virtual ~RateLimitTest() {} |
| 25 |
| 26 void SetUp() override { rate_limiter->SetMaxRate(kMaxRateBps); } |
| 27 |
| 28 protected: |
| 29 static constexpr int64_t kWindowSizeMs = 1000; |
| 30 static constexpr uint32_t kMaxRateBps = 100000; |
| 31 // Bytes needed to completely saturate the rate limiter. |
| 32 static constexpr size_t kRateFillingBytes = |
| 33 (kMaxRateBps * kWindowSizeMs) / (8 * 1000); |
| 34 SimulatedClock clock_; |
| 35 std::unique_ptr<RateLimiter> rate_limiter; |
| 36 }; |
| 37 |
| 38 TEST_F(RateLimitTest, IncreasingMaxRate) { |
| 39 // Fill rate, extend window to full size. |
| 40 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 41 clock_.AdvanceTimeMilliseconds(kWindowSizeMs - 1); |
| 42 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 43 |
| 44 // All rate consumed. |
| 45 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 46 |
| 47 // Double the available rate and fill that too. |
| 48 rate_limiter->SetMaxRate(kMaxRateBps * 2); |
| 49 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes)); |
| 50 |
| 51 // All rate consumed again. |
| 52 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 53 } |
| 54 |
| 55 TEST_F(RateLimitTest, DecreasingMaxRate) { |
| 56 // Fill rate, extend window to full size. |
| 57 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 58 clock_.AdvanceTimeMilliseconds(kWindowSizeMs - 1); |
| 59 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 60 |
| 61 // All rate consumed. |
| 62 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 63 |
| 64 // Halve the available rate and move window so half of the data falls out. |
| 65 rate_limiter->SetMaxRate(kMaxRateBps / 2); |
| 66 clock_.AdvanceTimeMilliseconds(1); |
| 67 |
| 68 // All rate still consumed. |
| 69 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 70 } |
| 71 |
| 72 TEST_F(RateLimitTest, ChangingWindowSize) { |
| 73 // Fill rate, extend window to full size. |
| 74 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 75 clock_.AdvanceTimeMilliseconds(kWindowSizeMs - 1); |
| 76 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 77 |
| 78 // All rate consumed. |
| 79 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 80 |
| 81 // Decrease window size so half of the data falls out. |
| 82 rate_limiter->SetWindowSize(kWindowSizeMs / 2); |
| 83 // Average rate should still be the same, so rate is still all consumed. |
| 84 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 85 |
| 86 // Increase window size again. Now the rate is only half used (removed data |
| 87 // points don't come back to life). |
| 88 rate_limiter->SetWindowSize(kWindowSizeMs); |
| 89 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes / 2)); |
| 90 |
| 91 // All rate consumed again. |
| 92 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 93 } |
| 94 |
| 95 TEST_F(RateLimitTest, SingleUsageAlwaysOk) { |
| 96 // Using more bytes than can fit in a window is OK for a single packet. |
| 97 EXPECT_TRUE(rate_limiter->TryUseRate(kRateFillingBytes + 1)); |
| 98 } |
| 99 |
| 100 } // namespace webrtc |
OLD | NEW |