1 | /* |
2 | * Copyright 2017-present Facebook, 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 <folly/executors/Codel.h> |
18 | #include <folly/portability/GTest.h> |
19 | #include <chrono> |
20 | #include <thread> |
21 | |
22 | using std::chrono::milliseconds; |
23 | using std::this_thread::sleep_for; |
24 | |
25 | TEST(CodelTest, Basic) { |
26 | folly::Codel c; |
27 | std::this_thread::sleep_for(milliseconds(110)); |
28 | // This interval is overloaded |
29 | EXPECT_FALSE(c.overloaded(milliseconds(100))); |
30 | std::this_thread::sleep_for(milliseconds(90)); |
31 | // At least two requests must happen in an interval before they will fail |
32 | EXPECT_FALSE(c.overloaded(milliseconds(50))); |
33 | EXPECT_TRUE(c.overloaded(milliseconds(50))); |
34 | std::this_thread::sleep_for(milliseconds(110)); |
35 | // Previous interval is overloaded, but 2ms isn't enough to fail |
36 | EXPECT_FALSE(c.overloaded(milliseconds(2))); |
37 | std::this_thread::sleep_for(milliseconds(90)); |
38 | // 20 ms > target interval * 2 |
39 | EXPECT_TRUE(c.overloaded(milliseconds(20))); |
40 | } |
41 | |
42 | TEST(CodelTest, highLoad) { |
43 | folly::Codel c; |
44 | c.overloaded(milliseconds(40)); |
45 | EXPECT_EQ(100, c.getLoad()); |
46 | } |
47 | |
48 | TEST(CodelTest, mediumLoad) { |
49 | folly::Codel c; |
50 | c.overloaded(milliseconds(20)); |
51 | sleep_for(milliseconds(90)); |
52 | // this is overloaded but this request shouldn't drop because it's not > |
53 | // slough timeout |
54 | EXPECT_FALSE(c.overloaded(milliseconds(8))); |
55 | EXPECT_GT(100, c.getLoad()); |
56 | } |
57 | |
58 | TEST(CodelTest, reducingLoad) { |
59 | folly::Codel c; |
60 | c.overloaded(milliseconds(20)); |
61 | sleep_for(milliseconds(90)); |
62 | EXPECT_FALSE(c.overloaded(milliseconds(4))); |
63 | } |
64 | |
65 | TEST(CodelTest, oneRequestNoDrop) { |
66 | folly::Codel c; |
67 | EXPECT_FALSE(c.overloaded(milliseconds(20))); |
68 | } |
69 | |
70 | TEST(CodelTest, getLoadSanity) { |
71 | folly::Codel c; |
72 | // should be 100% but leave a litte wiggle room. |
73 | c.overloaded(milliseconds(10)); |
74 | EXPECT_LT(99, c.getLoad()); |
75 | EXPECT_GT(101, c.getLoad()); |
76 | |
77 | // should be 70% but leave a litte wiggle room. |
78 | c.overloaded(milliseconds(7)); |
79 | EXPECT_LT(60, c.getLoad()); |
80 | EXPECT_GT(80, c.getLoad()); |
81 | |
82 | // should be 20% but leave a litte wiggle room. |
83 | c.overloaded(milliseconds(2)); |
84 | EXPECT_LT(10, c.getLoad()); |
85 | EXPECT_GT(30, c.getLoad()); |
86 | |
87 | // this test demonstrates how silly getLoad() is, but silly isn't |
88 | // necessarily useless |
89 | } |
90 | |