Chromium Code Reviews| Index: webrtc/base/rate_limiter_unittest.cc |
| diff --git a/webrtc/base/rate_limiter_unittest.cc b/webrtc/base/rate_limiter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b82f31819d357c0969a5f3fc2b3a28ab61070c85 |
| --- /dev/null |
| +++ b/webrtc/base/rate_limiter_unittest.cc |
| @@ -0,0 +1,97 @@ |
| +/* |
| + * Copyright (c) 2016 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 <algorithm> |
| +#include <memory> |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webrtc/base/rate_limiter.h" |
| +#include "webrtc/system_wrappers/include/clock.h" |
| + |
| +namespace webrtc { |
| + |
| +class RateLimitTest : public ::testing::Test { |
| + public: |
| + RateLimitTest() |
| + : clock_(0), rate_limiter(new RateLimiter(&clock_, kWindowSize)) {} |
| + virtual ~RateLimitTest() {} |
| + |
| + void SetUp() override { rate_limiter->SetMaxRate(kMaxRateBps); } |
| + |
| + protected: |
| + static const int64_t kWindowSize = 1000; |
|
danilchap
2016/07/04 12:22:57
constexpr kWindowSizeMs
sprang_webrtc
2016/07/04 12:47:00
Done.
|
| + static const uint32_t kMaxRateBps = 100000; |
|
danilchap
2016/07/04 12:22:57
constexpr
sprang_webrtc
2016/07/04 12:47:00
Done.
|
| + SimulatedClock clock_; |
| + std::unique_ptr<RateLimiter> rate_limiter; |
| +}; |
| + |
| +TEST_F(RateLimitTest, IncreasingMaxRate) { |
| + // Fill rate, extend window to full size. |
| + 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.
|
| + clock_.AdvanceTimeMilliseconds(kWindowSize - 1); |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); |
| + |
| + // All rate consumed. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| + |
| + // Double the available rate and fill that too. |
| + rate_limiter->SetMaxRate(kMaxRateBps * 2); |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 8)); |
| + |
| + // All rate consumed again. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| +} |
| + |
| +TEST_F(RateLimitTest, DecreasingMaxRate) { |
| + // Fill rate, extend window to full size. |
| + 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.
|
| + clock_.AdvanceTimeMilliseconds(kWindowSize - 1); |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); |
| + |
| + // All rate consumed. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| + |
| + // Halve the available rate and move window so half of the data falls out. |
| + rate_limiter->SetMaxRate(kMaxRateBps / 2); |
| + clock_.AdvanceTimeMilliseconds(1); |
| + |
| + // All rate still consumed. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| +} |
| + |
| +TEST_F(RateLimitTest, ChangingWindowSize) { |
| + // Fill rate, extend window to full size. |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); |
| + clock_.AdvanceTimeMilliseconds(kWindowSize - 1); |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); |
| + |
| + // All rate consumed. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| + |
| + // Decrease window size so half of the data falls out. |
| + rate_limiter->SetWindowSize(kWindowSize / 2); |
| + // Average rate should still be the same, so rate is still all consumed. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| + |
| + // Increase window size again. Now the rate is only half used (removed data |
| + // points don't come back to life). |
| + rate_limiter->SetWindowSize(kWindowSize); |
| + EXPECT_TRUE(rate_limiter->TryUseRate(kMaxRateBps / 16)); |
| + |
| + // All rate consumed again. |
| + EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| +} |
| + |
| +TEST_F(RateLimitTest, SingleUsageAlwaysOk) { |
| + // Using more bytes than can fit in a window is OK for a single packet. |
| + EXPECT_TRUE(rate_limiter->TryUseRate((kMaxRateBps / 8) + 1)); |
| +} |
| + |
| +} // namespace webrtc |