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(jQueue.addJob(jtCLIENT, "JobAddTest1", [&jobRan]() {
41  jobRan = true;
42  }) == true);
43 
44  // Wait for the Job to run.
45  while (jobRan == false)
46  ;
47  }
48  {
49  // If the JobQueue is stopped, we should no
50  // longer be able to add Jobs (and calling addJob() should
51  // return false).
52  using namespace std::chrono_literals;
53  jQueue.stop();
54 
55  // The Job should never run, so having the Job access this
56  // unprotected variable on the stack should be completely safe.
57  // Not recommended for the faint of heart...
58  bool unprotected;
59  BEAST_EXPECT(
60  jQueue.addJob(jtCLIENT, "JobAddTest2", [&unprotected]() {
61  unprotected = false;
62  }) == false);
63  }
64  }
65 
66  void
68  {
69  jtx::Env env{*this};
70 
71  JobQueue& jQueue = env.app().getJobQueue();
72  {
73  // Test repeated post()s until the Coro completes.
74  std::atomic<int> yieldCount{0};
75  auto const coro = jQueue.postCoro(
76  jtCLIENT,
77  "PostCoroTest1",
78  [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
79  while (++yieldCount < 4)
80  coroCopy->yield();
81  });
82  BEAST_EXPECT(coro != nullptr);
83 
84  // Wait for the Job to run and yield.
85  while (yieldCount == 0)
86  ;
87 
88  // Now re-post until the Coro says it is done.
89  int old = yieldCount;
90  while (coro->runnable())
91  {
92  BEAST_EXPECT(coro->post());
93  while (old == yieldCount)
94  {
95  }
96  coro->join();
97  BEAST_EXPECT(++old == yieldCount);
98  }
99  BEAST_EXPECT(yieldCount == 4);
100  }
101  {
102  // Test repeated resume()s until the Coro completes.
103  int yieldCount{0};
104  auto const coro = jQueue.postCoro(
105  jtCLIENT,
106  "PostCoroTest2",
107  [&yieldCount](std::shared_ptr<JobQueue::Coro> const& coroCopy) {
108  while (++yieldCount < 4)
109  coroCopy->yield();
110  });
111  if (!coro)
112  {
113  // There's no good reason we should not get a Coro, but we
114  // can't continue without one.
115  BEAST_EXPECT(false);
116  return;
117  }
118 
119  // Wait for the Job to run and yield.
120  coro->join();
121 
122  // Now resume until the Coro says it is done.
123  int old = yieldCount;
124  while (coro->runnable())
125  {
126  coro->resume(); // Resume runs synchronously on this thread.
127  BEAST_EXPECT(++old == yieldCount);
128  }
129  BEAST_EXPECT(yieldCount == 4);
130  }
131  {
132  // If the JobQueue is stopped, we should no
133  // longer be able to add a Coro (and calling postCoro() should
134  // return false).
135  using namespace std::chrono_literals;
136  jQueue.stop();
137 
138  // The Coro should never run, so having the Coro access this
139  // unprotected variable on the stack should be completely safe.
140  // Not recommended for the faint of heart...
141  bool unprotected;
142  auto const coro = jQueue.postCoro(
143  jtCLIENT,
144  "PostCoroTest3",
145  [&unprotected](std::shared_ptr<JobQueue::Coro> const&) {
146  unprotected = false;
147  });
148  BEAST_EXPECT(coro == nullptr);
149  }
150  }
151 
152 public:
153  void
154  run() override
155  {
156  testAddJob();
157  testPostCoro();
158  }
159 };
160 
162 
163 } // namespace test
164 } // 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:411
ripple::jtCLIENT
@ jtCLIENT
Definition: Job.h:45
ripple::test::JobQueue_test::run
void run() override
Definition: JobQueue_test.cpp:154
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::test::JobQueue_test::testPostCoro
void testPostCoro()
Definition: JobQueue_test.cpp:67
std::atomic< bool >
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::JobQueue::stop
void stop()
Definition: JobQueue.cpp:275
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)