20#include <test/jtx/Env.h>
22#include <xrpld/core/ClosureCounter.h>
24#include <xrpl/beast/unit_test.h>
53 BEAST_EXPECT(voidCounter.
count() == 0);
57 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
58 BEAST_EXPECT(voidCounter.
count() == 1);
59 BEAST_EXPECT(evidence == 0);
60 BEAST_EXPECT(wrapped);
64 BEAST_EXPECT(evidence == 1);
66 BEAST_EXPECT(evidence == 2);
70 BEAST_EXPECT(voidCounter.
count() == 0);
75 BEAST_EXPECT(setCounter.
count() == 0);
79 auto setInt = [&evidence](
int i) { evidence = i; };
80 auto wrapped = setCounter.
wrap(setInt);
82 BEAST_EXPECT(setCounter.
count() == 1);
83 BEAST_EXPECT(evidence == 0);
84 BEAST_EXPECT(wrapped);
88 BEAST_EXPECT(evidence == 5);
90 BEAST_EXPECT(evidence == 11);
94 BEAST_EXPECT(setCounter.
count() == 0);
99 BEAST_EXPECT(sumCounter.
count() == 0);
102 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
103 auto wrapped = sumCounter.
wrap(
sum);
105 BEAST_EXPECT(sumCounter.
count() == 1);
106 BEAST_EXPECT(wrapped);
109 BEAST_EXPECT((*wrapped)(5, 2) == 7);
110 BEAST_EXPECT((*wrapped)(2, -8) == -6);
114 BEAST_EXPECT(sumCounter.
count() == 0);
140 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
173 BEAST_EXPECT(strCounter.
count() == 0);
178 BEAST_EXPECT(strCounter.
count() == 1);
179 BEAST_EXPECT(wrapped);
183 BEAST_EXPECT(result.
copies == 2);
184 BEAST_EXPECT(result.
moves == 1);
185 BEAST_EXPECT(result.
str ==
"value!");
186 BEAST_EXPECT(strValue.
str.
size() == 5);
191 BEAST_EXPECT(strCounter.
count() == 0);
193 auto wrapped = strCounter.
wrap(
196 BEAST_EXPECT(strCounter.
count() == 1);
197 BEAST_EXPECT(wrapped);
201 BEAST_EXPECT(result.
copies == 1);
203 BEAST_EXPECT(result.
str ==
"const lvalue!");
204 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
209 BEAST_EXPECT(strCounter.
count() == 0);
214 BEAST_EXPECT(strCounter.
count() == 1);
215 BEAST_EXPECT(wrapped);
219 BEAST_EXPECT(result.
copies == 1);
220 BEAST_EXPECT(result.
moves == 0);
221 BEAST_EXPECT(result.
str ==
"lvalue!");
222 BEAST_EXPECT(strLValue.
str == result.
str);
227 BEAST_EXPECT(strCounter.
count() == 0);
234 return std::move(
in +=
"!");
237 BEAST_EXPECT(strCounter.
count() == 1);
238 BEAST_EXPECT(wrapped);
242 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
243 TrackedString const result = (*wrapped)(std::move(strRValue));
244 BEAST_EXPECT(result.
copies == 0);
245 BEAST_EXPECT(result.
moves == 1);
246 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
247 BEAST_EXPECT(strRValue.
str.
size() == 0);
256 BEAST_EXPECT(voidCounter.
count() == 0);
258 auto wrapped1 = voidCounter.
wrap([]() {});
259 BEAST_EXPECT(voidCounter.
count() == 1);
262 auto wrapped2(wrapped1);
263 BEAST_EXPECT(voidCounter.
count() == 2);
266 auto wrapped3(std::move(wrapped2));
267 BEAST_EXPECT(voidCounter.
count() == 3);
270 auto wrapped4 = voidCounter.
wrap([]() {});
271 BEAST_EXPECT(voidCounter.
count() == 4);
273 BEAST_EXPECT(voidCounter.
count() == 3);
275 BEAST_EXPECT(voidCounter.
count() == 2);
277 BEAST_EXPECT(voidCounter.
count() == 1);
279 BEAST_EXPECT(voidCounter.
count() == 0);
282 using namespace std::chrono_literals;
283 voidCounter.
join(
"testWrap", 1ms,
j);
294 BEAST_EXPECT(voidCounter.
count() == 0);
296 auto wrapped = (voidCounter.
wrap([]() {}));
297 BEAST_EXPECT(voidCounter.
count() == 1);
301 std::thread localThread([&voidCounter, &threadExited,
this]() {
303 using namespace std::chrono_literals;
304 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
305 threadExited.store(
true);
309 while (!voidCounter.
joined())
315 using namespace std::chrono_literals;
317 BEAST_EXPECT(threadExited ==
false);
322 BEAST_EXPECT(voidCounter.
count() == 0);
325 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)