|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +/*============================================================ |
| 5 | +** |
| 6 | +** Source: test1.cpp |
| 7 | +** |
| 8 | +** Purpose: Test for SetThreadDescription. Create a thread, call |
| 9 | +** SetThreadDescription, and then verify that the name of the thread |
| 10 | +** matches what was set. |
| 11 | +** |
| 12 | +**=========================================================*/ |
| 13 | + |
| 14 | +#include <palsuite.h> |
| 15 | +#include "pthread_helpers.hpp" |
| 16 | + |
| 17 | +char * threadName; |
| 18 | +char * expectedThreadName; |
| 19 | +char * actualThreadName; |
| 20 | + |
| 21 | +DWORD PALAPI SetThreadDescriptionTestThread(LPVOID lpParameter) |
| 22 | +{ |
| 23 | + HANDLE palThread = GetCurrentThread(); |
| 24 | + WCHAR wideThreadName[256]; |
| 25 | + |
| 26 | + MultiByteToWideChar(CP_ACP, 0, threadName, strlen(threadName)+1, wideThreadName, 256); |
| 27 | + SetThreadDescription(palThread, wideThreadName); |
| 28 | + actualThreadName = GetThreadName(); |
| 29 | + |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +BOOL SetThreadDescriptionTest(char* name, char* expected) |
| 34 | +{ |
| 35 | + BOOL bResult = FALSE; |
| 36 | + LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL; |
| 37 | + DWORD dwStackSize = 0; |
| 38 | + LPTHREAD_START_ROUTINE lpStartAddress = &SetThreadDescriptionTestThread; |
| 39 | + LPVOID lpParameter = (LPVOID)SetThreadDescriptionTestThread; |
| 40 | + DWORD dwCreationFlags = 0; |
| 41 | + DWORD dwThreadId = 0; |
| 42 | + |
| 43 | + threadName = name; |
| 44 | + expectedThreadName = expected; |
| 45 | + |
| 46 | + HANDLE hThread = CreateThread(lpThreadAttributes, |
| 47 | + dwStackSize, lpStartAddress, lpParameter, |
| 48 | + dwCreationFlags, &dwThreadId ); |
| 49 | + |
| 50 | + if (hThread != INVALID_HANDLE_VALUE) |
| 51 | + { |
| 52 | + WaitForSingleObject(hThread, INFINITE); |
| 53 | + bResult = strcmp(actualThreadName, expectedThreadName) == 0; |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + Trace("Unable to create SetThreadDescription test thread"); |
| 58 | + } |
| 59 | + |
| 60 | + return bResult; |
| 61 | +} |
| 62 | + |
| 63 | +BOOL SetThreadDescriptionTests() |
| 64 | +{ |
| 65 | + if (!SetThreadDescriptionTest("Hello, World", "Hello, World")) |
| 66 | + { |
| 67 | + Trace("Setting thread name failed"); |
| 68 | + return FAIL; |
| 69 | + } |
| 70 | + |
| 71 | + // verify that thread name truncations works correctly on linux on macOS. |
| 72 | + char * threadName = "aaaaaaa_15chars_aaaaaaa_31chars_aaaaaaaaaaaaaaaaaaaaaaa_63chars_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 73 | + char * expected; |
| 74 | + #if defined(__APPLE__) |
| 75 | + expected = "aaaaaaa_15chars_aaaaaaa_31chars_aaaaaaaaaaaaaaaaaaaaaaa_63chars"; |
| 76 | + #else |
| 77 | + expected = "aaaaaaa_15chars"; |
| 78 | + #endif |
| 79 | + |
| 80 | + if (!SetThreadDescriptionTest(threadName, expected)) |
| 81 | + { |
| 82 | + return PASS; |
| 83 | + } |
| 84 | + |
| 85 | + return FAIL; |
| 86 | +} |
| 87 | + |
| 88 | +PALTEST(threading_SetThreadDescription_test1_paltest_setthreaddescription_test1, "threading/SetThreadDescription/test1/paltest_setthreaddescription_test1") |
| 89 | +{ |
| 90 | + if (0 != (PAL_Initialize(argc, argv))) |
| 91 | + { |
| 92 | + return FAIL; |
| 93 | + } |
| 94 | + |
| 95 | + BOOL result = SetThreadDescriptionTests(); |
| 96 | + if(actualThreadName) free(actualThreadName); |
| 97 | + if (!result) |
| 98 | + { |
| 99 | + Fail("Test Failed"); |
| 100 | + } |
| 101 | + |
| 102 | + PAL_Terminate(); |
| 103 | + return PASS; |
| 104 | +} |
| 105 | + |
0 commit comments