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 <test/jtx.h>
22 #include <chrono>
23 #include <condition_variable>
24 #include <mutex>
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",
72  [&](auto const& cr)
73  {
74  c = cr;
75  g1.signal();
76  c->yield();
77  g2.signal();
78  });
79  BEAST_EXPECT(g1.wait_for(5s));
80  c->join();
81  c->post();
82  BEAST_EXPECT(g2.wait_for(5s));
83  }
84 
85  void
87  {
88  using namespace std::chrono_literals;
89  using namespace jtx;
90  Env env(*this);
91  auto& jq = env.app().getJobQueue();
92  jq.setThreadCount(0, false);
93  gate g;
94  jq.postCoro(jtCLIENT, "Coroutine-Test",
95  [&](auto const& c)
96  {
97  c->post();
98  c->yield();
99  g.signal();
100  });
101  BEAST_EXPECT(g.wait_for(5s));
102  }
103 
104  void
106  {
107  using namespace std::chrono_literals;
108  using namespace jtx;
109  Env env(*this);
110  auto& jq = env.app().getJobQueue();
111  jq.setThreadCount(0, true);
112  static int const N = 4;
114 
115  LocalValue<int> lv(-1);
116  BEAST_EXPECT(*lv == -1);
117 
118  gate g;
119  jq.addJob(jtCLIENT, "LocalValue-Test",
120  [&](auto const& job)
121  {
122  this->BEAST_EXPECT(*lv == -1);
123  *lv = -2;
124  this->BEAST_EXPECT(*lv == -2);
125  g.signal();
126  });
127  BEAST_EXPECT(g.wait_for(5s));
128  BEAST_EXPECT(*lv == -1);
129 
130  for(int i = 0; i < N; ++i)
131  {
132  jq.postCoro(jtCLIENT, "Coroutine-Test",
133  [&, id = i](auto const& c)
134  {
135  a[id] = c;
136  g.signal();
137  c->yield();
138 
139  this->BEAST_EXPECT(*lv == -1);
140  *lv = id;
141  this->BEAST_EXPECT(*lv == id);
142  g.signal();
143  c->yield();
144 
145  this->BEAST_EXPECT(*lv == id);
146  });
147  BEAST_EXPECT(g.wait_for(5s));
148  a[i]->join();
149  }
150  for(auto const& c : a)
151  {
152  c->post();
153  BEAST_EXPECT(g.wait_for(5s));
154  c->join();
155  }
156  for(auto const& c : a)
157  {
158  c->post();
159  c->join();
160  }
161 
162  jq.addJob(jtCLIENT, "LocalValue-Test",
163  [&](auto const& job)
164  {
165  this->BEAST_EXPECT(*lv == -2);
166  g.signal();
167  });
168  BEAST_EXPECT(g.wait_for(5s));
169  BEAST_EXPECT(*lv == -1);
170  }
171 
172  void
173  run() override
174  {
175  correct_order();
176  incorrect_order();
178  }
179 };
180 
181 BEAST_DEFINE_TESTSUITE(Coroutine,core,ripple);
182 
183 } // test
184 } // 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:173
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
std::shared_ptr
STL class.
ripple::jtCLIENT
@ jtCLIENT
Definition: Job.h:49
ripple::test::Coroutine_test::thread_specific_storage
void thread_specific_storage()
Definition: Coroutine_test.cpp:105
std::chrono::duration
std::lock_guard
STL class.
ripple::test::jtx::Env::app
Application & app()
Definition: Env.h:237
ripple::test::Coroutine_test::incorrect_order
void incorrect_order()
Definition: Coroutine_test.cpp:86
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:82
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:117