forked from libcpr/cpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool_tests.cpp
106 lines (80 loc) · 2.45 KB
/
threadpool_tests.cpp
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
#include <atomic>
#include <cstddef>
#include <gtest/gtest.h>
#include "cpr/threadpool.h"
TEST(ThreadPoolTests, DISABLED_BasicWorkOneThread) {
std::atomic_uint32_t invCount{0};
uint32_t invCountExpected{100};
{
cpr::ThreadPool tp;
tp.SetMinThreadNum(1);
tp.SetMaxThreadNum(1);
tp.Start(0);
for (size_t i = 0; i < invCountExpected; ++i) {
tp.Submit([&invCount]() -> void { invCount++; });
}
// Wait for the thread pool to finish its work
tp.Wait();
}
EXPECT_EQ(invCount, invCountExpected);
}
TEST(ThreadPoolTests, DISABLED_BasicWorkMultipleThreads) {
std::atomic_uint32_t invCount{0};
uint32_t invCountExpected{100};
{
cpr::ThreadPool tp;
tp.SetMinThreadNum(1);
tp.SetMaxThreadNum(10);
tp.Start(0);
for (size_t i = 0; i < invCountExpected; ++i) {
tp.Submit([&invCount]() -> void { invCount++; });
}
// Wait for the thread pool to finish its work
tp.Wait();
}
EXPECT_EQ(invCount, invCountExpected);
}
TEST(ThreadPoolTests, DISABLED_PauseResumeSingleThread) {
std::atomic_uint32_t invCount{0};
uint32_t repCount{100};
uint32_t invBunchSize{20};
cpr::ThreadPool tp;
tp.SetMinThreadNum(1);
tp.SetMaxThreadNum(10);
tp.Start(0);
for (size_t i = 0; i < repCount; ++i) {
tp.Pause();
EXPECT_EQ(invCount, i * invBunchSize);
for (size_t e = 0; e < invBunchSize; ++e) {
tp.Submit([&invCount]() -> void { invCount++; });
}
tp.Resume();
// Wait for the thread pool to finish its work
tp.Wait();
EXPECT_EQ(invCount, (i + 1) * invBunchSize);
}
}
TEST(ThreadPoolTests, DISABLED_PauseResumeMultipleThreads) {
std::atomic_uint32_t invCount{0};
uint32_t repCount{100};
uint32_t invBunchSize{20};
cpr::ThreadPool tp;
tp.SetMinThreadNum(1);
tp.SetMaxThreadNum(10);
tp.Start(0);
for (size_t i = 0; i < repCount; ++i) {
tp.Pause();
EXPECT_EQ(invCount, i * invBunchSize);
for (size_t e = 0; e < invBunchSize; ++e) {
tp.Submit([&invCount]() -> void { invCount++; });
}
tp.Resume();
// Wait for the thread pool to finish its work
tp.Wait();
EXPECT_EQ(invCount, (i + 1) * invBunchSize);
}
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}