1#include <test/jtx/Env.h>
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/core/ClosureCounter.h>
33 BEAST_EXPECT(voidCounter.
count() == 0);
37 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
38 BEAST_EXPECT(voidCounter.
count() == 1);
39 BEAST_EXPECT(evidence == 0);
40 BEAST_EXPECT(wrapped);
44 BEAST_EXPECT(evidence == 1);
46 BEAST_EXPECT(evidence == 2);
50 BEAST_EXPECT(voidCounter.
count() == 0);
55 BEAST_EXPECT(setCounter.
count() == 0);
59 auto setInt = [&evidence](
int i) { evidence = i; };
60 auto wrapped = setCounter.
wrap(setInt);
62 BEAST_EXPECT(setCounter.
count() == 1);
63 BEAST_EXPECT(evidence == 0);
64 BEAST_EXPECT(wrapped);
68 BEAST_EXPECT(evidence == 5);
70 BEAST_EXPECT(evidence == 11);
74 BEAST_EXPECT(setCounter.
count() == 0);
79 BEAST_EXPECT(sumCounter.
count() == 0);
82 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
83 auto wrapped = sumCounter.
wrap(
sum);
85 BEAST_EXPECT(sumCounter.
count() == 1);
86 BEAST_EXPECT(wrapped);
89 BEAST_EXPECT((*wrapped)(5, 2) == 7);
90 BEAST_EXPECT((*wrapped)(2, -8) == -6);
94 BEAST_EXPECT(sumCounter.
count() == 0);
120 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
153 BEAST_EXPECT(strCounter.
count() == 0);
158 BEAST_EXPECT(strCounter.
count() == 1);
159 BEAST_EXPECT(wrapped);
163 BEAST_EXPECT(result.
copies == 2);
164 BEAST_EXPECT(result.
moves == 1);
165 BEAST_EXPECT(result.
str ==
"value!");
166 BEAST_EXPECT(strValue.
str.
size() == 5);
171 BEAST_EXPECT(strCounter.
count() == 0);
173 auto wrapped = strCounter.
wrap(
176 BEAST_EXPECT(strCounter.
count() == 1);
177 BEAST_EXPECT(wrapped);
181 BEAST_EXPECT(result.
copies == 1);
183 BEAST_EXPECT(result.
str ==
"const lvalue!");
184 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
189 BEAST_EXPECT(strCounter.
count() == 0);
194 BEAST_EXPECT(strCounter.
count() == 1);
195 BEAST_EXPECT(wrapped);
199 BEAST_EXPECT(result.
copies == 1);
200 BEAST_EXPECT(result.
moves == 0);
201 BEAST_EXPECT(result.
str ==
"lvalue!");
202 BEAST_EXPECT(strLValue.
str == result.
str);
207 BEAST_EXPECT(strCounter.
count() == 0);
214 return std::move(
in +=
"!");
217 BEAST_EXPECT(strCounter.
count() == 1);
218 BEAST_EXPECT(wrapped);
222 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
223 TrackedString const result = (*wrapped)(std::move(strRValue));
224 BEAST_EXPECT(result.
copies == 0);
225 BEAST_EXPECT(result.
moves == 1);
226 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
227 BEAST_EXPECT(strRValue.
str.
size() == 0);
236 BEAST_EXPECT(voidCounter.
count() == 0);
238 auto wrapped1 = voidCounter.
wrap([]() {});
239 BEAST_EXPECT(voidCounter.
count() == 1);
242 auto wrapped2(wrapped1);
243 BEAST_EXPECT(voidCounter.
count() == 2);
246 auto wrapped3(std::move(wrapped2));
247 BEAST_EXPECT(voidCounter.
count() == 3);
250 auto wrapped4 = voidCounter.
wrap([]() {});
251 BEAST_EXPECT(voidCounter.
count() == 4);
253 BEAST_EXPECT(voidCounter.
count() == 3);
255 BEAST_EXPECT(voidCounter.
count() == 2);
257 BEAST_EXPECT(voidCounter.
count() == 1);
259 BEAST_EXPECT(voidCounter.
count() == 0);
262 using namespace std::chrono_literals;
263 voidCounter.
join(
"testWrap", 1ms,
j);
274 BEAST_EXPECT(voidCounter.
count() == 0);
276 auto wrapped = (voidCounter.
wrap([]() {}));
277 BEAST_EXPECT(voidCounter.
count() == 1);
281 std::thread localThread([&voidCounter, &threadExited,
this]() {
283 using namespace std::chrono_literals;
284 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
285 threadExited.store(
true);
289 while (!voidCounter.
joined())
295 using namespace std::chrono_literals;
297 BEAST_EXPECT(threadExited ==
false);
302 BEAST_EXPECT(voidCounter.
count() == 0);
305 while (threadExited ==
false)
A generic endpoint for log messages.
virtual beast::Journal journal(std::string const &name)=0
The role of a ClosureCounter is to assist in shutdown by letting callers wait for the completion of c...
bool joined() const
Returns true if this has been joined.
std::optional< Substitute< Closure > > wrap(Closure &&closure)
Wrap the passed closure with a reference counter.
int count() const
Current number of Closures outstanding.
void join(char const *name, std::chrono::milliseconds wait, beast::Journal j)
Returns once all counted in-flight closures are destroyed.
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static auto sum(TCollection const &col)