xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
jtx
WSClient_test.cpp
1
#include <test/jtx/Env.h>
2
#include <test/jtx/WSClient.h>
3
#include <test/jtx/amount.h>
4
5
#include <xrpl/beast/unit_test/suite.h>
6
#include <xrpl/json/json_value.h>
7
8
#include <
chrono
>
9
10
namespace
xrpl::test
{
11
12
class
WSClient_test
:
public
beast::unit_test::Suite
13
{
14
public
:
15
void
16
testSmoke
()
17
{
18
testcase
(
"smoke"
);
19
using namespace
jtx
;
20
Env
env(*
this
);
21
auto
wsc =
makeWSClient
(env.
app
().
config
());
22
{
23
json::Value
jv;
24
jv[
"streams"
] =
json::ValueType::Array
;
25
jv[
"streams"
].
append
(
"ledger"
);
26
}
27
env.
fund
(
XRP
(10000),
"alice"
);
28
env.
close
();
29
auto
jv = wsc->getMsg(
std::chrono::seconds
(1));
30
pass
();
31
}
32
33
void
34
testGracefulDisconnect
()
35
{
36
testcase
(
"graceful disconnect"
);
37
using namespace
jtx
;
38
using namespace
std::chrono
;
39
40
Env
env(*
this
);
41
auto
wsc =
makeWSClient
(env.
app
().
config
());
42
43
// Put real traffic on the connection before closing it.
44
json::Value
stream;
45
stream[
"streams"
] =
json::ValueType::Array
;
46
stream[
"streams"
].append(
"ledger"
);
47
auto
const
sub = wsc->invoke(
"subscribe"
, stream);
48
BEAST_EXPECT(sub.isMember(
"result"
) || sub.isMember(
"status"
));
49
50
// disconnect() performs a graceful WebSocket closing handshake and
51
// blocks until the server acknowledges. On loopback that completes in
52
// well under its internal 1s timeout; only a broken async_close/ack
53
// coordination would fall through to the force-close path at ~1s. A
54
// generous bound keeps this from flaking under load while still
55
// catching that regression.
56
auto
const
start =
steady_clock::now
();
57
wsc->disconnect();
58
auto
const
elapsed =
duration_cast<milliseconds>
(
steady_clock::now
() - start);
59
BEAST_EXPECT(elapsed <
milliseconds
{750});
60
61
// disconnect() must be idempotent: a second call (and the subsequent
62
// destructor) must not hang, double-close, or crash.
63
wsc->disconnect();
64
pass
();
65
}
66
67
void
68
run
()
override
69
{
70
testSmoke
();
71
testGracefulDisconnect
();
72
}
73
};
74
75
BEAST_DEFINE_TESTSUITE
(
WSClient
,
jtx
,
xrpl
);
76
77
}
// namespace xrpl::test
chrono
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::pass
void pass()
Record a successful test condition.
Definition
suite.h:532
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
json::Value
Represents a JSON value.
Definition
json_value.h:117
json::Value::append
Value & append(Value const &value)
Append value to array at the end.
Definition
json_value.cpp:997
xrpl::Application::config
virtual Config & config()=0
xrpl::test::WSClient_test
Definition
WSClient_test.cpp:13
xrpl::test::WSClient_test::testGracefulDisconnect
void testGracefulDisconnect()
Definition
WSClient_test.cpp:34
xrpl::test::WSClient_test::testSmoke
void testSmoke()
Definition
WSClient_test.cpp:16
xrpl::test::WSClient_test::run
void run() override
Runs the suite.
Definition
WSClient_test.cpp:68
xrpl::test::WSClient
Definition
WSClient.h:19
xrpl::test::jtx::Env
A transaction testing environment.
Definition
Env.h:161
xrpl::test::jtx::Env::app
Application & app()
Definition
Env.h:300
xrpl::test::jtx::Env::close
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition
Env.cpp:133
xrpl::test::jtx::Env::fund
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition
Env.cpp:323
std::chrono::duration_cast
T duration_cast(T... args)
std::chrono::seconds
json::ValueType::Array
@ Array
array value (ordered list)
Definition
json_value.h:28
std::chrono
xrpl::test::jtx
Definition
Oracle_test.cpp:34
xrpl::test::jtx::XRP
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition
amount.cpp:92
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
xrpl::test::makeWSClient
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpcVersion, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
Definition
WSClient.cpp:371
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
std::chrono::steady_clock::now
T now(T... args)
Generated by
1.17.0