rippled
JobQueue_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2017 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/beast/unit_test.h>
21 #include <ripple/core/JobQueue.h>
22 #include <test/jtx/Env.h>
23 
24 namespace ripple {
25 namespace test {
26 
27 //------------------------------------------------------------------------------
28 
29 class JobQueue_test : public beast::unit_test::suite
30 {
31  void
33  {
34  jtx::Env env{*this};
35 
36  JobQueue& jQueue = env.app().getJobQueue();
37  {
38  // addJob() should run the Job (and return true).
39  std::atomic<bool> jobRan{false};
40  BEAST_EXPECT(
41  jQueue.addJob(jtCLIENT, "JobAddTest1", [&jobRan](Job&) {
42  jobRan = true;
43  }) == true);
44 
45  // Wait for the Job to run.
46  while (jobRan == false)
47  ;
48  }
49  {
50  // If the JobQueue is stopped, we should no
51  // longer be able to add Jobs (and calling addJob() should
52  // return false).
53  using namespace std::chrono_literals;
54  jQueue.stop();
55 
56  // The Job should never run, so having the Job access this
57  // unprotected variable on the stack should be completely safe.
58  // Not recommended for the faint of heart...
59  bool unprotected;
60  BEAST_EXPECT(
61  jQueue.addJob(jtCLIENT, "JobAddTest2", [&unprotected](Job&) {
62  unprotected = false;
63  }) == false);
64  }
65  }
66 
67  void
69  {
70  jtx::Env env{*this};
71 
72  JobQueue& jQueue = env.app().getJobQueue();
73  {
74  // Test repeated post()s until the Coro completes.
75  std::atomic<int> yieldCount{0};
76  auto const coro = jQueue.postCoro(
77  jtCLIENT,
78  "PostCoroTest1",
79  [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
80  while (++yieldCount < 4)
81  coroCopy->yield();
82  });
83  BEAST_EXPECT(coro != nullptr);
84 
85  // Wait for the Job to run and yield.
86  while (yieldCount == 0)
87  ;
88 
89  // Now re-post until the Coro says it is done.
90  int old = yieldCount;
91  while (coro->runnable())
92  {
93  BEAST_EXPECT(coro->post());
94  while (old == yieldCount)
95  {
96  }
97  coro->join();
98  BEAST_EXPECT(++old == yieldCount);
99  }
100  BEAST_EXPECT(yieldCount == 4);
101  }
102  {
103  // Test repeated resume()s until the Coro completes.
104  int yieldCount{0};
105  auto const coro = jQueue.postCoro(
106  jtCLIENT,
107  "PostCoroTest2",
108  [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
109  while (++yieldCount < 4)
110  coroCopy->yield();
111  });
112  if (!coro)
113  {
114  // There's no good reason we should not get a Coro, but we
115  // can't continue without one.
116  BEAST_EXPECT(false);
117  return;
118  }
119 
120  // Wait for the Job to run and yield.
121  coro->join();
122 
123  // Now resume until the Coro says it is done.
124  int old = yieldCount;
125  while (coro->runnable())
126  {
127  coro->resume(); // Resume runs synchronously on this thread.
128  BEAST_EXPECT(++old == yieldCount);
129  }
130  BEAST_EXPECT(yieldCount == 4);
131  }
132  {
133  // If the JobQueue is stopped, we should no
134  // longer be able to add a Coro (and calling postCoro() should
135  // return false).
136  using namespace std::chrono_literals;
137  jQueue.stop();
138 
139  // The Coro should never run, so having the Coro access this
140  // unprotected variable on the stack should be completely safe.
141  // Not recommended for the faint of heart...
142  bool unprotected;
143  auto const coro = jQueue.postCoro(
144  jtCLIENT,
145  "PostCoroTest3",
146  [&unprotected](std::shared_ptr<JobQueue::Coro> const&) {
147  unprotected = false;
148  });
149  BEAST_EXPECT(coro == nullptr);
150  }
151  }
152 
153 public:
154  void
155  run() override
156  {
157  testAddJob();
158  testPostCoro();
159  }
160 };
161 
163 
164 } // namespace test
165 } // namespace ripple
std::shared_ptr
STL class.
ripple::JobQueue::postCoro
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:429
ripple::jtCLIENT
@ jtCLIENT
Definition: Job.h:44
ripple::test::JobQueue_test::run
void run() override
Definition: JobQueue_test.cpp:155
ripple::JobQueue::addJob
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
ripple::test::JobQueue_test
Definition: JobQueue_test.cpp:29
ripple::Job
Definition: Job.h:87
ripple::test::JobQueue_test::testPostCoro
void testPostCoro()
Definition: JobQueue_test.cpp:68
std::atomic< bool >
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::JobQueue::stop
void stop()
Definition: JobQueue.cpp:262
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::JobQueue_test::testAddJob
void testAddJob()
Definition: JobQueue_test.cpp:32
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:116
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)