-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththread_unittest.cc
More file actions
676 lines (529 loc) · 17 KB
/
thread_unittest.cc
File metadata and controls
676 lines (529 loc) · 17 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#include "thread.hpp"
#include <gtest/gtest.h>
#include <atomic>
#include <future>
using namespace std::chrono_literals;
class ThreadTest : public ::testing::Test
{
protected:
void SetUp() override
{
// 每个测试前的设置
}
void TearDown() override
{
// 每个测试后的清理
}
};
// 测试默认构造
TEST_F(ThreadTest, DefaultConstruction)
{
Thread thread;
EXPECT_FALSE(thread.isRunning());
EXPECT_TRUE(thread.isIdle());
EXPECT_FALSE(thread.isJoinable());
EXPECT_EQ(thread.getState(), Thread::State::Idle);
}
// 测试带任务构造
TEST_F(ThreadTest, ConstructionWithTask)
{
bool taskExecuted = false;
Thread thread([&taskExecuted](std::stop_token) { taskExecuted = true; });
EXPECT_FALSE(thread.isRunning());
EXPECT_TRUE(thread.isIdle());
EXPECT_FALSE(taskExecuted); // 任务不应该立即执行
}
// 测试启动和停止
TEST_F(ThreadTest, StartAndStop)
{
std::atomic<bool> taskExecuted{false};
std::atomic<int> loopCount{0};
Thread thread([&taskExecuted, &loopCount](std::stop_token token) {
while (!token.stop_requested()) {
++loopCount;
std::this_thread::sleep_for(5ms);
}
taskExecuted = true;
});
// 启动线程
EXPECT_TRUE(thread.start());
EXPECT_TRUE(thread.isRunning());
EXPECT_TRUE(thread.isJoinable());
// 等待线程运行一段时间
std::this_thread::sleep_for(50ms);
EXPECT_GT(loopCount.load(), 0);
// 停止线程
thread.stop();
EXPECT_FALSE(thread.isRunning());
EXPECT_TRUE(taskExecuted);
}
// 测试重复启动
TEST_F(ThreadTest, DoubleStart)
{
std::atomic<int> executionCount{0};
Thread thread([&executionCount](std::stop_token) {
++executionCount;
std::this_thread::sleep_for(100ms);
});
EXPECT_TRUE(thread.start());
EXPECT_FALSE(thread.start()); // 第二次启动应该失败
// 确保只执行了一次
thread.waitForFinished();
EXPECT_EQ(executionCount.load(), 1);
}
// 测试无任务启动
TEST_F(ThreadTest, StartWithoutTask)
{
Thread thread;
EXPECT_FALSE(thread.start()); // 没有任务应该启动失败
// 设置任务后应该能启动
thread.setTask([](std::stop_token) {});
EXPECT_TRUE(thread.start());
thread.stop();
}
// 测试运行时设置任务
TEST_F(ThreadTest, SetTaskWhileRunning)
{
Thread thread([](std::stop_token) { std::this_thread::sleep_for(100ms); });
EXPECT_TRUE(thread.start());
// 尝试在运行时设置任务应该抛出异常
EXPECT_THROW(thread.setTask([](std::stop_token) {}), std::runtime_error);
thread.stop();
}
// 测试在停止状态设置任务
TEST_F(ThreadTest, SetTaskAfterStopped)
{
Thread thread([](std::stop_token) {});
// 启动并等待完成
thread.start();
thread.waitForFinished();
// 停止后应该可以重新设置任务
EXPECT_NO_THROW(thread.setTask([](std::stop_token) {}));
}
// 测试等待完成
TEST_F(ThreadTest, WaitForFinished)
{
std::atomic<bool> taskCompleted{false};
Thread thread([&taskCompleted](std::stop_token) {
std::this_thread::sleep_for(100ms);
taskCompleted = true;
});
auto start = std::chrono::steady_clock::now();
thread.start();
thread.waitForFinished();
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
EXPECT_GE(duration.count(), 100);
EXPECT_TRUE(taskCompleted);
}
// 测试带超时的等待完成
TEST_F(ThreadTest, WaitForFinishedWithTimeout)
{
Thread thread([](std::stop_token) { std::this_thread::sleep_for(500ms); });
thread.start();
// 短超时应该返回false
auto start = std::chrono::steady_clock::now();
EXPECT_FALSE(thread.waitForFinished(50ms));
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
EXPECT_LE(duration.count(), 100); // 应该在超时后很快返回
// 长超时应该返回true
EXPECT_TRUE(thread.waitForFinished(600ms));
}
// 测试零超时等待
TEST_F(ThreadTest, WaitForFinishedWithZeroTimeout)
{
Thread thread([](std::stop_token) { std::this_thread::sleep_for(100ms); });
thread.start();
// 零超时应该立即返回false
EXPECT_FALSE(thread.waitForFinished(0ms));
thread.waitForFinished(); // 正常等待完成
}
// 测试请求停止
TEST_F(ThreadTest, RequestStop)
{
std::atomic<bool> stopRequested{false};
std::atomic<int> iterations{0};
Thread thread([&stopRequested, &iterations](std::stop_token token) {
while (!token.stop_requested()) {
++iterations;
std::this_thread::sleep_for(5ms);
}
stopRequested = true;
});
thread.start();
// 确保线程运行了一些迭代
std::this_thread::sleep_for(50ms);
EXPECT_GT(iterations.load(), 0);
thread.requestStop();
bool finished = thread.waitForFinished(100ms);
EXPECT_TRUE(stopRequested);
EXPECT_TRUE(finished);
}
// 测试线程ID
TEST_F(ThreadTest, ThreadId)
{
std::thread::id threadId;
std::promise<void> started;
Thread thread([&threadId, &started](std::stop_token) {
threadId = std::this_thread::get_id();
started.set_value();
std::this_thread::sleep_for(50ms);
});
EXPECT_EQ(thread.getThreadId(), std::thread::id()); // 启动前应该为空
thread.start();
started.get_future().wait(); // 等待线程真正启动
EXPECT_NE(threadId, std::thread::id());
EXPECT_EQ(threadId, thread.getThreadId());
thread.waitForFinished();
}
// 测试停止令牌
TEST_F(ThreadTest, StopToken)
{
std::atomic<bool> hasStopToken{false};
std::atomic<bool> canStop{false};
Thread thread([&hasStopToken, &canStop](std::stop_token token) {
hasStopToken = token.stop_possible();
while (!token.stop_requested()) {
std::this_thread::sleep_for(5ms);
}
canStop = true;
});
thread.start();
// 等待线程设置hasStopToken
std::this_thread::sleep_for(50ms);
EXPECT_TRUE(hasStopToken);
// 测试停止令牌功能
thread.requestStop();
thread.waitForFinished(100ms);
EXPECT_TRUE(canStop);
}
// 测试异常处理
TEST_F(ThreadTest, ExceptionHandling)
{
std::atomic<bool> exceptionThrown{false};
// 这个测试主要确保异常不会导致崩溃
Thread thread([&exceptionThrown](std::stop_token) {
try {
throw std::runtime_error("Test exception");
} catch (...) {
exceptionThrown = true;
throw; // 重新抛出,测试线程类的异常处理
}
});
EXPECT_TRUE(thread.start());
EXPECT_NO_THROW(thread.waitForFinished()); // 不应该抛出异常
EXPECT_FALSE(thread.isRunning());
EXPECT_TRUE(exceptionThrown);
}
// 测试多个异常
TEST_F(ThreadTest, MultipleExceptions)
{
// 测试多次启动停止,确保异常处理稳定
for (int i = 0; i < 3; ++i) {
Thread thread([](std::stop_token) { throw std::logic_error("Iteration exception"); });
EXPECT_TRUE(thread.start());
EXPECT_NO_THROW(thread.waitForFinished());
EXPECT_TRUE(thread.isStopped());
}
}
// 测试状态转换
TEST_F(ThreadTest, StateTransitions)
{
std::promise<void> startPromise;
std::future<void> startFuture = startPromise.get_future();
std::promise<void> endPromise;
std::future<void> endFuture = endPromise.get_future();
Thread thread([&startPromise, &endFuture](std::stop_token token) {
startPromise.set_value(); // 通知测试线程已启动
endFuture.get(); // 等待测试允许结束
});
// 初始状态
EXPECT_EQ(thread.getState(), Thread::State::Idle);
// 启动线程
thread.start();
EXPECT_EQ(thread.getState(), Thread::State::Starting);
// 等待线程内部设置promise
startFuture.get();
// 线程应该处于运行状态
EXPECT_EQ(thread.getState(), Thread::State::Running);
// 允许线程结束
endPromise.set_value();
thread.waitForFinished();
// 线程应该处于停止状态
EXPECT_EQ(thread.getState(), Thread::State::Stopped);
}
// 测试快速状态转换
TEST_F(ThreadTest, RapidStateTransitions)
{
Thread thread([](std::stop_token) {
// 快速完成的任务
});
// 快速连续调用状态检查
EXPECT_TRUE(thread.isIdle());
EXPECT_FALSE(thread.isRunning());
EXPECT_FALSE(thread.isStopped());
thread.start();
// 可能的状态:Starting 或 Running
EXPECT_TRUE(thread.isRunning() || thread.getState() == Thread::State::Starting);
thread.waitForFinished();
EXPECT_TRUE(thread.isStopped());
EXPECT_FALSE(thread.isRunning());
EXPECT_FALSE(thread.isIdle());
}
// 测试静态睡眠方法
TEST_F(ThreadTest, StaticSleepMethods)
{
auto start = std::chrono::steady_clock::now();
Thread::sleep(0); // 边界测试:零秒睡眠
Thread::msleep(10); // 短时间睡眠
Thread::usleep(100); // 微秒级睡眠
Thread::sleepFor(5ms); // 毫秒级睡眠
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
// 总睡眠时间应该合理
EXPECT_GE(duration.count(), 15);
}
// 测试静态yield方法
TEST_F(ThreadTest, StaticYield)
{
// yield方法不应该阻塞,我们只能测试它能够正常调用
for (int i = 0; i < 10; ++i) {
Thread::yield();
}
SUCCEED(); // 如果没有崩溃就是成功
}
// 测试硬件并发
TEST_F(ThreadTest, HardwareConcurrency)
{
unsigned int cores = Thread::hardwareConcurrency();
EXPECT_GT(cores, 0u);
EXPECT_LE(cores, std::thread::hardware_concurrency());
// 多次调用应该返回相同结果
EXPECT_EQ(cores, Thread::hardwareConcurrency());
}
// 测试析构时自动停止
TEST_F(ThreadTest, AutoStopOnDestruction)
{
std::atomic<bool> threadRunning{false};
std::atomic<bool> threadStopped{false};
std::promise<void> threadStarted;
{
Thread thread([&threadRunning, &threadStopped, &threadStarted](std::stop_token token) {
threadRunning = true;
threadStarted.set_value();
while (!token.stop_requested()) {
std::this_thread::sleep_for(5ms);
}
threadStopped = true;
});
thread.start();
// 等待线程启动
threadStarted.get_future().wait();
EXPECT_TRUE(threadRunning);
// thread对象离开作用域,应该自动停止
}
// 给线程一些时间来处理停止
std::this_thread::sleep_for(50ms);
EXPECT_TRUE(threadStopped);
}
// 测试带参数的停止方法
TEST_F(ThreadTest, StopWithTimeout)
{
Thread thread([](std::stop_token) { std::this_thread::sleep_for(200ms); });
thread.start();
auto start = std::chrono::steady_clock::now();
thread.stop(50ms); // 短超时,可能不会完全等待
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
EXPECT_LE(duration.count(), 100); // 应该在大约50ms左右返回
// 线程可能还在运行,需要最终清理
if (thread.isJoinable()) {
thread.waitForFinished();
}
}
// 测试任务返回值(通过future)
TEST_F(ThreadTest, TaskWithReturnValue)
{
std::atomic<int> result{0};
auto task = [&result](std::stop_token) {
std::this_thread::sleep_for(50ms);
result = 42;
};
Thread thread(task);
thread.start();
thread.waitForFinished();
EXPECT_EQ(result.load(), 42);
}
// 测试多次停止调用
TEST_F(ThreadTest, MultipleStopCalls)
{
std::atomic<int> stopCount{0};
Thread thread([&stopCount](std::stop_token token) {
while (!token.stop_requested()) {
std::this_thread::sleep_for(5ms);
}
++stopCount;
});
thread.start();
std::this_thread::sleep_for(50ms);
// 多次调用stop应该是安全的
thread.stop();
thread.stop();
thread.stop();
thread.requestStop();
thread.requestStop();
thread.waitForFinished();
// 确保任务只停止了一次
EXPECT_EQ(stopCount.load(), 1);
EXPECT_FALSE(thread.isRunning());
}
// 测试在已停止的线程上操作
TEST_F(ThreadTest, OperationsOnStoppedThread)
{
Thread thread([](std::stop_token) {
// 快速结束的任务
});
thread.start();
thread.waitForFinished();
// 在已停止的线程上操作
EXPECT_FALSE(thread.isRunning());
EXPECT_TRUE(thread.isStopped());
EXPECT_FALSE(thread.isIdle());
EXPECT_TRUE(thread.waitForFinished()); // 应该立即返回true
EXPECT_TRUE(thread.waitForFinished(10ms)); // 应该立即返回true
// 再次停止应该是安全的
thread.stop();
thread.requestStop();
// 状态应该保持不变
EXPECT_TRUE(thread.isStopped());
}
// 测试在空闲状态的操作
TEST_F(ThreadTest, OperationsOnIdleThread)
{
Thread thread;
EXPECT_TRUE(thread.isIdle());
EXPECT_FALSE(thread.isRunning());
EXPECT_FALSE(thread.isStopped());
EXPECT_TRUE(thread.waitForFinished()); // 应该立即返回true
EXPECT_TRUE(thread.waitForFinished(10ms)); // 应该立即返回true
// 停止操作在空闲状态应该是安全的
thread.stop();
thread.requestStop();
// 状态应该保持不变
EXPECT_TRUE(thread.isIdle());
}
// 性能测试:快速启动停止多个线程
TEST_F(ThreadTest, RapidStartStop)
{
const int NUM_THREADS = 10;
std::vector<std::unique_ptr<Thread>> threads;
std::atomic<int> completedCount{0};
for (int i = 0; i < NUM_THREADS; ++i) {
auto thread = std::make_unique<Thread>([&completedCount](std::stop_token token) {
std::this_thread::sleep_for(10ms);
++completedCount;
});
EXPECT_TRUE(thread->start());
threads.push_back(std::move(thread));
}
// 等待所有线程完成
for (auto &thread : threads) {
thread->waitForFinished();
EXPECT_FALSE(thread->isRunning());
}
EXPECT_EQ(completedCount.load(), NUM_THREADS);
}
// 测试长时间运行的任务
TEST_F(ThreadTest, LongRunningTask)
{
std::atomic<bool> running{false};
std::promise<void> started;
Thread thread([&running, &started](std::stop_token token) {
running = true;
started.set_value();
// 长时间运行,但会响应停止请求
int counter = 0;
while (!token.stop_requested() && counter < 1000) {
std::this_thread::sleep_for(1ms);
++counter;
}
running = false;
});
thread.start();
started.get_future().wait(); // 等待线程启动
EXPECT_TRUE(running);
// 让线程运行一段时间
std::this_thread::sleep_for(100ms);
EXPECT_TRUE(running);
// 然后停止
thread.stop();
thread.waitForFinished();
EXPECT_FALSE(running);
}
// 测试任务在停止请求前自然结束
TEST_F(ThreadTest, NaturalTaskCompletion)
{
std::atomic<bool> completed{false};
Thread thread([&completed](std::stop_token) {
std::this_thread::sleep_for(50ms);
completed = true;
});
thread.start();
// 不调用stop,等待任务自然完成
thread.waitForFinished();
EXPECT_TRUE(completed);
EXPECT_TRUE(thread.isStopped());
}
// 测试线程局部存储交互
TEST_F(ThreadTest, ThreadLocalStorage)
{
static thread_local int tls_value = 0;
std::atomic<int> thread_tls_value{0};
Thread thread([&thread_tls_value](std::stop_token) {
tls_value = 42;
thread_tls_value = tls_value;
});
tls_value = 100; // 主线程的值
thread.start();
thread.waitForFinished();
EXPECT_EQ(thread_tls_value.load(), 42);
EXPECT_EQ(tls_value, 100); // 主线程的TLS不应该被影响
}
// 测试内存使用(基本验证)
TEST_F(ThreadTest, MemoryUsage)
{
// 创建多个线程确保没有内存泄漏
const int NUM_THREADS = 5;
std::vector<std::unique_ptr<Thread>> threads;
for (int i = 0; i < NUM_THREADS; ++i) {
threads.emplace_back(std::make_unique<Thread>([](std::stop_token) {}));
}
// 启动所有线程
for (auto &thread : threads) {
EXPECT_TRUE(thread->start());
}
// 等待所有线程完成
for (auto &thread : threads) {
thread->waitForFinished();
}
// 没有崩溃就是成功
SUCCEED();
}
// 测试极端超时值
TEST_F(ThreadTest, ExtremeTimeoutValues)
{
Thread thread([](std::stop_token) { std::this_thread::sleep_for(100ms); });
thread.start();
// 测试各种边界超时值
EXPECT_FALSE(thread.waitForFinished(0ms)); // 零超时
EXPECT_FALSE(thread.waitForFinished(-1ms)); // 负超时(应该视为零)
EXPECT_TRUE(thread.waitForFinished(200ms)); // 足够长的超时
// 确保线程已停止
EXPECT_TRUE(thread.isStopped());
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}