]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/utils/RateLimiter.h" | |
18 | #include <chrono> | |
19 | #include <gtest/gtest.h> | |
20 | ||
21 | namespace jaegertracing { | |
22 | namespace utils { | |
23 | namespace { | |
24 | ||
25 | std::chrono::steady_clock::time_point currentTime; | |
26 | ||
27 | class MockClock { | |
28 | public: | |
29 | using rep = std::chrono::steady_clock::rep; | |
30 | using period = std::chrono::steady_clock::period; | |
31 | using duration = std::chrono::steady_clock::duration; | |
32 | using time_point = std::chrono::steady_clock::time_point; | |
33 | ||
34 | static const bool is_steady() { return false; } | |
35 | ||
36 | static time_point now() { return currentTime; } | |
37 | }; | |
38 | ||
39 | } // anonymous namespace | |
40 | ||
41 | TEST(RateLimiter, testRateLimiter) | |
42 | { | |
43 | const auto timestamp = std::chrono::steady_clock::now(); | |
44 | currentTime = timestamp; | |
45 | RateLimiter<MockClock> limiter(2, 2); | |
46 | ||
47 | ASSERT_TRUE(limiter.checkCredit(1)); | |
48 | ASSERT_TRUE(limiter.checkCredit(1)); | |
49 | ASSERT_FALSE(limiter.checkCredit(1)); | |
50 | ||
51 | currentTime = timestamp + std::chrono::milliseconds(250); | |
52 | ASSERT_FALSE(limiter.checkCredit(1)); | |
53 | ||
54 | currentTime = timestamp + std::chrono::milliseconds(750); | |
55 | ASSERT_TRUE(limiter.checkCredit(1)); | |
56 | ASSERT_FALSE(limiter.checkCredit(1)); | |
57 | ||
58 | currentTime = timestamp + std::chrono::seconds(5); | |
59 | ASSERT_TRUE(limiter.checkCredit(1)); | |
60 | ASSERT_TRUE(limiter.checkCredit(1)); | |
61 | ASSERT_FALSE(limiter.checkCredit(1)); | |
62 | ASSERT_FALSE(limiter.checkCredit(1)); | |
63 | ASSERT_FALSE(limiter.checkCredit(1)); | |
64 | } | |
65 | ||
66 | TEST(RateLimiter, testMaxBalance) | |
67 | { | |
68 | const auto timestamp = std::chrono::steady_clock::now(); | |
69 | currentTime = timestamp; | |
70 | RateLimiter<MockClock> limiter(0.1, 1.0); | |
71 | ||
72 | ASSERT_TRUE(limiter.checkCredit(1.0)); | |
73 | ||
74 | currentTime = timestamp + std::chrono::seconds(20); | |
75 | ASSERT_TRUE(limiter.checkCredit(1.0)); | |
76 | ASSERT_FALSE(limiter.checkCredit(1.0)); | |
77 | } | |
78 | ||
79 | } // namespace utils | |
80 | } // namespace jaegertracing |