rippled
beast_CurrentThreadName_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/beast/core/CurrentThreadName.h>
21 #include <ripple/beast/unit_test.h>
22 #include <atomic>
23 #include <thread>
24 
25 namespace ripple {
26 namespace test {
27 
28 class CurrentThreadName_test : public beast::unit_test::suite
29 {
30 private:
31  static void
33  std::string myName,
34  std::atomic<bool>* stop,
35  std::atomic<int>* state)
36  {
37  // Verify that upon creation a thread has no name.
38  auto const initialThreadName = beast::getCurrentThreadName();
39 
40  // Set the new name.
42 
43  // Indicate to caller that the name is set.
44  *state = 1;
45 
46  // If there is an initial thread name then we failed.
47  if (!initialThreadName.empty())
48  return;
49 
50  // Wait until all threads have their names.
51  while (!*stop)
52  ;
53 
54  // Make sure the thread name that we set before is still there
55  // (not overwritten by, for instance, another thread).
56  if (beast::getCurrentThreadName() == myName)
57  *state = 2;
58  }
59 
60 public:
61  void
62  run() override
63  {
64  // Make two different threads with two different names. Make sure
65  // that the expected thread names are still there when the thread
66  // exits.
67  std::atomic<bool> stop{false};
68 
69  std::atomic<int> stateA{0};
70  std::thread tA(exerciseName, "tA", &stop, &stateA);
71 
72  std::atomic<int> stateB{0};
73  std::thread tB(exerciseName, "tB", &stop, &stateB);
74 
75  // Wait until both threads have set their names.
76  while (stateA == 0 || stateB == 0)
77  ;
78 
79  stop = true;
80  tA.join();
81  tB.join();
82 
83  // Both threads should still have the expected name when they exit.
84  BEAST_EXPECT(stateA == 2);
85  BEAST_EXPECT(stateB == 2);
86  }
87 };
88 
89 BEAST_DEFINE_TESTSUITE(CurrentThreadName, core, beast);
90 
91 } // namespace test
92 } // namespace ripple
std::string
STL class.
ripple::test::CurrentThreadName_test::run
void run() override
Definition: beast_CurrentThreadName_test.cpp:62
ripple::test::CurrentThreadName_test::exerciseName
static void exerciseName(std::string myName, std::atomic< bool > *stop, std::atomic< int > *state)
Definition: beast_CurrentThreadName_test.cpp:32
thread
beast::getCurrentThreadName
std::string getCurrentThreadName()
Returns the name of the caller thread.
Definition: CurrentThreadName.cpp:113
atomic
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:119
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::thread::join
T join(T... args)
ripple::test::CurrentThreadName_test
Definition: beast_CurrentThreadName_test.cpp:28
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)
beast
Definition: base_uint.h:641