-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy paththrottle_utilities_test.cc
107 lines (93 loc) · 4.25 KB
/
throttle_utilities_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2023-Present Couchbase, Inc.
*
* Use of this software is governed by the Business Source License included
* in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
* in that file, in accordance with the Business Source License, use of this
* software will be governed by the Apache License, Version 2.0, included in
* the file licenses/APL2.txt.
*/
#include "throttle_utilities.h"
#include <folly/portability/GTest.h>
#include <nlohmann/json.hpp>
using namespace cb::throttle;
TEST(SetThrottleLimitPayload, AllDefault) {
SetThrottleLimitPayload limits = nlohmann::json::parse("{}");
EXPECT_EQ(std::numeric_limits<std::size_t>::max(), limits.reserved);
EXPECT_EQ(std::numeric_limits<std::size_t>::max(), limits.hard_limit);
}
TEST(SetThrottleLimitPayload, NumericValues) {
SetThrottleLimitPayload limits =
nlohmann::json::parse(R"({"reserved": 1, "hard_limit": 2})");
EXPECT_EQ(1, limits.reserved);
EXPECT_EQ(2, limits.hard_limit);
}
TEST(SetThrottleLimitPayload, StringValues) {
SetThrottleLimitPayload limits = nlohmann::json::parse(
R"({"reserved": 1, "hard_limit": "unlimited"})");
EXPECT_EQ(1, limits.reserved);
EXPECT_EQ(std::numeric_limits<std::size_t>::max(), limits.hard_limit);
}
TEST(SetThrottleLimitPayload, ToJson) {
nlohmann::json json = SetThrottleLimitPayload();
EXPECT_EQ(R"({"hard_limit":"unlimited","reserved":"unlimited"})",
json.dump());
json = SetThrottleLimitPayload(1, 2);
EXPECT_EQ(R"({"hard_limit":2,"reserved":1})", json.dump());
}
TEST(SetNodeThrottleLimitPayload, AllDefault) {
SetNodeThrottleLimitPayload limits = nlohmann::json::parse("{}");
EXPECT_FALSE(limits.capacity);
EXPECT_FALSE(limits.default_throttle_reserved_units);
EXPECT_FALSE(limits.default_throttle_hard_limit);
}
TEST(SetNodeThrottleLimitPayload, Capacity) {
SetNodeThrottleLimitPayload limits =
nlohmann::json::parse(R"({"capacity":1})");
EXPECT_TRUE(limits.capacity);
EXPECT_EQ(1, limits.capacity.value());
EXPECT_FALSE(limits.default_throttle_reserved_units);
EXPECT_FALSE(limits.default_throttle_hard_limit);
limits = nlohmann::json::parse(R"({"capacity":0})");
EXPECT_TRUE(limits.capacity);
EXPECT_EQ(0, limits.capacity.value());
limits = nlohmann::json::parse(R"({"capacity":"unlimited"})");
EXPECT_TRUE(limits.capacity);
EXPECT_EQ(std::numeric_limits<std::size_t>::max(), limits.capacity.value());
}
TEST(SetNodeThrottleLimitPayload, DefaultThrottle) {
SetNodeThrottleLimitPayload limits =
nlohmann::json{{"default_throttle_reserved_units", 10},
{"default_throttle_hard_limit", 25}};
EXPECT_FALSE(limits.capacity);
EXPECT_TRUE(limits.default_throttle_reserved_units);
EXPECT_TRUE(limits.default_throttle_hard_limit);
EXPECT_EQ(10, limits.default_throttle_reserved_units.value());
EXPECT_EQ(25, limits.default_throttle_hard_limit.value());
// They may be equal
limits = nlohmann::json{{"default_throttle_reserved_units", 40},
{"default_throttle_hard_limit", 40}};
EXPECT_FALSE(limits.capacity);
EXPECT_TRUE(limits.default_throttle_reserved_units);
EXPECT_TRUE(limits.default_throttle_hard_limit);
EXPECT_EQ(40, limits.default_throttle_reserved_units.value());
EXPECT_EQ(40, limits.default_throttle_hard_limit.value());
// They may be string
limits = nlohmann::json{{"default_throttle_reserved_units", "unlimited"},
{"default_throttle_hard_limit", "unlimited"}};
EXPECT_FALSE(limits.capacity);
EXPECT_TRUE(limits.default_throttle_reserved_units);
EXPECT_TRUE(limits.default_throttle_hard_limit);
EXPECT_EQ(std::numeric_limits<std::size_t>::max(),
limits.default_throttle_reserved_units.value());
EXPECT_EQ(std::numeric_limits<std::size_t>::max(),
limits.default_throttle_hard_limit.value());
// but reserved cannot exceed hard
try {
limits =
nlohmann::json{{"default_throttle_reserved_units", "unlimited"},
{"default_throttle_hard_limit", 100}};
FAIL() << "reserved must be less or equal to hard limit";
} catch (const std::exception&) {
}
}