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);
116 :
copies(rhs.copies),
moves(rhs.moves + 1),
str(std::move(rhs.str))
149 BEAST_EXPECT(strCounter.
count() == 0);
153 BEAST_EXPECT(strCounter.
count() == 1);
154 BEAST_EXPECT(wrapped);
158 (*wrapped)(strValue);
159 BEAST_EXPECT(result.
copies == 2);
160 BEAST_EXPECT(result.
moves == 1);
161 BEAST_EXPECT(result.
str ==
"value!");
162 BEAST_EXPECT(strValue.
str.
size() == 5);
167 BEAST_EXPECT(strCounter.
count() == 0);
171 BEAST_EXPECT(strCounter.
count() == 1);
172 BEAST_EXPECT(wrapped);
176 (*wrapped)(strConstLValue);
177 BEAST_EXPECT(result.
copies == 1);
179 BEAST_EXPECT(result.
str ==
"const lvalue!");
180 BEAST_EXPECT(strConstLValue.
str.
size() == 12);
185 BEAST_EXPECT(strCounter.
count() == 0);
189 BEAST_EXPECT(strCounter.
count() == 1);
190 BEAST_EXPECT(wrapped);
194 (*wrapped)(strLValue);
195 BEAST_EXPECT(result.
copies == 1);
196 BEAST_EXPECT(result.
moves == 0);
197 BEAST_EXPECT(result.
str ==
"lvalue!");
198 BEAST_EXPECT(strLValue.
str == result.
str);
203 BEAST_EXPECT(strCounter.
count() == 0);
211 return std::move(
in);
214 BEAST_EXPECT(strCounter.
count() == 1);
215 BEAST_EXPECT(wrapped);
218 TrackedString strRValue(
"rvalue abcdefghijklmnopqrstuvwxyz");
220 (*wrapped)(std::move(strRValue));
221 BEAST_EXPECT(result.
copies == 0);
222 BEAST_EXPECT(result.
moves == 1);
223 BEAST_EXPECT(result.
str ==
"rvalue abcdefghijklmnopqrstuvwxyz!");
224 BEAST_EXPECT(strRValue.
str.
empty());
233 BEAST_EXPECT(voidCounter.
count() == 0);
235 auto wrapped1 = voidCounter.
wrap([]() {});
236 BEAST_EXPECT(voidCounter.
count() == 1);
239 auto wrapped2(wrapped1);
240 BEAST_EXPECT(voidCounter.
count() == 2);
243 auto wrapped3(std::move(wrapped2));
244 BEAST_EXPECT(voidCounter.
count() == 3);
247 auto wrapped4 = voidCounter.
wrap([]() {});
248 BEAST_EXPECT(voidCounter.
count() == 4);
250 BEAST_EXPECT(voidCounter.
count() == 3);
252 BEAST_EXPECT(voidCounter.
count() == 2);
254 BEAST_EXPECT(voidCounter.
count() == 1);
256 BEAST_EXPECT(voidCounter.
count() == 0);
259 using namespace std::chrono_literals;
260 voidCounter.
join(
"testWrap", 1ms,
j);
271 BEAST_EXPECT(voidCounter.
count() == 0);
273 auto wrapped = (voidCounter.
wrap([]() {}));
274 BEAST_EXPECT(voidCounter.
count() == 1);
278 std::thread localThread([&voidCounter, &threadExited,
this]() {
280 using namespace std::chrono_literals;
281 voidCounter.
join(
"testWaitOnJoin", 1ms,
j);
282 threadExited.store(
true);
286 while (!voidCounter.
joined())
292 using namespace std::chrono_literals;
294 BEAST_EXPECT(threadExited ==
false);
299 BEAST_EXPECT(voidCounter.
count() == 0);
302 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...
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.
virtual beast::Journal getJournal(std::string const &name)=0
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)