rippled
Loading...
Searching...
No Matches
JobQueue_test.cpp
1#include <test/jtx/Env.h>
2
3#include <xrpl/beast/unit_test.h>
4#include <xrpl/core/JobQueue.h>
5
6namespace xrpl {
7namespace test {
8
9//------------------------------------------------------------------------------
10
12{
13 void
15 {
16 jtx::Env env{*this};
17
18 JobQueue& jQueue = env.app().getJobQueue();
19 {
20 // addJob() should run the Job (and return true).
21 std::atomic<bool> jobRan{false};
22 BEAST_EXPECT(jQueue.addJob(jtCLIENT, "JobAddTest1", [&jobRan]() {
23 jobRan = true;
24 }) == true);
25
26 // Wait for the Job to run.
27 while (jobRan == false)
28 ;
29 }
30 {
31 // If the JobQueue is stopped, we should no
32 // longer be able to add Jobs (and calling addJob() should
33 // return false).
34 using namespace std::chrono_literals;
35 jQueue.stop();
36
37 // The Job should never run, so having the Job access this
38 // unprotected variable on the stack should be completely safe.
39 // Not recommended for the faint of heart...
40 bool unprotected;
41 BEAST_EXPECT(
42 jQueue.addJob(jtCLIENT, "JobAddTest2", [&unprotected]() {
43 unprotected = false;
44 }) == false);
45 }
46 }
47
48 void
50 {
51 jtx::Env env{*this};
52
53 JobQueue& jQueue = env.app().getJobQueue();
54 {
55 // Test repeated post()s until the Coro completes.
56 std::atomic<int> yieldCount{0};
57 auto const coro = jQueue.postCoro(
59 "PostCoroTest1",
60 [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
61 while (++yieldCount < 4)
62 coroCopy->yield();
63 });
64 BEAST_EXPECT(coro != nullptr);
65
66 // Wait for the Job to run and yield.
67 while (yieldCount == 0)
68 ;
69
70 // Now re-post until the Coro says it is done.
71 int old = yieldCount;
72 while (coro->runnable())
73 {
74 BEAST_EXPECT(coro->post());
75 while (old == yieldCount)
76 {
77 }
78 coro->join();
79 BEAST_EXPECT(++old == yieldCount);
80 }
81 BEAST_EXPECT(yieldCount == 4);
82 }
83 {
84 // Test repeated resume()s until the Coro completes.
85 int yieldCount{0};
86 auto const coro = jQueue.postCoro(
88 "PostCoroTest2",
89 [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
90 while (++yieldCount < 4)
91 coroCopy->yield();
92 });
93 if (!coro)
94 {
95 // There's no good reason we should not get a Coro, but we
96 // can't continue without one.
97 BEAST_EXPECT(false);
98 return;
99 }
100
101 // Wait for the Job to run and yield.
102 coro->join();
103
104 // Now resume until the Coro says it is done.
105 int old = yieldCount;
106 while (coro->runnable())
107 {
108 coro->resume(); // Resume runs synchronously on this thread.
109 BEAST_EXPECT(++old == yieldCount);
110 }
111 BEAST_EXPECT(yieldCount == 4);
112 }
113 {
114 // If the JobQueue is stopped, we should no
115 // longer be able to add a Coro (and calling postCoro() should
116 // return false).
117 using namespace std::chrono_literals;
118 jQueue.stop();
119
120 // The Coro should never run, so having the Coro access this
121 // unprotected variable on the stack should be completely safe.
122 // Not recommended for the faint of heart...
123 bool unprotected;
124 auto const coro = jQueue.postCoro(
125 jtCLIENT,
126 "PostCoroTest3",
127 [&unprotected](std::shared_ptr<JobQueue::Coro> const&) {
128 unprotected = false;
129 });
130 BEAST_EXPECT(coro == nullptr);
131 }
132 }
133
134public:
135 void
136 run() override
137 {
138 testAddJob();
139 testPostCoro();
140 }
141};
142
143BEAST_DEFINE_TESTSUITE(JobQueue, core, xrpl);
144
145} // namespace test
146} // namespace xrpl
A testsuite class.
Definition suite.h:52
A pool of threads to perform work.
Definition JobQueue.h:38
std::shared_ptr< Coro > postCoro(JobType t, std::string const &name, F &&f)
Creates a coroutine and adds a job to the queue which will run it.
Definition JobQueue.h:393
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition JobQueue.h:148
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:102
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ jtCLIENT
Definition Job.h:25