1#include <test/jtx/Env.h>
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/core/ClosureCounter.h>
29 BEAST_EXPECT(voidCounter.
count() == 0);
33 auto wrapped = voidCounter.
wrap([&evidence]() { ++evidence; });
34 BEAST_EXPECT(voidCounter.
count() == 1);
35 BEAST_EXPECT(evidence == 0);
36 BEAST_EXPECT(wrapped);
40 BEAST_EXPECT(evidence == 1);
42 BEAST_EXPECT(evidence == 2);
46 BEAST_EXPECT(voidCounter.
count() == 0);
51 BEAST_EXPECT(setCounter.
count() == 0);
55 auto setInt = [&evidence](
int i) { evidence = i; };
56 auto wrapped = setCounter.
wrap(setInt);
58 BEAST_EXPECT(setCounter.
count() == 1);
59 BEAST_EXPECT(evidence == 0);
60 BEAST_EXPECT(wrapped);
64 BEAST_EXPECT(evidence == 5);
66 BEAST_EXPECT(evidence == 11);
70 BEAST_EXPECT(setCounter.
count() == 0);
75 BEAST_EXPECT(sumCounter.
count() == 0);
78 auto const sum = [](
int ii,
int jj) {
return ii + jj; };
79 auto wrapped = sumCounter.
wrap(
sum);
81 BEAST_EXPECT(sumCounter.
count() == 1);
82 BEAST_EXPECT(wrapped);
85 BEAST_EXPECT((*wrapped)(5, 2) == 7);
86 BEAST_EXPECT((*wrapped)(2, -8) == -6);
90 BEAST_EXPECT(sumCounter.
count() == 0);
147 BEAST_EXPECT(strCounter.
count() == 0);
151 BEAST_EXPECT(strCounter.
count() == 1);
152 BEAST_EXPECT(wrapped);
156 BEAST_EXPECT(result.
copies == 2);
157 BEAST_EXPECT(result.
moves == 1);
158 BEAST_EXPECT(result.
str ==
"value!");
159 BEAST_EXPECT(strValue.
str.
size() == 5);
164 BEAST_EXPECT(strCounter.
count() == 0);
168 BEAST_EXPECT(strCounter.
count() == 1);
169 BEAST_EXPECT(wrapped);
173 BEAST_EXPECT(result.
copies == 1);
175 BEAST_EXPECT(result.
str ==
"const lvalue!");
176 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
181 BEAST_EXPECT(strCounter.
count() == 0);
185 BEAST_EXPECT(strCounter.
count() == 1);
186 BEAST_EXPECT(wrapped);
190 BEAST_EXPECT(result.
copies == 1);
191 BEAST_EXPECT(result.
moves == 0);
192 BEAST_EXPECT(result.
str ==
"lvalue!");
193 BEAST_EXPECT(strLValue.
str == result.
str);
198 BEAST_EXPECT(strCounter.
count() == 0);
205 return std::move(
in +=
"!");
208 BEAST_EXPECT(strCounter.
count() == 1);
209 BEAST_EXPECT(wrapped);
213 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
214 TrackedString const result = (*wrapped)(std::move(strRValue));
215 BEAST_EXPECT(result.
copies == 0);
216 BEAST_EXPECT(result.
moves == 1);
217 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
218 BEAST_EXPECT(strRValue.
str.
size() == 0);
227 BEAST_EXPECT(voidCounter.
count() == 0);
229 auto wrapped1 = voidCounter.
wrap([]() {});
230 BEAST_EXPECT(voidCounter.
count() == 1);
233 auto wrapped2(wrapped1);
234 BEAST_EXPECT(voidCounter.
count() == 2);
237 auto wrapped3(std::move(wrapped2));
238 BEAST_EXPECT(voidCounter.
count() == 3);
241 auto wrapped4 = voidCounter.
wrap([]() {});
242 BEAST_EXPECT(voidCounter.
count() == 4);
244 BEAST_EXPECT(voidCounter.
count() == 3);
246 BEAST_EXPECT(voidCounter.
count() == 2);
248 BEAST_EXPECT(voidCounter.
count() == 1);
250 BEAST_EXPECT(voidCounter.
count() == 0);
253 using namespace std::chrono_literals;
254 voidCounter.
join(
"testWrap", 1ms,
j);
265 BEAST_EXPECT(voidCounter.
count() == 0);
267 auto wrapped = (voidCounter.
wrap([]() {}));
268 BEAST_EXPECT(voidCounter.
count() == 1);
272 std::thread localThread([&voidCounter, &threadExited,
this]() {
274 using namespace std::chrono_literals;
275 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
276 threadExited.store(
true);
280 while (!voidCounter.
joined())
286 using namespace std::chrono_literals;
288 BEAST_EXPECT(threadExited ==
false);
293 BEAST_EXPECT(voidCounter.
count() == 0);
296 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)