|
| 1 | +// Copyright 2026 Bloomberg Finance L.P. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// bmqsys_threadutil.t.cpp -*-C++-*- |
| 17 | +#include <bmqsys_threadutil.h> |
| 18 | + |
| 19 | +// BDE |
| 20 | +#include <bdlf_bind.h> |
| 21 | +#include <bslmt_threadutil.h> |
| 22 | + |
| 23 | +// TEST DRIVER |
| 24 | +#include <bmqtst_testhelper.h> |
| 25 | + |
| 26 | +// CONVENIENCE |
| 27 | +using namespace BloombergLP; |
| 28 | +using namespace bsl; |
| 29 | + |
| 30 | +// ============================================================================ |
| 31 | +// TESTS |
| 32 | +// ---------------------------------------------------------------------------- |
| 33 | + |
| 34 | +static void test1_breathingTest() |
| 35 | +// ------------------------------------------------------------------------ |
| 36 | +// BREATHING TEST |
| 37 | +// |
| 38 | +// Concerns: |
| 39 | +// Exercise the basic functionality of the component. |
| 40 | +// |
| 41 | +// Plan: |
| 42 | +// Set the thread name "once". |
| 43 | +// Validate the thread name does not update. |
| 44 | +// |
| 45 | +// Testing: |
| 46 | +// ------------------------------------------------------------------------ |
| 47 | +{ |
| 48 | + bmqtst::TestHelper::printTestName("BREATHING TEST"); |
| 49 | + |
| 50 | + // Skip under MemorySanitizer: bslmt::ThreadUtil::getThreadName uses |
| 51 | + // pthread_getname_np which leaves the buffer uninitialized from MSan's |
| 52 | + // perspective. |
| 53 | +#if defined(__has_feature) // Clang-supported method for checking sanitizers. |
| 54 | + const bool skipTest = __has_feature(memory_sanitizer); |
| 55 | +#elif defined(__SANITIZE_MEMORY__) |
| 56 | + // GCC-supported macros for checking MSAN. |
| 57 | + const bool skipTest = true; |
| 58 | +#else |
| 59 | + const bool skipTest = false; |
| 60 | +#endif |
| 61 | + |
| 62 | + if (skipTest) { |
| 63 | + bsl::cout << "Test skipped (running under sanitizer)" << bsl::endl; |
| 64 | + return; // RETURN |
| 65 | + } |
| 66 | + |
| 67 | + bsl::string threadName; |
| 68 | + |
| 69 | + bmqsys::ThreadUtil::setCurrentThreadNameOnce("TestThread1"); |
| 70 | + bslmt::ThreadUtil::getThreadName(&threadName); |
| 71 | + BMQTST_ASSERT_EQ(threadName, "TestThread1"); |
| 72 | + |
| 73 | + bmqsys::ThreadUtil::setCurrentThreadNameOnce("TestThread2"); |
| 74 | + bslmt::ThreadUtil::getThreadName(&threadName); |
| 75 | + BMQTST_ASSERT_EQ(threadName, "TestThread1"); |
| 76 | +} |
| 77 | + |
| 78 | +// ============================================================================ |
| 79 | +// MAIN PROGRAM |
| 80 | +// ---------------------------------------------------------------------------- |
| 81 | + |
| 82 | +int main(int argc, char* argv[]) |
| 83 | +{ |
| 84 | + TEST_PROLOG(bmqtst::TestHelper::e_DEFAULT); |
| 85 | + |
| 86 | + switch (_testCase) { |
| 87 | + case 0: |
| 88 | + case 1: test1_breathingTest(); break; |
| 89 | + default: { |
| 90 | + cerr << "WARNING: CASE '" << _testCase << "' NOT FOUND." << endl; |
| 91 | + bmqtst::TestHelperUtil::testStatus() = -1; |
| 92 | + } break; |
| 93 | + } |
| 94 | + |
| 95 | + TEST_EPILOG(bmqtst::TestHelper::e_CHECK_DEF_GBL_ALLOC); |
| 96 | +} |
0 commit comments