xrpld
Loading...
Searching...
No Matches
JobQueue_test.cpp
1#include <test/jtx/Env.h>
2
3#include <xrpl/beast/unit_test/suite.h>
4#include <xrpl/core/Job.h>
5#include <xrpl/core/JobQueue.h>
6
7#include <atomic>
8#include <memory>
9
10namespace xrpl::test {
11
12//------------------------------------------------------------------------------
13
15{
16 void
18 {
19 jtx::Env env{*this};
20
21 JobQueue& jQueue = env.app().getJobQueue();
22 {
23 // addJob() should run the Job (and return true).
24 std::atomic<bool> jobRan{false};
25 BEAST_EXPECT(
26 jQueue.addJob(JtClient, "JobAddTest1", [&jobRan]() { jobRan = true; }) == true);
27
28 // Wait for the Job to run.
29 while (!jobRan)
30 ;
31 }
32 {
33 // If the JobQueue is stopped, we should no
34 // longer be able to add Jobs (and calling addJob() should
35 // return false).
36 using namespace std::chrono_literals;
37 jQueue.stop();
38
39 // The Job should never run, so having the Job access this
40 // unprotected variable on the stack should be completely safe.
41 // Not recommended for the faint of heart...
42 bool unprotected = false;
43 BEAST_EXPECT(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 = false;
125 auto const coro = jQueue.postCoro(
126 JtClient, "PostCoroTest3", [&unprotected](std::shared_ptr<JobQueue::Coro> const&) {
127 unprotected = false;
128 });
129 BEAST_EXPECT(coro == nullptr);
130 }
131 }
132
133public:
134 void
135 run() override
136 {
137 testAddJob();
138 testPostCoro();
139 }
140};
141
143
144} // namespace xrpl::test
A testsuite class.
Definition suite.h:50
A pool of threads to perform work.
Definition JobQueue.h:43
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:150
virtual JobQueue & getJobQueue()=0
void run() override
Runs the suite.
A transaction testing environment.
Definition Env.h:143
Application & app()
Definition Env.h:280
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ JtClient
Definition Job.h:26