1#include <test/jtx/Env.h>
2#include <test/jtx/envconfig.h>
4#include <xrpl/beast/unit_test/suite.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/core/ClosureCounter.h>
31 BEAST_EXPECT(voidCounter.
count() == 0);
35 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
36 BEAST_EXPECT(voidCounter.
count() == 1);
37 BEAST_EXPECT(evidence == 0);
38 BEAST_EXPECT(wrapped);
42 BEAST_EXPECT(evidence == 1);
44 BEAST_EXPECT(evidence == 2);
47 wrapped = std::nullopt;
48 BEAST_EXPECT(voidCounter.
count() == 0);
53 BEAST_EXPECT(setCounter.
count() == 0);
57 auto setInt = [&evidence](
int i) { evidence = i; };
58 auto wrapped = setCounter.
wrap(setInt);
60 BEAST_EXPECT(setCounter.
count() == 1);
61 BEAST_EXPECT(evidence == 0);
62 BEAST_EXPECT(wrapped);
66 BEAST_EXPECT(evidence == 5);
68 BEAST_EXPECT(evidence == 11);
71 wrapped = std::nullopt;
72 BEAST_EXPECT(setCounter.
count() == 0);
77 BEAST_EXPECT(sumCounter.
count() == 0);
80 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
81 auto wrapped = sumCounter.
wrap(
sum);
83 BEAST_EXPECT(sumCounter.
count() == 1);
84 BEAST_EXPECT(wrapped);
87 BEAST_EXPECT((*wrapped)(5, 2) == 7);
88 BEAST_EXPECT((*wrapped)(2, -8) == -6);
91 wrapped = std::nullopt;
92 BEAST_EXPECT(sumCounter.
count() == 0);
118 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
151 BEAST_EXPECT(strCounter.
count() == 0);
155 BEAST_EXPECT(strCounter.
count() == 1);
156 BEAST_EXPECT(wrapped);
160 (*wrapped)(strValue);
161 BEAST_EXPECT(result.
copies == 2);
162 BEAST_EXPECT(result.
moves == 1);
163 BEAST_EXPECT(result.
str ==
"value!");
164 BEAST_EXPECT(strValue.
str.
size() == 5);
169 BEAST_EXPECT(strCounter.
count() == 0);
171 auto wrapped = strCounter.
wrap([](
TrackedString const& in) {
return in +
"!"; });
173 BEAST_EXPECT(strCounter.
count() == 1);
174 BEAST_EXPECT(wrapped);
178 (*wrapped)(strConstLValue);
179 BEAST_EXPECT(result.
copies == 1);
181 BEAST_EXPECT(result.
str ==
"const lvalue!");
182 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
187 BEAST_EXPECT(strCounter.
count() == 0);
191 BEAST_EXPECT(strCounter.
count() == 1);
192 BEAST_EXPECT(wrapped);
196 (*wrapped)(strLValue);
197 BEAST_EXPECT(result.
copies == 1);
198 BEAST_EXPECT(result.
moves == 0);
199 BEAST_EXPECT(result.
str ==
"lvalue!");
200 BEAST_EXPECT(strLValue.
str == result.
str);
205 BEAST_EXPECT(strCounter.
count() == 0);
213 return std::move(in);
216 BEAST_EXPECT(strCounter.
count() == 1);
217 BEAST_EXPECT(wrapped);
220 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
222 (*wrapped)(std::move(strRValue));
223 BEAST_EXPECT(result.
copies == 0);
224 BEAST_EXPECT(result.
moves == 1);
225 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
226 BEAST_EXPECT(strRValue.
str.
empty());
235 BEAST_EXPECT(voidCounter.
count() == 0);
237 auto wrapped1 = voidCounter.
wrap([]() {});
238 BEAST_EXPECT(voidCounter.
count() == 1);
241 auto wrapped2(wrapped1);
242 BEAST_EXPECT(voidCounter.
count() == 2);
245 auto wrapped3(std::move(wrapped2));
246 BEAST_EXPECT(voidCounter.
count() == 3);
249 auto wrapped4 = voidCounter.
wrap([]() {});
250 BEAST_EXPECT(voidCounter.
count() == 4);
252 BEAST_EXPECT(voidCounter.
count() == 3);
254 BEAST_EXPECT(voidCounter.
count() == 2);
256 BEAST_EXPECT(voidCounter.
count() == 1);
258 BEAST_EXPECT(voidCounter.
count() == 0);
261 using namespace std::chrono_literals;
262 voidCounter.
join(
"testWrap", 1ms,
j_);
265 BEAST_EXPECT(voidCounter.
wrap([]() {}) == std::nullopt);
273 BEAST_EXPECT(voidCounter.
count() == 0);
275 auto wrapped = (voidCounter.
wrap([]() {}));
276 BEAST_EXPECT(voidCounter.
count() == 1);
280 std::thread localThread([&voidCounter, &threadExited,
this]() {
282 using namespace std::chrono_literals;
283 voidCounter.
join(
"testWaitOnJoin", 1ms,
j_);
284 threadExited.
store(
true);
288 while (!voidCounter.
joined())
294 using namespace std::chrono_literals;
296 BEAST_EXPECT(threadExited ==
false);
300 wrapped = std::nullopt;
301 BEAST_EXPECT(voidCounter.
count() == 0);
304 while (!threadExited)
A generic endpoint for log messages.
The role of a ClosureCounter is to assist in shutdown by letting callers wait for the completion of c...
void join(char const *name, std::chrono::milliseconds wait, beast::Journal j)
Returns once all counted in-flight closures are destroyed.
std::optional< Substitute< Closure > > wrap(Closure &&closure)
Wrap the passed closure with a reference counter.
bool joined() const
Returns true if this has been joined.
int count() const
Current number of Closures outstanding.
TrackedString & operator+=(char const *rhs)
friend TrackedString operator+(TrackedString const &s, char const *rhs)
TrackedString(TrackedString const &rhs)
TrackedString(char const *rhs)
TrackedString(TrackedString &&rhs) noexcept
TrackedString & operator=(TrackedString const &rhs)=delete
void run() override
Runs the suite.
A transaction testing environment.
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static auto sum(TCollection const &col)