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