rippled
Coroutine_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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 <chrono>
22 #include <condition_variable>
23 #include <mutex>
24 #include <test/jtx.h>
25 
26 namespace ripple {
27 namespace test {
28 
29 class Coroutine_test : public beast::unit_test::suite
30 {
31 public:
32  class gate
33  {
34  private:
37  bool signaled_ = false;
38 
39  public:
40  // Thread safe, blocks until signaled or period expires.
41  // Returns `true` if signaled.
42  template <class Rep, class Period>
43  bool
45  {
47  auto b = cv_.wait_for(lk, rel_time, [=] { return signaled_; });
48  signaled_ = false;
49  return b;
50  }
51 
52  void
54  {
56  signaled_ = true;
57  cv_.notify_all();
58  }
59  };
60 
61  void
63  {
64  using namespace std::chrono_literals;
65  using namespace jtx;
66  Env env(*this);
67  auto& jq = env.app().getJobQueue();
68  jq.setThreadCount(0, false);
69  gate g1, g2;
71  jq.postCoro(jtCLIENT, "Coroutine-Test", [&](auto const& cr) {
72  c = cr;
73  g1.signal();
74  c->yield();
75  g2.signal();
76  });
77  BEAST_EXPECT(g1.wait_for(5s));
78  c->join();
79  c->post();
80  BEAST_EXPECT(g2.wait_for(5s));
81  }
82 
83  void
85  {
86  using namespace std::chrono_literals;
87  using namespace jtx;
88  Env env(*this);
89  auto& jq = env.app().getJobQueue();
90  jq.setThreadCount(0, false);
91  gate g;
92  jq.postCoro(jtCLIENT, "Coroutine-Test", [&](auto const& c) {
93  c->post();
94  c->yield();
95  g.signal();
96  });
97  BEAST_EXPECT(g.wait_for(5s));
98  }
99 
100  void
102  {
103  using namespace std::chrono_literals;
104  using namespace jtx;
105  Env env(*this);
106  auto& jq = env.app().getJobQueue();
107  jq.setThreadCount(0, true);
108  static int const N = 4;
110 
111  LocalValue<int> lv(-1);
112  BEAST_EXPECT(*lv == -1);
113 
114  gate g;
115  jq.addJob(jtCLIENT, "LocalValue-Test", [&](auto const& job) {
116  this->BEAST_EXPECT(*lv == -1);
117  *lv = -2;
118  this->BEAST_EXPECT(*lv == -2);
119  g.signal();
120  });
121  BEAST_EXPECT(g.wait_for(5s));
122  BEAST_EXPECT(*lv == -1);
123 
124  for (int i = 0; i < N; ++i)
125  {
126  jq.postCoro(jtCLIENT, "Coroutine-Test", [&, id = i](auto const& c) {
127  a[id] = c;
128  g.signal();
129  c->yield();
130 
131  this->BEAST_EXPECT(*lv == -1);
132  *lv = id;
133  this->BEAST_EXPECT(*lv == id);
134  g.signal();
135  c->yield();
136 
137  this->BEAST_EXPECT(*lv == id);
138  });
139  BEAST_EXPECT(g.wait_for(5s));
140  a[i]->join();
141  }
142  for (auto const& c : a)
143  {
144  c->post();
145  BEAST_EXPECT(g.wait_for(5s));
146  c->join();
147  }
148  for (auto const& c : a)
149  {
150  c->post();
151  c->join();
152  }
153 
154  jq.addJob(jtCLIENT, "LocalValue-Test", [&](auto const& job) {
155  this->BEAST_EXPECT(*lv == -2);
156  g.signal();
157  });
158  BEAST_EXPECT(g.wait_for(5s));
159  BEAST_EXPECT(*lv == -1);
160  }
161 
162  void
163  run() override
164  {
165  correct_order();
166  incorrect_order();
168  }
169 };
170 
171 BEAST_DEFINE_TESTSUITE(Coroutine, core, ripple);
172 
173 } // namespace test
174 } // namespace ripple
ripple::test::Coroutine_test::gate::cv_
std::condition_variable cv_
Definition: Coroutine_test.cpp:35
ripple::test::Coroutine_test::run
void run() override
Definition: Coroutine_test.cpp:163
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
std::shared_ptr
STL class.
ripple::jtCLIENT
@ jtCLIENT
Definition: Job.h:48
ripple::test::Coroutine_test::thread_specific_storage
void thread_specific_storage()
Definition: Coroutine_test.cpp:101
std::chrono::duration
std::lock_guard
STL class.
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:238
ripple::test::Coroutine_test::incorrect_order
void incorrect_order()
Definition: Coroutine_test.cpp:84
ripple::test::Coroutine_test::gate::signaled_
bool signaled_
Definition: Coroutine_test.cpp:37
chrono
std::unique_lock
STL class.
ripple::Application::getJobQueue
virtual JobQueue & getJobQueue()=0
std::array
STL class.
ripple::test::Coroutine_test::gate::wait_for
bool wait_for(std::chrono::duration< Rep, Period > const &rel_time)
Definition: Coroutine_test.cpp:44
std::condition_variable::wait_for
T wait_for(T... args)
ripple::test::Coroutine_test::correct_order
void correct_order()
Definition: Coroutine_test.cpp:62
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::LocalValue
Definition: LocalValue.h:84
ripple::JobQueue::setThreadCount
void setThreadCount(int c, bool const standaloneMode)
Set the number of thread serving the job queue to precisely this number.
Definition: JobQueue.cpp:158
condition_variable
ripple::test::Coroutine_test::gate::mutex_
std::mutex mutex_
Definition: Coroutine_test.cpp:36
ripple::test::Coroutine_test::gate::signal
void signal()
Definition: Coroutine_test.cpp:53
mutex
ripple::test::Coroutine_test
Definition: Coroutine_test.cpp:29
ripple::test::Coroutine_test::gate
Definition: Coroutine_test.cpp:32
std::condition_variable::notify_all
T notify_all(T... args)
ripple::test::jtx::Env
A transaction testing environment.
Definition: Env.h:114