Changeset View
Changeset View
Standalone View
Standalone View
src/test/util_threadnames_tests.cpp
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/string.h> | |||||
#include <util/threadnames.h> | #include <util/threadnames.h> | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
Show All 14 Lines | |||||
* @return the set of name each thread has after attempted renaming. | * @return the set of name each thread has after attempted renaming. | ||||
*/ | */ | ||||
std::set<std::string> RenameEnMasse(int num_threads) { | std::set<std::string> RenameEnMasse(int num_threads) { | ||||
std::vector<std::thread> threads; | std::vector<std::thread> threads; | ||||
std::set<std::string> names; | std::set<std::string> names; | ||||
std::mutex lock; | std::mutex lock; | ||||
auto RenameThisThread = [&](int i) { | auto RenameThisThread = [&](int i) { | ||||
util::ThreadRename(TEST_THREAD_NAME_BASE + std::to_string(i)); | util::ThreadRename(TEST_THREAD_NAME_BASE + ToString(i)); | ||||
std::lock_guard<std::mutex> guard(lock); | std::lock_guard<std::mutex> guard(lock); | ||||
names.insert(util::ThreadGetInternalName()); | names.insert(util::ThreadGetInternalName()); | ||||
}; | }; | ||||
for (int i = 0; i < num_threads; ++i) { | for (int i = 0; i < num_threads; ++i) { | ||||
threads.push_back(std::thread(RenameThisThread, i)); | threads.push_back(std::thread(RenameThisThread, i)); | ||||
} | } | ||||
Show All 12 Lines | BOOST_AUTO_TEST_CASE(util_threadnames_test_rename_threaded) { | ||||
BOOST_CHECK_EQUAL(util::ThreadGetInternalName(), ""); | BOOST_CHECK_EQUAL(util::ThreadGetInternalName(), ""); | ||||
std::set<std::string> names = RenameEnMasse(100); | std::set<std::string> names = RenameEnMasse(100); | ||||
BOOST_CHECK_EQUAL(names.size(), 100); | BOOST_CHECK_EQUAL(names.size(), 100); | ||||
// Names "test_thread.[n]" should exist for n = [0, 99] | // Names "test_thread.[n]" should exist for n = [0, 99] | ||||
for (int i = 0; i < 100; ++i) { | for (int i = 0; i < 100; ++i) { | ||||
BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + std::to_string(i)) != | BOOST_CHECK(names.find(TEST_THREAD_NAME_BASE + ToString(i)) != | ||||
names.end()); | names.end()); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |