Bitcoin Core  22.99.0
P2P Digital Currency
util_threadnames_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <util/string.h>
6 #include <util/threadnames.h>
7 
8 #include <mutex>
9 #include <set>
10 #include <string>
11 #include <thread>
12 #include <vector>
13 
14 #if defined(HAVE_CONFIG_H)
15 #include <config/bitcoin-config.h>
16 #endif
17 
18 #include <boost/test/unit_test.hpp>
19 
20 BOOST_AUTO_TEST_SUITE(util_threadnames_tests)
21 
22 const std::string TEST_THREAD_NAME_BASE = "test_thread.";
23 
29 std::set<std::string> RenameEnMasse(int num_threads)
30 {
31  std::vector<std::thread> threads;
32  std::set<std::string> names;
33  std::mutex lock;
34 
35  auto RenameThisThread = [&](int i) {
37  std::lock_guard<std::mutex> guard(lock);
38  names.insert(util::ThreadGetInternalName());
39  };
40 
41  for (int i = 0; i < num_threads; ++i) {
42  threads.push_back(std::thread(RenameThisThread, i));
43  }
44 
45  for (std::thread& thread : threads) thread.join();
46 
47  return names;
48 }
49 
54 BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)
55 {
56 #if !defined(HAVE_THREAD_LOCAL)
57  // This test doesn't apply to platforms where we don't have thread_local.
58  return;
59 #endif
60 
61  std::set<std::string> names = RenameEnMasse(100);
62 
63  BOOST_CHECK_EQUAL(names.size(), 100U);
64 
65  // Names "test_thread.[n]" should exist for n = [0, 99]
66  for (int i = 0; i < 100; ++i) {
67  BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + ToString(i)) != names.end());
68  }
69 
70 }
71 
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
string.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)
Rename a bunch of threads with the same basename (expect_multiple=true), ensuring suffixes are applie...
Definition: util_threadnames_tests.cpp:54
bitcoin-config.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
util::ThreadGetInternalName
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:53
RenameEnMasse
std::set< std::string > RenameEnMasse(int num_threads)
Run a bunch of threads to all call util::ThreadRename.
Definition: util_threadnames_tests.cpp:29
util::ThreadRename
void ThreadRename(std::string &&)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
Definition: threadnames.cpp:57
TEST_THREAD_NAME_BASE
const std::string TEST_THREAD_NAME_BASE
Definition: util_threadnames_tests.cpp:22
threadnames.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18