2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/executors/Codel.h>
18 #include <folly/portability/GTest.h>
22 using std::chrono::milliseconds;
23 using std::this_thread::sleep_for;
25 TEST(CodelTest, Basic) {
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)));
42 TEST(CodelTest, highLoad) {
44 c.overloaded(milliseconds(40));
45 EXPECT_EQ(100, c.getLoad());
48 TEST(CodelTest, mediumLoad) {
50 c.overloaded(milliseconds(20));
51 sleep_for(milliseconds(90));
52 // this is overloaded but this request shouldn't drop because it's not >
54 EXPECT_FALSE(c.overloaded(milliseconds(8)));
55 EXPECT_GT(100, c.getLoad());
58 TEST(CodelTest, reducingLoad) {
60 c.overloaded(milliseconds(20));
61 sleep_for(milliseconds(90));
62 EXPECT_FALSE(c.overloaded(milliseconds(4)));
65 TEST(CodelTest, oneRequestNoDrop) {
67 EXPECT_FALSE(c.overloaded(milliseconds(20)));
70 TEST(CodelTest, getLoadSanity) {
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());
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());
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());
87 // this test demonstrates how silly getLoad() is, but silly isn't
88 // necessarily useless