LCOV - code coverage report
Current view: top level - src/test - util_threadnames_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 3 25 12.0 %
Date: 2022-04-21 14:51:19 Functions: 1 3 33.3 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 6 0.0 %

           Branch data     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                 :            : #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                 :            : 
#      24                 :            : /**
#      25                 :            :  * Run a bunch of threads to all call util::ThreadRename.
#      26                 :            :  *
#      27                 :            :  * @return the set of name each thread has after attempted renaming.
#      28                 :            :  */
#      29                 :            : std::set<std::string> RenameEnMasse(int num_threads)
#      30                 :          0 : {
#      31                 :          0 :     std::vector<std::thread> threads;
#      32                 :          0 :     std::set<std::string> names;
#      33                 :          0 :     std::mutex lock;
#      34                 :            : 
#      35                 :          0 :     auto RenameThisThread = [&](int i) {
#      36                 :          0 :         util::ThreadRename(TEST_THREAD_NAME_BASE + ToString(i));
#      37                 :          0 :         std::lock_guard<std::mutex> guard(lock);
#      38                 :          0 :         names.insert(util::ThreadGetInternalName());
#      39                 :          0 :     };
#      40                 :            : 
#      41         [ #  # ]:          0 :     for (int i = 0; i < num_threads; ++i) {
#      42                 :          0 :         threads.push_back(std::thread(RenameThisThread, i));
#      43                 :          0 :     }
#      44                 :            : 
#      45         [ #  # ]:          0 :     for (std::thread& thread : threads) thread.join();
#      46                 :            : 
#      47                 :          0 :     return names;
#      48                 :          0 : }
#      49                 :            : 
#      50                 :            : /**
#      51                 :            :  * Rename a bunch of threads with the same basename (expect_multiple=true), ensuring suffixes are
#      52                 :            :  * applied properly.
#      53                 :            :  */
#      54                 :            : BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded)
#      55                 :          1 : {
#      56                 :          1 : #if !defined(HAVE_THREAD_LOCAL)
#      57                 :            :     // This test doesn't apply to platforms where we don't have thread_local.
#      58                 :          1 :     return;
#      59                 :          0 : #endif
#      60                 :            : 
#      61                 :          0 :     std::set<std::string> names = RenameEnMasse(100);
#      62                 :            : 
#      63                 :          0 :     BOOST_CHECK_EQUAL(names.size(), 100U);
#      64                 :            : 
#      65                 :            :     // Names "test_thread.[n]" should exist for n = [0, 99]
#      66         [ #  # ]:          0 :     for (int i = 0; i < 100; ++i) {
#      67                 :          0 :         BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + ToString(i)) != names.end());
#      68                 :          0 :     }
#      69                 :            : 
#      70                 :          0 : }
#      71                 :            : 
#      72                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a