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