| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <memory> | 12 #include <memory> |
| 13 | 13 |
| 14 #include "webrtc/base/event.h" | 14 #include "webrtc/base/event.h" |
| 15 #include "webrtc/base/platform_thread.h" | 15 #include "webrtc/base/platform_thread.h" |
| 16 #include "webrtc/base/rate_limiter.h" | 16 #include "webrtc/base/rate_limiter.h" |
| 17 #include "webrtc/base/task_queue.h" | 17 #include "webrtc/base/task_queue.h" |
| 18 #include "webrtc/system_wrappers/include/clock.h" | 18 #include "webrtc/system_wrappers/include/clock.h" |
| 19 #include "webrtc/test/gtest.h" | 19 #include "webrtc/test/gtest.h" |
| 20 | 20 |
| 21 namespace webrtc { | 21 namespace webrtc { |
| 22 | 22 |
| 23 class RateLimitTest : public ::testing::Test { | 23 class RateLimitTest : public ::testing::Test { |
| 24 public: | 24 public: |
| 25 RateLimitTest() | 25 RateLimitTest() |
| 26 : clock_(0), rate_limiter(new RateLimiter(&clock_, kWindowSizeMs)) {} | 26 : clock_(0), rate_limiter(new RateLimiter(&clock_, kWindowSizeMs)) {} |
| 27 virtual ~RateLimitTest() {} | 27 ~RateLimitTest() override {} |
| 28 | 28 |
| 29 void SetUp() override { rate_limiter->SetMaxRate(kMaxRateBps); } | 29 void SetUp() override { rate_limiter->SetMaxRate(kMaxRateBps); } |
| 30 | 30 |
| 31 protected: | 31 protected: |
| 32 static constexpr int64_t kWindowSizeMs = 1000; | 32 static constexpr int64_t kWindowSizeMs = 1000; |
| 33 static constexpr uint32_t kMaxRateBps = 100000; | 33 static constexpr uint32_t kMaxRateBps = 100000; |
| 34 // Bytes needed to completely saturate the rate limiter. | 34 // Bytes needed to completely saturate the rate limiter. |
| 35 static constexpr size_t kRateFillingBytes = | 35 static constexpr size_t kRateFillingBytes = |
| 36 (kMaxRateBps * kWindowSizeMs) / (8 * 1000); | 36 (kMaxRateBps * kWindowSizeMs) / (8 * 1000); |
| 37 SimulatedClock clock_; | 37 SimulatedClock clock_; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // Runs a few simple tasks, each on its own thread, but coordinated with | 140 // Runs a few simple tasks, each on its own thread, but coordinated with |
| 141 // events so that they run in a serialized order. Intended to catch data | 141 // events so that they run in a serialized order. Intended to catch data |
| 142 // races when run with tsan et al. | 142 // races when run with tsan et al. |
| 143 | 143 |
| 144 // Half window size, double rate -> same amount of bytes needed to fill rate. | 144 // Half window size, double rate -> same amount of bytes needed to fill rate. |
| 145 | 145 |
| 146 class SetWindowSizeTask : public ThreadTask { | 146 class SetWindowSizeTask : public ThreadTask { |
| 147 public: | 147 public: |
| 148 explicit SetWindowSizeTask(RateLimiter* rate_limiter) | 148 explicit SetWindowSizeTask(RateLimiter* rate_limiter) |
| 149 : ThreadTask(rate_limiter) {} | 149 : ThreadTask(rate_limiter) {} |
| 150 virtual ~SetWindowSizeTask() {} | 150 ~SetWindowSizeTask() override {} |
| 151 | 151 |
| 152 void DoRun() override { | 152 void DoRun() override { |
| 153 EXPECT_TRUE(rate_limiter_->SetWindowSize(kWindowSizeMs / 2)); | 153 EXPECT_TRUE(rate_limiter_->SetWindowSize(kWindowSizeMs / 2)); |
| 154 } | 154 } |
| 155 } set_window_size_task(rate_limiter.get()); | 155 } set_window_size_task(rate_limiter.get()); |
| 156 rtc::PlatformThread thread1(RunTask, &set_window_size_task, "Thread1"); | 156 rtc::PlatformThread thread1(RunTask, &set_window_size_task, "Thread1"); |
| 157 thread1.Start(); | 157 thread1.Start(); |
| 158 | 158 |
| 159 class SetMaxRateTask : public ThreadTask { | 159 class SetMaxRateTask : public ThreadTask { |
| 160 public: | 160 public: |
| 161 explicit SetMaxRateTask(RateLimiter* rate_limiter) | 161 explicit SetMaxRateTask(RateLimiter* rate_limiter) |
| 162 : ThreadTask(rate_limiter) {} | 162 : ThreadTask(rate_limiter) {} |
| 163 virtual ~SetMaxRateTask() {} | 163 ~SetMaxRateTask() override {} |
| 164 | 164 |
| 165 void DoRun() override { rate_limiter_->SetMaxRate(kMaxRateBps * 2); } | 165 void DoRun() override { rate_limiter_->SetMaxRate(kMaxRateBps * 2); } |
| 166 } set_max_rate_task(rate_limiter.get()); | 166 } set_max_rate_task(rate_limiter.get()); |
| 167 rtc::PlatformThread thread2(RunTask, &set_max_rate_task, "Thread2"); | 167 rtc::PlatformThread thread2(RunTask, &set_max_rate_task, "Thread2"); |
| 168 thread2.Start(); | 168 thread2.Start(); |
| 169 | 169 |
| 170 class UseRateTask : public ThreadTask { | 170 class UseRateTask : public ThreadTask { |
| 171 public: | 171 public: |
| 172 UseRateTask(RateLimiter* rate_limiter, SimulatedClock* clock) | 172 UseRateTask(RateLimiter* rate_limiter, SimulatedClock* clock) |
| 173 : ThreadTask(rate_limiter), clock_(clock) {} | 173 : ThreadTask(rate_limiter), clock_(clock) {} |
| 174 virtual ~UseRateTask() {} | 174 ~UseRateTask() override {} |
| 175 | 175 |
| 176 void DoRun() override { | 176 void DoRun() override { |
| 177 EXPECT_TRUE(rate_limiter_->TryUseRate(kRateFillingBytes / 2)); | 177 EXPECT_TRUE(rate_limiter_->TryUseRate(kRateFillingBytes / 2)); |
| 178 clock_->AdvanceTimeMilliseconds((kWindowSizeMs / 2) - 1); | 178 clock_->AdvanceTimeMilliseconds((kWindowSizeMs / 2) - 1); |
| 179 EXPECT_TRUE(rate_limiter_->TryUseRate(kRateFillingBytes / 2)); | 179 EXPECT_TRUE(rate_limiter_->TryUseRate(kRateFillingBytes / 2)); |
| 180 } | 180 } |
| 181 | 181 |
| 182 SimulatedClock* const clock_; | 182 SimulatedClock* const clock_; |
| 183 } use_rate_task(rate_limiter.get(), &clock_); | 183 } use_rate_task(rate_limiter.get(), &clock_); |
| 184 rtc::PlatformThread thread3(RunTask, &use_rate_task, "Thread3"); | 184 rtc::PlatformThread thread3(RunTask, &use_rate_task, "Thread3"); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 195 | 195 |
| 196 // All rate consumed. | 196 // All rate consumed. |
| 197 EXPECT_FALSE(rate_limiter->TryUseRate(1)); | 197 EXPECT_FALSE(rate_limiter->TryUseRate(1)); |
| 198 | 198 |
| 199 thread1.Stop(); | 199 thread1.Stop(); |
| 200 thread2.Stop(); | 200 thread2.Stop(); |
| 201 thread3.Stop(); | 201 thread3.Stop(); |
| 202 } | 202 } |
| 203 | 203 |
| 204 } // namespace webrtc | 204 } // namespace webrtc |
| OLD | NEW |