rippled
ThreadName_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/basics/ThreadUtilities.h>
21 #include <ripple/beast/unit_test.h>
22 
23 namespace ripple {
24 namespace test {
25 
26 class ThreadName_test : public beast::unit_test::suite
27 {
28 private:
29  static void
31  std::string myName,
32  std::atomic<bool>* stop,
33  std::atomic<int>* state)
34  {
35  // Set the new name.
36  this_thread::set_name(myName);
37 
38  // Indicate to caller that the name is set.
39  *state = 1;
40 
41  // Wait until all threads have their names.
42  while (!*stop)
43  ;
44 
45  // Make sure the thread name that we set before is still there
46  // (not overwritten by, for instance, another thread).
47  if (this_thread::get_name() == myName)
48  *state = 2;
49  }
50 
51 public:
52  void
53  run() override
54  {
55  // Make two different threads with two different names. Make sure
56  // that the expected thread names are still there when the thread
57  // exits.
58  std::atomic<bool> stop{false};
59 
60  std::atomic<int> stateA{0};
61  std::thread tA(exerciseName, "tA", &stop, &stateA);
62 
63  std::atomic<int> stateB{0};
64  std::thread tB(exerciseName, "tB", &stop, &stateB);
65 
66  // Wait until both threads have set their names.
67  while (stateA == 0 || stateB == 0)
68  ;
69 
70  stop = true;
71  tA.join();
72  tB.join();
73 
74  // Both threads should still have the expected name when they exit.
75  BEAST_EXPECT(stateA == 2);
76  BEAST_EXPECT(stateB == 2);
77  }
78 };
79 
80 BEAST_DEFINE_TESTSUITE(ThreadName, basics, ripple);
81 
82 } // namespace test
83 } // namespace ripple
std::string
STL class.
ripple::this_thread::set_name
void set_name(std::string s)
std::thread
STL class.
ripple::test::ThreadName_test::exerciseName
static void exerciseName(std::string myName, std::atomic< bool > *stop, std::atomic< int > *state)
Definition: ThreadName_test.cpp:30
std::atomic< bool >
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::ThreadName_test
Definition: ThreadName_test.cpp:26
ripple::this_thread::get_name
std::string get_name()
ripple::test::ThreadName_test::run
void run() override
Definition: ThreadName_test.cpp:53
std::thread::join
T join(T... args)
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)