Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
q_consumer_threads_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
39#include <gtest/gtest.h>
40
41#include <atomic>
42#include <chrono>
43#include <stdexcept>
44#include <thread>
45
46// QueueTheadsPool is deprecated, but supported until 8.0.0: this test is
47// its coverage
48#if defined(_MSC_VER) && !defined(__clang__)
49# pragma warning(push)
50# pragma warning(disable : 4996)
51#else
52# pragma GCC diagnostic push
53# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54#endif
55
56#include <q-consumer-threads.H>
57
58namespace
59{
60 using Pool = QueueTheadsPool<int>;
61
62 // Adds the items that its thread takes
63 struct Adder : Pool::Event
64 {
65 std::atomic<int> * sum = nullptr;
66 void run() override { *sum += item; }
67 };
68
69 void wait_for(const std::atomic<int> & value, int expected)
70 {
71 for (int k = 0; k < 1000 and value.load() != expected; ++k)
72 std::this_thread::sleep_for(std::chrono::milliseconds(10));
73 }
74}
75
76TEST(QueueTheadsPool, ItsThreadsConsumeEveryItem)
77{
78 std::atomic<int> sum{0};
79 Pool pool;
80 Adder e1, e2;
81 e1.sum = e2.sum = &sum;
82 pool.create_thread(&e1);
83 pool.create_thread(&e2);
84 for (int i = 1; i <= 100; ++i)
85 EXPECT_TRUE(pool.put(int(i)));
86 pool.resume();
87 wait_for(sum, 5050);
88 EXPECT_EQ(sum.load(), 5050);
89 pool.shutdown();
90 EXPECT_TRUE(pool.is_shutdown());
91}
92
93TEST(QueueTheadsPool, TheDestructorShutsThePoolDown)
94{
95 // It threw when shutdown() had not been called, which ended the program
96 std::atomic<int> sum{0};
97 {
98 Adder e; // it outlives the pool, whose thread
99 e.sum = &sum; // writes its status after run()
100 Pool pool;
101 pool.create_thread(&e);
102 EXPECT_TRUE(pool.put(7));
103 pool.resume();
104 wait_for(sum, 7);
105 }
106 EXPECT_EQ(sum.load(), 7);
107}
108
109TEST(QueueTheadsPool, AnEventOfAnotherThreadStartsNoThread)
110{
111 // The thread started before the event was checked: two threads ran with
112 // it, and the one that the pool did not count left it with a negative
113 // number of threads after shutdown()
114 std::atomic<int> sum{0};
115 Pool pool;
116 Adder e;
117 e.sum = &sum;
118 pool.create_thread(&e);
119 EXPECT_THROW(pool.create_thread(&e), std::domain_error);
120 EXPECT_TRUE(pool.put(5));
121 pool.resume();
122 wait_for(sum, 5);
123 pool.shutdown();
124 EXPECT_TRUE(pool.is_shutdown());
125 EXPECT_EQ(sum.load(), 5);
126}
127
128TEST(QueueTheadsPool, PutWithoutThreadsFails)
129{
130 Pool pool;
131 EXPECT_FALSE(pool.put(1));
132}
133
134TEST(QueueTheadsPool, ASuspendedPoolShutsDown)
135{
136 // Its threads did not leave their wait while it was suspended, as it is
137 // until resume(): shutdown(), and so the destructor, waited forever
138 std::atomic<int> sum{0};
139 {
140 Adder e; // it outlives the pool
141 e.sum = &sum;
142 Pool pool;
143 pool.create_thread(&e);
144 EXPECT_TRUE(pool.put(1));
145 }
146 EXPECT_EQ(sum.load(), 0); // never resumed: nothing consumed
147
148 Adder e1, e2;
149 e1.sum = e2.sum = &sum;
150 Pool pool;
151 pool.create_thread(&e1);
152 pool.create_thread(&e2);
153 pool.resume();
154 EXPECT_TRUE(pool.put(2));
155 wait_for(sum, 2);
156 pool.suspend();
157 EXPECT_TRUE(pool.put(3));
158 pool.shutdown();
159 EXPECT_TRUE(pool.is_shutdown());
160 EXPECT_EQ(sum.load(), 2); // the item put while suspended stays
161}
162
163TEST(QueueTheadsPool, AnEventShutsItsPoolDown)
164{
165 // An event that called shutdown() waited for its own thread, forever,
166 // also when another thread had started the shutdown
167 struct Stopper : Pool::Event
168 {
169 Pool * pool = nullptr;
170 std::atomic<bool> started{false};
171 std::atomic<bool> go{true};
172 std::atomic<int> runs{0};
173 void run() override
174 {
175 started = true;
176 while (not go)
177 std::this_thread::sleep_for(std::chrono::milliseconds(1));
178 pool->shutdown();
179 ++runs;
180 }
181 };
182
183 // The event starts the shutdown
184 {
185 Stopper e;
186 Pool pool;
187 e.pool = &pool;
188 pool.create_thread(&e);
189 EXPECT_TRUE(pool.put(1));
190 pool.resume();
191 for (int k = 0; k < 1000 and e.runs.load() == 0; ++k)
192 std::this_thread::sleep_for(std::chrono::milliseconds(10));
193 EXPECT_EQ(e.runs.load(), 1);
194 pool.shutdown();
195 EXPECT_TRUE(pool.is_shutdown());
196 }
197
198 // Another thread starts it while the event runs, and waits for it
199 Stopper e;
200 e.go = false;
201 Pool pool;
202 e.pool = &pool;
203 pool.create_thread(&e);
204 EXPECT_TRUE(pool.put(1));
205 pool.resume();
206 for (int k = 0; k < 1000 and not e.started.load(); ++k)
207 std::this_thread::sleep_for(std::chrono::milliseconds(10));
208 std::thread other([&pool] { pool.shutdown(); });
209 std::this_thread::sleep_for(std::chrono::milliseconds(100));
210 e.go = true;
211 other.join();
212 EXPECT_EQ(e.runs.load(), 1);
213 EXPECT_TRUE(pool.is_shutdown());
214}
215
216TEST(ConsumerQueueEvent, ItsRunIsVirtual)
217{
218 // run() was pure but not virtual: an event of it did not compile
219 struct Event : ConsumerQueueEvent<int>
220 {
221 bool run() override { return item > 0; }
222 };
223 Event e;
224 e.item = 3;
225 ConsumerQueueEvent<int> & base = e;
226 EXPECT_TRUE(base.run());
227}
228
229#if defined(_MSC_VER) && !defined(__clang__)
230# pragma warning(pop)
231#else
232# pragma GCC diagnostic pop
233#endif
size_t size_t int32_t value
Definition ca-c-api.h:116
Minimal std::expected-style result type for C++20.
An event of a queue: what its derived classes do with each item.
virtual bool run()=0
this is a item extracted from the queue
Pool of consumer threads form a queue.
#define TEST(name)
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Queue-based consumer thread pool.
static int * k