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