|
24 | 24 | #include "rpc/RPCHelpers.hpp" |
25 | 25 | #include "rpc/common/Types.hpp" |
26 | 26 | #include "util/AsioContextTestFixture.hpp" |
| 27 | +#include "util/LoggerFixtures.hpp" |
27 | 28 | #include "util/MockAmendmentCenter.hpp" |
28 | 29 | #include "util/MockBackendTestFixture.hpp" |
29 | 30 | #include "util/MockPrometheus.hpp" |
30 | 31 | #include "util/NameGenerator.hpp" |
| 32 | +#include "util/Taggable.hpp" |
31 | 33 | #include "util/TestObject.hpp" |
| 34 | +#include "util/newconfig/ConfigDefinition.hpp" |
| 35 | +#include "util/newconfig/ConfigValue.hpp" |
| 36 | +#include "util/newconfig/Types.hpp" |
32 | 37 |
|
33 | 38 | #include <boost/asio/impl/spawn.hpp> |
34 | 39 | #include <boost/asio/spawn.hpp> |
35 | 40 | #include <boost/json/array.hpp> |
| 41 | +#include <boost/json/object.hpp> |
36 | 42 | #include <boost/json/parse.hpp> |
37 | 43 | #include <fmt/core.h> |
38 | 44 | #include <gmock/gmock.h> |
|
48 | 54 | #include <xrpl/protocol/jss.h> |
49 | 55 |
|
50 | 56 | #include <array> |
| 57 | +#include <chrono> |
51 | 58 | #include <cstddef> |
52 | 59 | #include <cstdint> |
53 | 60 | #include <optional> |
| 61 | +#include <sstream> |
54 | 62 | #include <stdexcept> |
55 | 63 | #include <string> |
56 | 64 | #include <string_view> |
@@ -1166,3 +1174,71 @@ TEST_P(IsAdminCmdParameterTest, Test) |
1166 | 1174 | auto const testBundle = GetParam(); |
1167 | 1175 | EXPECT_EQ(isAdminCmd(testBundle.method, boost::json::parse(testBundle.testJson).as_object()), testBundle.expected); |
1168 | 1176 | } |
| 1177 | + |
| 1178 | +struct RPCHelpersLogDurationTestBundle { |
| 1179 | + std::string testName; |
| 1180 | + std::chrono::milliseconds duration; |
| 1181 | + std::string expectedLogLevel; |
| 1182 | + bool expectDuration; |
| 1183 | +}; |
| 1184 | + |
| 1185 | +struct RPCHelpersLogDurationTest : LoggerFixture, testing::WithParamInterface<RPCHelpersLogDurationTestBundle> { |
| 1186 | + boost::json::object const request = { |
| 1187 | + {"method", "account_info"}, |
| 1188 | + {"params", |
| 1189 | + boost::json::array{ |
| 1190 | + boost::json::object{{"account", "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh"}, {"secret", "should be deleted"}} |
| 1191 | + }} |
| 1192 | + }; |
| 1193 | + util::TagDecoratorFactory tagFactory{util::config::ClioConfigDefinition{ |
| 1194 | + {"log_tag_style", util::config::ConfigValue{util::config::ConfigType::String}.defaultValue("none")} |
| 1195 | + }}; |
| 1196 | + struct DummyTaggable : util::Taggable { |
| 1197 | + DummyTaggable(util::TagDecoratorFactory& f) : util::Taggable(f) |
| 1198 | + { |
| 1199 | + } |
| 1200 | + }; |
| 1201 | + DummyTaggable taggable{tagFactory}; |
| 1202 | +}; |
| 1203 | + |
| 1204 | +TEST_P(RPCHelpersLogDurationTest, LogDuration) |
| 1205 | +{ |
| 1206 | + auto const& tag = taggable.tag(); |
| 1207 | + // TOOD: Update in https://github.com/XRPLF/clio/issues/2008 |
| 1208 | + auto const tagStr = [&tag]() { |
| 1209 | + std::stringstream ss; |
| 1210 | + ss << tag; |
| 1211 | + return ss.str(); |
| 1212 | + }(); |
| 1213 | + |
| 1214 | + logDuration(request, tag, GetParam().duration); |
| 1215 | + |
| 1216 | + std::string const output = getLoggerString(); |
| 1217 | + |
| 1218 | + EXPECT_NE(output.find(GetParam().expectedLogLevel), std::string::npos) << output; |
| 1219 | + EXPECT_NE(output.find(tagStr), std::string::npos); |
| 1220 | + |
| 1221 | + if (GetParam().expectDuration) { |
| 1222 | + std::string durationStr = std::to_string(GetParam().duration.count()) + " milliseconds"; |
| 1223 | + EXPECT_NE(output.find(durationStr), std::string::npos); |
| 1224 | + } |
| 1225 | + |
| 1226 | + EXPECT_NE(output.find("account_info"), std::string::npos); |
| 1227 | + EXPECT_EQ(output.find("should be deleted"), std::string::npos); |
| 1228 | +} |
| 1229 | + |
| 1230 | +INSTANTIATE_TEST_SUITE_P( |
| 1231 | + RPCHelpersLogDurationTests, |
| 1232 | + RPCHelpersLogDurationTest, |
| 1233 | + testing::Values( |
| 1234 | + RPCHelpersLogDurationTestBundle{"ShortDurationLogsAsInfo", std::chrono::milliseconds(500), "RPC:NFO", true}, |
| 1235 | + RPCHelpersLogDurationTestBundle{ |
| 1236 | + "MediumDurationLogsAsWarning", |
| 1237 | + std::chrono::milliseconds(5000), |
| 1238 | + "RPC:WRN", |
| 1239 | + true |
| 1240 | + }, |
| 1241 | + RPCHelpersLogDurationTestBundle{"LongDurationLogsAsError", std::chrono::milliseconds(15000), "RPC:ERR", true} |
| 1242 | + ), |
| 1243 | + tests::util::kNAME_GENERATOR |
| 1244 | +); |
0 commit comments