File: debugging_test.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (66 lines) | stat: -rw-r--r-- 2,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cpp-utils/thread/debugging.h>
#include <cpp-utils/assert/assert.h>
#include <cpp-utils/lock/ConditionBarrier.h>
#include <gtest/gtest.h>

using namespace cpputils;
using std::string;

TEST(ThreadDebuggingTest_ThreadName, givenMainThread_whenSettingAndGetting_thenDoesntCrash) {
	set_thread_name("my_thread_name");
	get_thread_name();
}

TEST(ThreadDebuggingTest_ThreadName, givenMainThread_whenGettingFromInside_thenIsCorrect) {
    set_thread_name("my_thread_name");
    const string name = get_thread_name();
    EXPECT_EQ("my_thread_name", name);
}

TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenGettingFromInside_thenIsCorrect) {
    std::thread child([] {
        set_thread_name("my_thread_name");
        const string name = get_thread_name();
        EXPECT_EQ("my_thread_name", name);
    });
    child.join();
}


#if defined(__GLIBC__) || defined(__APPLE__) || defined(_MSC_VER)
// disabled on musl because getting the thread name for a child thread doesn't work there
TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenSettingAndGetting_thenDoesntCrash) {
    ConditionBarrier nameIsChecked;

	bool child_didnt_crash = false;
	std::thread child([&] {
		set_thread_name("my_thread_name");
		get_thread_name();
		child_didnt_crash = true;
		nameIsChecked.wait();
	});
	get_thread_name(&child);
	nameIsChecked.release(); // getting the name of a not-running thread would cause errors, so let's make sure we only exit after getting the name
	child.join();
	EXPECT_TRUE(child_didnt_crash);
}

TEST(ThreadDebuggingTest_ThreadName, givenChildThread_whenGettingFromOutside_thenIsCorrect) {
    ConditionBarrier nameIsSet;
    ConditionBarrier nameIsChecked;

    std::thread child([&] {
        set_thread_name("my_thread_name");
        nameIsSet.release();
        nameIsChecked.wait();
    });

    nameIsSet.wait();
    set_thread_name("outer_thread_name"); // just to make sure the next line doesn't read the outer thread name
    const string name = get_thread_name(&child);
    EXPECT_EQ("my_thread_name", name);

    nameIsChecked.release();
    child.join();
}
#endif