LCOV - code coverage report
Current view: top level - src/test - util_threadnames_tests.cpp (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 24 24 100.0 %
Date: 2026-06-25 07:23:51 Functions: 8 8 100.0 %

          Line data    Source code
       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         146 : BOOST_AUTO_TEST_SUITE(util_threadnames_tests)
      17             : 
      18             : const std::string TEST_THREAD_NAME_BASE = "test_thread.";
      19             : 
      20             : /**
      21             :  * Run a bunch of threads to all call util::ThreadRename.
      22             :  *
      23             :  * @return the set of name each thread has after attempted renaming.
      24             :  */
      25           1 : std::set<std::string> RenameEnMasse(int num_threads)
      26             : {
      27           1 :     std::vector<std::thread> threads;
      28           1 :     std::set<std::string> names;
      29           1 :     std::mutex lock;
      30             : 
      31         101 :     auto RenameThisThread = [&](int i) {
      32         100 :         util::ThreadRename(TEST_THREAD_NAME_BASE + ToString(i));
      33         100 :         std::lock_guard<std::mutex> guard(lock);
      34         100 :         names.insert(util::ThreadGetInternalName());
      35         100 :     };
      36             : 
      37         101 :     for (int i = 0; i < num_threads; ++i) {
      38         100 :         threads.emplace_back(RenameThisThread, i);
      39         100 :     }
      40             : 
      41         101 :     for (std::thread& thread : threads) thread.join();
      42             : 
      43           1 :     return names;
      44           1 : }
      45             : 
      46             : /**
      47             :  * Rename a bunch of threads with the same basename (expect_multiple=true), ensuring suffixes are
      48             :  * applied properly.
      49             :  */
      50         148 : BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)
      51             : {
      52           1 :     std::set<std::string> names = RenameEnMasse(100);
      53             : 
      54           1 :     BOOST_CHECK_EQUAL(names.size(), 100U);
      55             : 
      56             :     // Names "test_thread.[n]" should exist for n = [0, 99]
      57         101 :     for (int i = 0; i < 100; ++i) {
      58         100 :         BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + ToString(i)) != names.end());
      59         100 :     }
      60             : 
      61           1 : }
      62             : 
      63         146 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.16