aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/thread_ut.cpp
blob: b99a2008641ae515ff67d82673d8f28f63d4d8f7 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "thread.h"

#include <library/cpp/testing/unittest/registar.h>

#include <atomic> 
 
Y_UNIT_TEST_SUITE(TSysThreadTest) {
    struct TIdTester {
        inline TIdTester()
            : Thr(nullptr)
            , Cur(0)
            , Real(0)
        {
        }

        static inline void* DoRun(void* ptr) {
            ((TIdTester*)ptr)->Run();

            return nullptr;
        }

        inline void Run() {
            Cur = TThread::CurrentThreadId();
            Real = Thr->Id();
            Numeric = TThread::CurrentThreadNumericId(); 
        }

        TThread* Thr;
        TThread::TId Cur;
        TThread::TId Real;
        TThread::TId Numeric; 
    };

    Y_UNIT_TEST(TestThreadId) {
        TIdTester tst;
        TThread thr(tst.DoRun, &tst);

        tst.Thr = &thr;

        thr.Start();
        thr.Join();

        UNIT_ASSERT_EQUAL(tst.Cur, tst.Real);
        UNIT_ASSERT(tst.Cur != 0);
        UNIT_ASSERT(tst.Numeric != 0); 
        UNIT_ASSERT(tst.Numeric != tst.Real); 
    }

    void* ThreadProc(void*) {
        TThread::SetCurrentThreadName("CurrentThreadSetNameTest");
        return nullptr;
    }

    void* ThreadProc2(void*) {
        return nullptr;
    }

    void* ThreadProc3(void*) {
        const auto name = TThread::CurrentThreadName();
        Y_FAKE_READ(name);
        return nullptr;
    }

    void* ThreadProc4(void*) {
        const TString setName = "ThreadName";
        TThread::SetCurrentThreadName(setName.data());

        const auto getName = TThread::CurrentThreadName();
        if (TThread::CanGetCurrentThreadName()) {
            UNIT_ASSERT_VALUES_EQUAL(setName, getName);
        } else {
            UNIT_ASSERT_VALUES_EQUAL("", getName);
        }
        return nullptr;
    }

    void* ThreadProcChild(void*) {
        const auto name = TThread::CurrentThreadName();
        const auto defaultName = GetProgramName();

        (void)name;
        (void)defaultName;

#if defined(_darwin_) || defined(_linux_)
        UNIT_ASSERT_VALUES_EQUAL(name, defaultName);
#endif
        return nullptr;
    }

    void* ThreadProcParent(void*) {
        const TString setName = "Parent";
        TThread::SetCurrentThreadName(setName.data());

        TThread thread(&ThreadProcChild, nullptr);

        thread.Start();
        thread.Join();

        const auto getName = TThread::CurrentThreadName();
        if (TThread::CanGetCurrentThreadName()) {
            UNIT_ASSERT_VALUES_EQUAL(setName, getName);
        } else {
            UNIT_ASSERT_VALUES_EQUAL("", getName);
        }
        return nullptr;
    }

    Y_UNIT_TEST(TestSetThreadName) {
        TThread thread(&ThreadProc, nullptr);
        // just check it doesn't crash
        thread.Start();
        thread.Join();
    }

    Y_UNIT_TEST(TestSetThreadName2) {
        TThread thread(TThread::TParams(&ThreadProc, nullptr, 0).SetName("XXX"));

        thread.Start();
        thread.Join();
    }

    Y_UNIT_TEST(TestGetThreadName) {
        TThread thread(&ThreadProc3, nullptr);
        thread.Start();
        thread.Join();
    }

    Y_UNIT_TEST(TestSetGetThreadName) {
        TThread thread(&ThreadProc4, nullptr);
        thread.Start();
        thread.Join();
    }

    Y_UNIT_TEST(TestSetGetThreadNameInChildThread) {
        TThread thread(&ThreadProcParent, nullptr);
        thread.Start();
        thread.Join();
    }

    Y_UNIT_TEST(TestDoubleJoin) {
        TThread thread(&ThreadProc, nullptr);

        thread.Start();
        thread.Join();

        UNIT_ASSERT_EQUAL(thread.Join(), nullptr);
    }

    Y_UNIT_TEST(TestDoubleStart) {
        TThread thread(&ThreadProc, nullptr);

        thread.Start();
        UNIT_ASSERT_EXCEPTION(thread.Start(), yexception);
        thread.Join();
    }

    Y_UNIT_TEST(TestNoStart) {
        TThread thread(&ThreadProc, nullptr);
    }

    Y_UNIT_TEST(TestNoStartJoin) {
        TThread thread(&ThreadProc, nullptr);

        UNIT_ASSERT_EQUAL(thread.Join(), nullptr);
    }

    Y_UNIT_TEST(TestStackPointer) {
        TArrayHolder<char> buf(new char[64000]);
        TThread thr(TThread::TParams(ThreadProc2, nullptr).SetStackPointer(buf.Get()).SetStackSize(64000));

        thr.Start();
        UNIT_ASSERT_VALUES_EQUAL(thr.Join(), nullptr);
    }

    Y_UNIT_TEST(TestStackLimits) {
        TCurrentThreadLimits sl;

        UNIT_ASSERT(sl.StackBegin);
        UNIT_ASSERT(sl.StackLength > 0);
    }

    Y_UNIT_TEST(TestFunc) {
        std::atomic_bool flag = {false};
        TThread thread([&flag]() { flag = true; });

        thread.Start();
        UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);
        UNIT_ASSERT(flag);
    }

    Y_UNIT_TEST(TestCopyFunc) {
        std::atomic_bool flag = {false};
        auto func = [&flag]() { flag = true; };

        TThread thread(func);
        thread.Start();
        UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);

        TThread thread2(func);
        thread2.Start();
        UNIT_ASSERT_VALUES_EQUAL(thread2.Join(), nullptr);

        UNIT_ASSERT(flag);
    }

    Y_UNIT_TEST(TestCallable) {
        std::atomic_bool flag = {false};

        struct TCallable: TMoveOnly {
            std::atomic_bool* Flag_;

            TCallable(std::atomic_bool* flag)
                : Flag_(flag)
            {
            }

            void operator()() {
                *Flag_ = true;
            }
        };

        TCallable foo(&flag);
        TThread thread(std::move(foo));

        thread.Start();
        UNIT_ASSERT_VALUES_EQUAL(thread.Join(), nullptr);
        UNIT_ASSERT(flag);
    }
};