|
22 | 22 | #include "rpc/WorkQueue.hpp" |
23 | 23 | #include "util/MockPrometheus.hpp" |
24 | 24 | #include "util/prometheus/Counter.hpp" |
| 25 | +#include "util/prometheus/Histogram.hpp" |
25 | 26 |
|
26 | 27 | #include <boost/json/object.hpp> |
27 | 28 | #include <boost/json/value_to.hpp> |
|
30 | 31 | #include <xrpl/protocol/jss.h> |
31 | 32 |
|
32 | 33 | #include <chrono> |
| 34 | +#include <cstdint> |
33 | 35 | #include <string> |
34 | 36 |
|
35 | 37 | using namespace rpc; |
@@ -272,3 +274,93 @@ TEST_F(RPCCountersMockPrometheusTests, onInternalError) |
272 | 274 | EXPECT_CALL(internalErrorMock, add(1)); |
273 | 275 | counters.onInternalError(); |
274 | 276 | } |
| 277 | + |
| 278 | +struct RPCCountersMockPrometheusRecotdLedgerRequestTest : RPCCountersMockPrometheusTests { |
| 279 | + testing::StrictMock<util::prometheus::MockHistogramImpl<int64_t>>& ageLedgersHistogramMock = |
| 280 | + makeMock<util::prometheus::HistogramInt>("rpc_requested_ledger_age_histogram", ""); |
| 281 | +}; |
| 282 | + |
| 283 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, currentLedger) |
| 284 | +{ |
| 285 | + // "current" is not tracked in the histogram (it's not a historical ledger lookup) |
| 286 | + // No mock expectations needed |
| 287 | + boost::json::object params; |
| 288 | + params["ledger_index"] = "current"; |
| 289 | + counters.recordLedgerRequest(params, 1000); |
| 290 | +} |
| 291 | + |
| 292 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, validateLedger) |
| 293 | +{ |
| 294 | + EXPECT_CALL(ageLedgersHistogramMock, observe(0)); |
| 295 | + |
| 296 | + boost::json::object params; |
| 297 | + params["ledger_index"] = "validated"; |
| 298 | + counters.recordLedgerRequest(params, 1000); |
| 299 | +} |
| 300 | + |
| 301 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, validatedDefaultLedger) |
| 302 | +{ |
| 303 | + EXPECT_CALL(ageLedgersHistogramMock, observe(0)); |
| 304 | + |
| 305 | + boost::json::object params; |
| 306 | + counters.recordLedgerRequest(params, 1000); |
| 307 | +} |
| 308 | + |
| 309 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, specificLedger) |
| 310 | +{ |
| 311 | + auto& ageLedgersHistogramMock = |
| 312 | + makeMock<util::prometheus::HistogramInt>("rpc_requested_ledger_age_histogram", ""); |
| 313 | + |
| 314 | + EXPECT_CALL(ageLedgersHistogramMock, observe(100)); // age is 1000 - 900 = 100 |
| 315 | + |
| 316 | + boost::json::object params; |
| 317 | + params["ledger_index"] = 900; |
| 318 | + counters.recordLedgerRequest(params, 1000); |
| 319 | +} |
| 320 | + |
| 321 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, stringNumberLedger) |
| 322 | +{ |
| 323 | + EXPECT_CALL(ageLedgersHistogramMock, observe(50)); // 1000 - 950 = 50 ledgers |
| 324 | + |
| 325 | + boost::json::object params; |
| 326 | + params["ledger_index"] = "950"; |
| 327 | + counters.recordLedgerRequest(params, 1000); |
| 328 | +} |
| 329 | + |
| 330 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, zeroAgeLedger) |
| 331 | +{ |
| 332 | + auto& ageLedgersHistogramMock = |
| 333 | + makeMock<util::prometheus::HistogramInt>("rpc_requested_ledger_age_histogram", ""); |
| 334 | + |
| 335 | + EXPECT_CALL(ageLedgersHistogramMock, observe(0)); // 1000 - 1000 = 0 ledgers |
| 336 | + |
| 337 | + boost::json::object params; |
| 338 | + params["ledger_index"] = 1000; // Same as current |
| 339 | + counters.recordLedgerRequest(params, 1000); |
| 340 | +} |
| 341 | + |
| 342 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, ledgerHashRequest) |
| 343 | +{ |
| 344 | + auto& ledgerHashCounterMock = makeMock<CounterInt>("rpc_ledger_hash_requests_total_number", ""); |
| 345 | + |
| 346 | + EXPECT_CALL(ledgerHashCounterMock, add(1)); |
| 347 | + |
| 348 | + boost::json::object params; |
| 349 | + params["ledger_hash"] = "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789"; |
| 350 | + counters.recordLedgerRequest(params, 1000); |
| 351 | +} |
| 352 | + |
| 353 | +TEST_F(RPCCountersMockPrometheusRecotdLedgerRequestTest, ledgerHashWithIndexIgnoresIndex) |
| 354 | +{ |
| 355 | + auto& ledgerHashCounterMock = makeMock<CounterInt>("rpc_ledger_hash_requests_total_number", ""); |
| 356 | + |
| 357 | + // When both ledger_hash and ledger_index are present, only ledger_hash counter should be |
| 358 | + // incremented |
| 359 | + EXPECT_CALL(ledgerHashCounterMock, add(1)); |
| 360 | + // No histogram call should be made |
| 361 | + |
| 362 | + boost::json::object params; |
| 363 | + params["ledger_hash"] = "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789"; |
| 364 | + params["ledger_index"] = 900; // This should be ignored |
| 365 | + counters.recordLedgerRequest(params, 1000); |
| 366 | +} |
0 commit comments