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