Bitcoin Core  27.99.0
P2P Digital Currency
util_threadnames_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 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 #include <boost/test/unit_test.hpp>
15 
16 BOOST_AUTO_TEST_SUITE(util_threadnames_tests)
17 
18 const std::string TEST_THREAD_NAME_BASE = "test_thread.";
19 
25 std::set<std::string> RenameEnMasse(int num_threads)
26 {
27  std::vector<std::thread> threads;
28  std::set<std::string> names;
29  std::mutex lock;
30 
31  auto RenameThisThread = [&](int i) {
33  std::lock_guard<std::mutex> guard(lock);
34  names.insert(util::ThreadGetInternalName());
35  };
36 
37  threads.reserve(num_threads);
38  for (int i = 0; i < num_threads; ++i) {
39  threads.emplace_back(RenameThisThread, i);
40  }
41 
42  for (std::thread& thread : threads) thread.join();
43 
44  return names;
45 }
46 
51 BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)
52 {
53  std::set<std::string> names = RenameEnMasse(100);
54 
55  BOOST_CHECK_EQUAL(names.size(), 100U);
56 
57  // Names "test_thread.[n]" should exist for n = [0, 99]
58  for (int i = 0; i < 100; ++i) {
59  BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + ToString(i)) != names.end());
60  }
61 
62 }
63 
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void ThreadRename(const 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
std::string ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
Definition: threadnames.cpp:47
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:110
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...
std::set< std::string > RenameEnMasse(int num_threads)
Run a bunch of threads to all call util::ThreadRename.
const std::string TEST_THREAD_NAME_BASE