xrpld
Loading...
Searching...
No Matches
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
10namespace xrpl::test {
11
13{
14public:
15 void
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
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();
72 }
73};
74
76
77} // namespace xrpl::test
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
TestcaseT testcase
Memberspace for declaring test cases.
Definition suite.h:155
Represents a JSON value.
Definition json_value.h:117
Value & append(Value const &value)
Append value to array at the end.
virtual Config & config()=0
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:161
Application & app()
Definition Env.h:300
bool close(NetClock::time_point closeTime, std::optional< std::chrono::milliseconds > consensusDelay=std::nullopt)
Close and advance the ledger.
Definition Env.cpp:133
void fund(bool setDefaultRipple, STAmount const &amount, Account const &account)
Definition Env.cpp:323
T duration_cast(T... args)
@ Array
array value (ordered list)
Definition json_value.h:28
XrpT const XRP
Converts to XRP Issue or STAmount.
Definition amount.cpp:92
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5