-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathThreadTest.cpp
120 lines (101 loc) · 3.5 KB
/
ThreadTest.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Copyright (C) 2019-2023 The ViaDuck Project
*
* This file is part of Commons.
*
* Commons is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Commons is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Commons. If not, see <http://www.gnu.org/licenses/>.
*/
#include <commons/thread/impl/LockFreeQueue.h>
#include <commons/thread/impl/LockingQueue.h>
#include <commons/thread/IQueueWorker.h>
#include "ThreadTest.h"
#define TEST_ITER 100
struct TestMessage {
int testVal;
};
void testBasic(IQueue<TestMessage> &queue) {
for (int i = 0; i < TEST_ITER; i++)
queue.push(TestMessage{i});
TestMessage value{};
for (int i = 0; i < TEST_ITER; i++) {
ASSERT_TRUE(queue.pop(value)) << i;
EXPECT_EQ(i, value.testVal) << i;
}
ASSERT_FALSE(queue.pop(value));
}
void testAdvanced(IQueue<TestMessage> &queue) {
std::thread t([&queue] () {
TestMessage value{};
for (int i = 0; i < TEST_ITER; i++) {
ASSERT_TRUE(queue.pop_wait(value)) << i;
EXPECT_EQ(i, value.testVal) << i;
}
// infinite wait
ASSERT_FALSE(queue.pop_wait(value));
});
for (int i = 0; i < TEST_ITER; i++)
queue.push(TestMessage{i});
// let it wait, abort
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
queue.abort();
// if wait does not abort, force gtest into timeout
t.join();
}
void testBasicWorker(IQueueWorker<TestMessage> &worker) {
worker.startThread();
for (int i = 0; i < TEST_ITER; i++)
worker.enqueue(TestMessage{i});
worker.stopThread();
}
void testAdvancedWorker(IQueueWorker<TestMessage> &worker) {
worker.startThread();
for (int i = 0; i < TEST_ITER / 2; i++)
worker.enqueue(TestMessage{i});
// let it wait
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
for (int i = TEST_ITER / 2; i < TEST_ITER / 2; i++)
worker.enqueue(TestMessage{i});
std::this_thread::sleep_for(100ms);
worker.stopThread();
}
#define MAKE_QUEUE_TEST(test, impl) \
TEST_F(ThreadTest, test##impl) { \
impl##Queue<TestMessage> queue; \
test(queue); \
}
#define MAKE_IMPL_WORKER(impl) \
class impl##TestWorker : public IQueueWorker<TestMessage> { \
public: \
impl##TestWorker() : IQueueWorker(new impl##Queue<TestMessage>()) { } \
protected: \
void doWork(const TestMessage &value) override { \
ASSERT_EQ(mCounter++, value.testVal); \
} \
int mCounter = 0; \
};
#define MAKE_WORKER_TEST(test, impl) \
TEST_F(ThreadTest, test##impl) { \
impl##TestWorker worker; \
test(worker); \
}
#define MAKE_IMPL_TESTS(impl) \
MAKE_IMPL_WORKER(impl) \
MAKE_QUEUE_TEST(testBasic, impl) \
MAKE_QUEUE_TEST(testAdvanced, impl) \
MAKE_WORKER_TEST(testBasicWorker, impl) \
MAKE_WORKER_TEST(testAdvancedWorker, impl)
MAKE_IMPL_TESTS(Locking);
MAKE_IMPL_TESTS(LockFree);