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