rippled
Stoppable_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/Stoppable.h>
21 #include <ripple/beast/unit_test.h>
22 #include <test/unit_test/SuiteJournal.h>
23 #include <thread>
24 
25 namespace ripple {
26 namespace test {
27 
29  : public beast::unit_test::suite
30 {
31 /*
32  R
33  / | \
34  / | \
35  A B C
36  / | \ /\ |
37  D E F G H I
38  |
39  J
40 */
41  unsigned count = 0;
42 
43  class D
44  : public Stoppable
45  {
47  public:
48  D(Stoppable& parent, Stoppable_test& test)
49  : Stoppable("D", parent)
50  , test_(test)
51  {}
52 
53  void onPrepare() override
54  {
55  test_.expect(++test_.count == 9, "D::onPrepare called out of order");
56  }
57 
58  void onStart() override
59  {
60  test_.expect(--test_.count == 0, "D::onStart called out of order");
61  }
62 
63  void onStop() override
64  {
65  test_.expect(++test_.count == 11, "D::onStop called out of order");
66  }
67 
68  void onChildrenStopped() override
69  {
71  test_.expect(--test_.count == 2, "D::onChildrenStopped called out of order");
72  }
73  };
74 
75  class J
76  : public Stoppable
77  {
79  public:
80  J(Stoppable& parent, Stoppable_test& test)
81  : Stoppable("J", parent)
82  , test_(test)
83  {}
84 
85  void onPrepare() override
86  {
87  test_.expect(++test_.count == 7, "J::onPrepare called out of order");
88  }
89 
90  void onStart() override
91  {
92  test_.expect(--test_.count == 1, "J::onStart called out of order");
93  }
94 
95  void onStop() override
96  {
97  test_.expect(++test_.count == 10, "J::onStop called out of order");
98  }
99 
100  void onChildrenStopped() override
101  {
103  test_.expect(--test_.count == 4, "J::onChildrenStopped called out of order");
104  }
105  };
106 
107  class E
108  : public Stoppable
109  {
110  J j_;
112  public:
113  E(Stoppable& parent, Stoppable_test& test)
114  : Stoppable("E", parent)
115  , j_(*this, test)
116  , test_(test)
117  {}
118 
119  void onPrepare() override
120  {
121  test_.expect(++test_.count == 8, "E::onPrepare called out of order");
122  }
123 
124  void onStart() override
125  {
126  test_.expect(--test_.count == 2, "E::onStart called out of order");
127  }
128 
129  void onStop() override
130  {
131  test_.expect(++test_.count == 9, "E::onStop called out of order");
132  }
133 
134  void onChildrenStopped() override
135  {
137  test_.expect(--test_.count == 3, "E::onChildrenStopped called out of order");
138  }
139  };
140 
141  class F
142  : public Stoppable
143  {
145  public:
146  F(Stoppable& parent, Stoppable_test& test)
147  : Stoppable("F", parent)
148  , test_(test)
149  {}
150 
151  void onPrepare() override
152  {
153  test_.expect(++test_.count == 6, "F::onPrepare called out of order");
154  }
155 
156  void onStart() override
157  {
158  test_.expect(--test_.count == 3, "F::onStart called out of order");
159  }
160 
161  void onStop() override
162  {
163  test_.expect(++test_.count == 8, "F::onStop called out of order");
164  }
165 
166  void onChildrenStopped() override
167  {
169  test_.expect(--test_.count == 5, "F::onChildrenStopped called out of order");
170  }
171  };
172 
173  class A
174  : public Stoppable
175  {
177  D d_;
178  E e_;
179  F f_;
182  public:
183  A(Stoppable& parent, Stoppable_test& test)
184  : Stoppable("A", parent)
185  , d_(*this, test)
186  , e_(*this, test)
187  , f_(*this, test)
188  , test_(test)
189  , stop_(running)
190  {}
191  ~A() override
192  {
193  while (stop_ != stopped)
194  ;
195  }
196 
197  void run()
198  {
199  while (stop_ == running)
200  ;
201  stop_ = stopping;
202  }
203 
204  void onPrepare() override
205  {
206  test_.expect(++test_.count == 10, "A::onPrepare called out of order");
207  }
208 
209  void onStart() override
210  {
211  test_.expect(--test_.count == 4, "A::onStart called out of order");
212  }
213 
214  void onStop() override
215  {
216  test_.expect(++test_.count == 7, "A::onStop called out of order");
217  }
218 
219  void onChildrenStopped() override
220  {
221  stop_ = please_stop;
222  while (stop_ != stopping)
223  ;
225  test_.expect(--test_.count == 1, "A::onChildrenStopped called out of order");
226  stop_ = stopped;
227  }
228  };
229 
230  class G
231  : public Stoppable
232  {
234  public:
235  G(Stoppable& parent, Stoppable_test& test)
236  : Stoppable("G", parent)
237  , test_(test)
238  {}
239 
240  void onPrepare() override
241  {
242  test_.expect(++test_.count == 4, "G::onPrepare called out of order");
243  }
244 
245  void onStart() override
246  {
247  test_.expect(--test_.count == 5, "G::onStart called out of order");
248  }
249 
250  void onStop() override
251  {
252  test_.expect(++test_.count == 6, "G::onStop called out of order");
253  }
254 
255  void onChildrenStopped() override
256  {
258  test_.expect(--test_.count == 7, "G::onChildrenStopped called out of order");
259  }
260  };
261 
262  class H
263  : public Stoppable
264  {
266  public:
267  H(Stoppable& parent, Stoppable_test& test)
268  : Stoppable("H", parent)
269  , test_(test)
270  {}
271 
272  void onPrepare() override
273  {
274  test_.expect(++test_.count == 3, "H::onPrepare called out of order");
275  }
276 
277  void onStart() override
278  {
279  test_.expect(--test_.count == 6, "H::onStart called out of order");
280  }
281 
282  void onStop() override
283  {
284  test_.expect(++test_.count == 5, "H::onStop called out of order");
285  }
286 
287  void onChildrenStopped() override
288  {
290  test_.expect(--test_.count == 8, "H::onChildrenStopped called out of order");
291  }
292  };
293 
294  class B
295  : public Stoppable
296  {
297  G g_;
298  H h_;
300  public:
301  B(Stoppable& parent, Stoppable_test& test)
302  : Stoppable("B", parent)
303  , g_(*this, test)
304  , h_(*this, test)
305  , test_(test)
306  {}
307 
308  void onPrepare() override
309  {
310  test_.expect(++test_.count == 5, "B::onPrepare called out of order");
311  }
312 
313  void onStart() override
314  {
315  test_.expect(--test_.count == 7, "B::onStart called out of order");
316  }
317 
318  void onStop() override
319  {
320  test_.expect(++test_.count == 4, "B::onStop called out of order");
321  }
322 
323  void onChildrenStopped() override
324  {
326  test_.expect(--test_.count == 6, "B::onChildrenStopped called out of order");
327  }
328  };
329 
330  class I
331  : public Stoppable
332  {
334  public:
335  I(Stoppable& parent, Stoppable_test& test)
336  : Stoppable("I", parent)
337  , test_(test)
338  {}
339 
340  void onPrepare() override
341  {
342  test_.expect(++test_.count == 1, "I::onPrepare called out of order");
343  }
344 
345  void onStart() override
346  {
347  test_.expect(--test_.count == 8, "I::onStart called out of order");
348  }
349 
350  void onStop() override
351  {
352  test_.expect(++test_.count == 3, "I::onStop called out of order");
353  }
354 
355  void onChildrenStopped() override
356  {
358  test_.expect(--test_.count == 10, "I::onChildrenStopped called out of order");
359  }
360  };
361 
362  class C
363  : public Stoppable
364  {
365  I i_;
367  public:
368  C(Stoppable& parent, Stoppable_test& test)
369  : Stoppable("C", parent)
370  , i_(*this, test)
371  , test_(test)
372  {}
373 
374  void onPrepare() override
375  {
376  test_.expect(++test_.count == 2, "C::onPrepare called out of order");
377  }
378 
379  void onStart() override
380  {
381  test_.expect(--test_.count == 9, "C::onStart called out of order");
382  }
383 
384  void onStop() override
385  {
386  test_.expect(++test_.count == 2, "C::onStop called out of order");
387  }
388 
389  void onChildrenStopped() override
390  {
392  test_.expect(--test_.count == 9, "C::onChildrenStopped called out of order");
393  }
394  };
395 
396  class Root
397  : public RootStoppable
398  {
400  B b_;
401  C c_;
404 
405  public:
406  explicit Root(Stoppable_test& test)
407  : RootStoppable("R")
408  , a_(&A::run, std::make_unique<A>(*this, test))
409  , b_(*this, test)
410  , c_(*this, test)
411  , test_(test)
412  , journal_("Stoppable_test", test)
413  {}
414 
415  void run()
416  {
417  prepare();
418  start();
419  stop (journal_);
420  }
421 
422  void onPrepare() override
423  {
424  test_.expect(++test_.count == 11, "Root::onPrepare called out of order");
425  }
426 
427  void onStart() override
428  {
429  test_.expect(--test_.count == 10, "Root::onStart called out of order");
430  }
431 
432  void onStop() override
433  {
434  test_.expect(++test_.count == 1, "Root::onStop called out of order");
435  }
436 
437  void onChildrenStopped() override
438  {
439  a_.join();
441  test_.expect(--test_.count == 0, "Root::onChildrenStopped called out of order");
442  }
443 
444  void secondStop()
445  {
446  // Calling stop() a second time should have no negative
447  // consequences.
448  stop (journal_);
449  }
450  };
451 
452 public:
453  void run() override
454  {
455  {
456  Root rt(*this);
457  rt.run();
458  rt.secondStop();
459  }
460  pass();
461  }
462 };
463 
465 
466 }
467 }
ripple::test::Stoppable_test::G::G
G(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:235
ripple::test::Stoppable_test::G::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:240
ripple::test::Stoppable_test::A::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:180
ripple::test::Stoppable_test::A::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:214
ripple::test::Stoppable_test::J::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:85
ripple::test::Stoppable_test::D::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:63
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountDelete, app, ripple)
ripple::test::Stoppable_test::C::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:374
ripple::test::Stoppable_test::Root::run
void run()
Definition: Stoppable_test.cpp:415
ripple::test::Stoppable_test::C::C
C(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:368
ripple::test::Stoppable_test::J
Definition: Stoppable_test.cpp:75
ripple::Stoppable::stopped
void stopped()
Called by derived classes to indicate that the stoppable has stopped.
Definition: Stoppable.cpp:71
ripple::test::Stoppable_test::Root::c_
C c_
Definition: Stoppable_test.cpp:401
ripple::test::Stoppable_test::Root::journal_
SuiteJournal journal_
Definition: Stoppable_test.cpp:403
ripple::test::Stoppable_test::F::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:161
ripple::test::Stoppable_test::D::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:46
ripple::test::Stoppable_test::run
void run() override
Definition: Stoppable_test.cpp:453
ripple::test::Stoppable_test::E
Definition: Stoppable_test.cpp:107
ripple::test::Stoppable_test::B::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:323
ripple::test::Stoppable_test::F::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:151
ripple::test::Stoppable_test::J::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:90
ripple::test::Stoppable_test::G::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:233
ripple::test::Stoppable_test::H::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:272
ripple::test::Stoppable_test::Root::b_
B b_
Definition: Stoppable_test.cpp:400
ripple::test::Stoppable_test::A::stopping
@ stopping
Definition: Stoppable_test.cpp:176
ripple::test::Stoppable_test::C::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:384
ripple::test::Stoppable_test::B::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:318
ripple::test::Stoppable_test::A::~A
~A() override
Definition: Stoppable_test.cpp:191
ripple::test::Stoppable_test::H::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:282
ripple::test::Stoppable_test::A::d_
D d_
Definition: Stoppable_test.cpp:177
ripple::test::Stoppable_test::Root::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:422
ripple::test::Stoppable_test::A::run
void run()
Definition: Stoppable_test.cpp:197
ripple::test::Stoppable_test::A::running
@ running
Definition: Stoppable_test.cpp:176
ripple::test::Stoppable_test::G::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:250
ripple::test::Stoppable_test::I
Definition: Stoppable_test.cpp:330
ripple::test::Stoppable_test::I::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:355
ripple::test::Stoppable_test::H::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:287
ripple::test::Stoppable_test::B::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:299
ripple::test::Stoppable_test::Root::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:437
ripple::test::Stoppable_test::A::e_
E e_
Definition: Stoppable_test.cpp:178
ripple::test::Stoppable_test::F::F
F(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:146
ripple::test::Stoppable_test::B
Definition: Stoppable_test.cpp:294
ripple::RootStoppable::prepare
void prepare()
Prepare all contained Stoppable objects.
Definition: Stoppable.cpp:166
ripple::test::Stoppable_test::D::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:68
ripple::test::Stoppable_test::J::J
J(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:80
ripple::test::Stoppable_test::D
Definition: Stoppable_test.cpp:43
ripple::test::Stoppable_test::Root::Root
Root(Stoppable_test &test)
Definition: Stoppable_test.cpp:406
ripple::test::Stoppable_test::E::j_
J j_
Definition: Stoppable_test.cpp:110
ripple::test::Stoppable_test::I::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:333
ripple::RootStoppable::stop
void stop(beast::Journal j)
Notify a root stoppable and children to stop, and block until stopped.
Definition: Stoppable.cpp:182
ripple::test::Stoppable_test::B::g_
G g_
Definition: Stoppable_test.cpp:297
ripple::test::Stoppable_test::A::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:204
ripple::Stoppable
Provides an interface for starting and stopping.
Definition: Stoppable.h:200
ripple::test::Stoppable_test::D::D
D(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:48
ripple::test::Stoppable_test::B::B
B(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:301
ripple::test::Stoppable_test::A::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:209
ripple::test::Stoppable_test::Root
Definition: Stoppable_test.cpp:396
ripple::test::Stoppable_test::F::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:156
ripple::RootStoppable
Definition: Stoppable.h:336
thread
ripple::test::Stoppable_test::H::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:277
ripple::test::Stoppable_test::F::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:144
ripple::test::Stoppable_test::J::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:100
ripple::test::Stoppable_test::C::i_
I i_
Definition: Stoppable_test.cpp:365
ripple::test::Stoppable_test::A::f_
F f_
Definition: Stoppable_test.cpp:179
ripple::test::Stoppable_test::D::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:53
ripple::test::Stoppable_test::E::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:119
ripple::test::Stoppable_test::H
Definition: Stoppable_test.cpp:262
ripple::test::Stoppable_test::A::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:219
ripple::test::Stoppable_test::A::stop_
std::atomic< int > stop_
Definition: Stoppable_test.cpp:181
ripple::test::Stoppable_test::I::I
I(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:335
ripple::test::Stoppable_test::A
Definition: Stoppable_test.cpp:173
ripple::test::Stoppable_test::B::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:308
ripple::test::Stoppable_test::I::onPrepare
void onPrepare() override
Override called during preparation.
Definition: Stoppable_test.cpp:340
ripple::test::Stoppable_test::E::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:129
ripple::test::Stoppable_test::count
unsigned count
Definition: Stoppable_test.cpp:41
std::atomic< int >
ripple::test::Stoppable_test::C
Definition: Stoppable_test.cpp:362
ripple::test::Stoppable_test::C::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:366
ripple::test::Stoppable_test::I::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:350
ripple::test::SuiteJournal
Definition: SuiteJournal.h:81
ripple::test::Stoppable_test::E::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:111
ripple::test::Stoppable_test::Root::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:402
ripple::test::Stoppable_test
Definition: Stoppable_test.cpp:28
ripple::RootStoppable::start
void start()
Start all contained Stoppable objects.
Definition: Stoppable.cpp:172
ripple::test::Stoppable_test::A::A
A(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:183
ripple::test::Stoppable_test::I::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:345
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::Stoppable_test::E::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:134
ripple::test::Stoppable_test::Root::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:427
ripple::test::Stoppable_test::B::h_
H h_
Definition: Stoppable_test.cpp:298
ripple::test::Stoppable_test::G
Definition: Stoppable_test.cpp:230
ripple::test::Stoppable_test::B::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:313
std
STL namespace.
ripple::test::Stoppable_test::G::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:245
ripple::test::Stoppable_test::A::please_stop
@ please_stop
Definition: Stoppable_test.cpp:176
ripple::test::Stoppable_test::F::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:166
ripple::test::Stoppable_test::A::stopped
@ stopped
Definition: Stoppable_test.cpp:176
ripple::test::Stoppable_test::E::E
E(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:113
ripple::test::Stoppable_test::J::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:95
ripple::test::Stoppable_test::Root::onStop
void onStop() override
Override called when the stop notification is issued.
Definition: Stoppable_test.cpp:432
ripple::test::Stoppable_test::C::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:379
ripple::test::Stoppable_test::Root::a_
std::thread a_
Definition: Stoppable_test.cpp:399
ripple::test::Stoppable_test::H::H
H(Stoppable &parent, Stoppable_test &test)
Definition: Stoppable_test.cpp:267
ripple::test::Stoppable_test::C::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:389
ripple::test::Stoppable_test::H::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:265
ripple::test::Stoppable_test::E::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:124
ripple::test::Stoppable_test::F
Definition: Stoppable_test.cpp:141
ripple::test::Stoppable_test::G::onChildrenStopped
void onChildrenStopped() override
Override called when all children have stopped.
Definition: Stoppable_test.cpp:255
ripple::test::Stoppable_test::D::onStart
void onStart() override
Override called during start.
Definition: Stoppable_test.cpp:58
std::thread::join
T join(T... args)
ripple::test::Stoppable_test::Root::secondStop
void secondStop()
Definition: Stoppable_test.cpp:444
ripple::test::Stoppable_test::J::test_
Stoppable_test & test_
Definition: Stoppable_test.cpp:78