1#include <test/jtx/Env.h>
3#include <xrpld/core/ClosureCounter.h>
5#include <xrpl/beast/unit_test.h>
34 BEAST_EXPECT(voidCounter.
count() == 0);
38 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
39 BEAST_EXPECT(voidCounter.
count() == 1);
40 BEAST_EXPECT(evidence == 0);
41 BEAST_EXPECT(wrapped);
45 BEAST_EXPECT(evidence == 1);
47 BEAST_EXPECT(evidence == 2);
51 BEAST_EXPECT(voidCounter.
count() == 0);
56 BEAST_EXPECT(setCounter.
count() == 0);
60 auto setInt = [&evidence](
int i) { evidence = i; };
61 auto wrapped = setCounter.
wrap(setInt);
63 BEAST_EXPECT(setCounter.
count() == 1);
64 BEAST_EXPECT(evidence == 0);
65 BEAST_EXPECT(wrapped);
69 BEAST_EXPECT(evidence == 5);
71 BEAST_EXPECT(evidence == 11);
75 BEAST_EXPECT(setCounter.
count() == 0);
80 BEAST_EXPECT(sumCounter.
count() == 0);
83 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
84 auto wrapped = sumCounter.
wrap(
sum);
86 BEAST_EXPECT(sumCounter.
count() == 1);
87 BEAST_EXPECT(wrapped);
90 BEAST_EXPECT((*wrapped)(5, 2) == 7);
91 BEAST_EXPECT((*wrapped)(2, -8) == -6);
95 BEAST_EXPECT(sumCounter.
count() == 0);
121 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
154 BEAST_EXPECT(strCounter.
count() == 0);
159 BEAST_EXPECT(strCounter.
count() == 1);
160 BEAST_EXPECT(wrapped);
164 BEAST_EXPECT(result.
copies == 2);
165 BEAST_EXPECT(result.
moves == 1);
166 BEAST_EXPECT(result.
str ==
"value!");
167 BEAST_EXPECT(strValue.
str.
size() == 5);
172 BEAST_EXPECT(strCounter.
count() == 0);
174 auto wrapped = strCounter.
wrap(
177 BEAST_EXPECT(strCounter.
count() == 1);
178 BEAST_EXPECT(wrapped);
182 BEAST_EXPECT(result.
copies == 1);
184 BEAST_EXPECT(result.
str ==
"const lvalue!");
185 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
190 BEAST_EXPECT(strCounter.
count() == 0);
195 BEAST_EXPECT(strCounter.
count() == 1);
196 BEAST_EXPECT(wrapped);
200 BEAST_EXPECT(result.
copies == 1);
201 BEAST_EXPECT(result.
moves == 0);
202 BEAST_EXPECT(result.
str ==
"lvalue!");
203 BEAST_EXPECT(strLValue.
str == result.
str);
208 BEAST_EXPECT(strCounter.
count() == 0);
215 return std::move(
in +=
"!");
218 BEAST_EXPECT(strCounter.
count() == 1);
219 BEAST_EXPECT(wrapped);
223 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
224 TrackedString const result = (*wrapped)(std::move(strRValue));
225 BEAST_EXPECT(result.
copies == 0);
226 BEAST_EXPECT(result.
moves == 1);
227 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
228 BEAST_EXPECT(strRValue.
str.
size() == 0);
237 BEAST_EXPECT(voidCounter.
count() == 0);
239 auto wrapped1 = voidCounter.
wrap([]() {});
240 BEAST_EXPECT(voidCounter.
count() == 1);
243 auto wrapped2(wrapped1);
244 BEAST_EXPECT(voidCounter.
count() == 2);
247 auto wrapped3(std::move(wrapped2));
248 BEAST_EXPECT(voidCounter.
count() == 3);
251 auto wrapped4 = voidCounter.
wrap([]() {});
252 BEAST_EXPECT(voidCounter.
count() == 4);
254 BEAST_EXPECT(voidCounter.
count() == 3);
256 BEAST_EXPECT(voidCounter.
count() == 2);
258 BEAST_EXPECT(voidCounter.
count() == 1);
260 BEAST_EXPECT(voidCounter.
count() == 0);
263 using namespace std::chrono_literals;
264 voidCounter.
join(
"testWrap", 1ms,
j);
275 BEAST_EXPECT(voidCounter.
count() == 0);
277 auto wrapped = (voidCounter.
wrap([]() {}));
278 BEAST_EXPECT(voidCounter.
count() == 1);
282 std::thread localThread([&voidCounter, &threadExited,
this]() {
284 using namespace std::chrono_literals;
285 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
286 threadExited.store(
true);
290 while (!voidCounter.
joined())
296 using namespace std::chrono_literals;
298 BEAST_EXPECT(threadExited ==
false);
303 BEAST_EXPECT(voidCounter.
count() == 0);
306 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.
friend TrackedString operator+(TrackedString const &s, char const *rhs)
TrackedString(TrackedString const &rhs)
TrackedString(TrackedString &&rhs) noexcept
TrackedString & operator=(TrackedString const &rhs)=delete
TrackedString(char const *rhs)
TrackedString & operator+=(char const *rhs)
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)