rippled
Loading...
Searching...
No Matches
GetCounts_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012-2017 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <test/jtx.h>
21
22#include <xrpl/basics/CountedObject.h>
23#include <xrpl/beast/unit_test.h>
24#include <xrpl/protocol/SField.h>
25#include <xrpl/protocol/jss.h>
26
27namespace ripple {
28
30{
31 void
33 {
34 using namespace test::jtx;
35 Env env(*this);
36
37 Json::Value result;
38 {
39 using namespace std::chrono_literals;
40 // Add a little delay so the App's "uptime" will have a value.
42 // check counts with no transactions posted
43 result = env.rpc("get_counts")[jss::result];
44 BEAST_EXPECT(result[jss::status] == "success");
45 BEAST_EXPECT(!result.isMember("Transaction"));
46 BEAST_EXPECT(!result.isMember("STObject"));
47 BEAST_EXPECT(!result.isMember("HashRouterEntry"));
48 BEAST_EXPECT(
49 result.isMember(jss::uptime) &&
50 !result[jss::uptime].asString().empty());
51 BEAST_EXPECT(
52 result.isMember(jss::dbKBTotal) &&
53 result[jss::dbKBTotal].asInt() > 0);
54 }
55
56 // create some transactions
57 env.close();
58 Account alice{"alice"};
59 Account bob{"bob"};
60 env.fund(XRP(10000), alice, bob);
61 env.trust(alice["USD"](1000), bob);
62 for (auto i = 0; i < 20; ++i)
63 {
64 env(pay(alice, bob, alice["USD"](5)));
65 env.close();
66 }
67
68 {
69 // check counts, default params
70 result = env.rpc("get_counts")[jss::result];
71 BEAST_EXPECT(result[jss::status] == "success");
72 // compare with values reported by CountedObjects
73 auto const& objectCounts =
75 for (auto const& it : objectCounts)
76 {
77 BEAST_EXPECTS(result.isMember(it.first), it.first);
78 BEAST_EXPECTS(result[it.first].asInt() == it.second, it.first);
79 }
80 BEAST_EXPECT(!result.isMember(jss::local_txs));
81 }
82
83 {
84 // make request with min threshold 100 and verify
85 // that only STObject and NodeObject are reported
86 result = env.rpc("get_counts", "100")[jss::result];
87 BEAST_EXPECT(result[jss::status] == "success");
88
89 // compare with values reported by CountedObjects
90 auto const& objectCounts =
92 for (auto const& it : objectCounts)
93 {
94 BEAST_EXPECTS(result.isMember(it.first), it.first);
95 BEAST_EXPECTS(result[it.first].asInt() == it.second, it.first);
96 }
97 BEAST_EXPECT(!result.isMember("Transaction"));
98 BEAST_EXPECT(!result.isMember("STTx"));
99 BEAST_EXPECT(!result.isMember("STArray"));
100 BEAST_EXPECT(!result.isMember("HashRouterEntry"));
101 BEAST_EXPECT(!result.isMember("STLedgerEntry"));
102 }
103
104 {
105 // local_txs field will exist when there are open Txs
106 env(pay(alice, bob, alice["USD"](5)));
107 result = env.rpc("get_counts")[jss::result];
108 // deliberately don't call close so we have open Tx
109 BEAST_EXPECT(
110 result.isMember(jss::local_txs) &&
111 result[jss::local_txs].asInt() > 0);
112 }
113 }
114
115public:
116 void
117 run() override
118 {
120 }
121};
122
123BEAST_DEFINE_TESTSUITE(GetCounts, rpc, ripple);
124
125} // namespace ripple
Represents a JSON value.
Definition: json_value.h:150
Int asInt() const
Definition: json_value.cpp:516
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482
bool isMember(char const *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:962
A testsuite class.
Definition: suite.h:55
static CountedObjects & getInstance() noexcept
List getCounts(int minimumThreshold) const
void run() override
Runs the suite.
Immutable cryptographic account descriptor.
Definition: Account.h:39
A transaction testing environment.
Definition: Env.h:121
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition: Env.cpp:117
void trust(STAmount const &amount, Account const &account)
Establish trust lines.
Definition: Env.cpp:264
Json::Value rpc(unsigned apiVersion, std::unordered_map< std::string, std::string > const &headers, std::string const &cmd, Args &&... args)
Execute an RPC command.
Definition: Env.h:770
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition: Env.cpp:233
Set the expected result code for a JTx The test will fail if the code doesn't match.
Definition: rpc.h:35
T empty(T... args)
Json::Value pay(AccountID const &account, AccountID const &to, AnyAmount amount)
Create a payment.
Definition: pay.cpp:30
XRP_t const XRP
Converts to XRP Issue or STAmount.
Definition: amount.cpp:105
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T sleep_for(T... args)