Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
worker_pool_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
38#include <gtest/gtest.h>
39
40#include <atomic>
41#include <chrono>
42#include <cstdlib>
43#include <stdexcept>
44#include <thread>
45
46// WorkersSet is deprecated, but supported until 8.0.0: this test is its
47// 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 <worker_pool.H>
57
58namespace
59{
60 std::atomic<int> calls{0};
61 std::atomic<int> item_sum{0};
62
63 // An item for the pool, which frees it with free()
64 void * make_item(int v)
65 {
66 auto * p = static_cast<int *>(std::malloc(sizeof(int)));
67 *p = v;
68 return p;
69 }
70
71 bool add_item(void * p)
72 {
73 item_sum += *static_cast<int *>(p);
74 ++calls;
75 return false;
76 }
77
78 // Ends the job at a negative item
79 bool stop_at_a_negative(void * p)
80 {
81 ++calls;
82 return *static_cast<int *>(p) < 0;
83 }
84
85 using Pool = WorkersSet<bool (*)(void *)>;
86}
87
88TEST(WorkersSet, ProcessesEveryScheduledItem)
89{
90 calls = 0;
91 item_sum = 0;
92 Pool pool(&add_item, 100, 4);
93 pool.prepare_num_workers(50);
94 for (int i = 1; i <= 50; ++i)
95 pool.schedule_call(make_item(i));
96 pool.wait_until_all_workers_finished_or_job_is_done();
97 EXPECT_EQ(calls.load(), 50);
98 EXPECT_EQ(item_sum.load(), 1275);
99
100 // is_jobs_done() is const, and locked a mutex that was not mutable: it
101 // did not compile
102 const Pool & cpool = pool;
103 EXPECT_FALSE(cpool.is_jobs_done());
104}
105
106TEST(WorkersSet, TheWaitReturnsWhenTheItemsAreDoneBeforeIt)
107{
108 // The wait had no predicate: it waited forever when the items were done
109 // before it started
110 calls = 0;
111 item_sum = 0;
112 Pool pool(&add_item, 10, 2);
113 pool.prepare_num_workers(3);
114 for (int i = 1; i <= 3; ++i)
115 pool.schedule_call(make_item(i));
116 for (int k = 0; k < 500 and calls.load() < 3; ++k)
117 std::this_thread::sleep_for(std::chrono::milliseconds(10));
118 pool.wait_until_all_workers_finished_or_job_is_done();
119 EXPECT_EQ(item_sum.load(), 6);
120}
121
122TEST(WorkersSet, ItemsScheduledBeforeTheirCountAreProcessed)
123{
124 // prepare_num_workers() did not wake the workers: the items scheduled
125 // before it waited forever
126 calls = 0;
127 item_sum = 0;
128 Pool pool(&add_item, 10, 2);
129 for (int i = 1; i <= 4; ++i)
130 pool.schedule_call(make_item(i));
131 // So that the workers that the items woke wait again for the count
132 std::this_thread::sleep_for(std::chrono::milliseconds(200));
133 pool.prepare_num_workers(4);
134 pool.wait_until_all_workers_finished_or_job_is_done();
135 EXPECT_EQ(item_sum.load(), 10);
136}
137
138TEST(WorkersSet, AnItemThatReturnsTrueEndsTheJob)
139{
140 // job_done was written without the lock, and the items after it were
141 // not freed, which LeakSanitizer reports: the job sanitizers of CI runs
142 // this test with it
143 calls = 0;
144 Pool pool(&stop_at_a_negative, 100, 1);
145 pool.prepare_num_workers(20);
146 pool.schedule_call(make_item(-1));
147 for (int i = 1; i < 20; ++i)
148 pool.schedule_call(make_item(i));
149 pool.wait_until_all_workers_finished_or_job_is_done();
150 EXPECT_TRUE(pool.is_jobs_done());
151 EXPECT_GE(calls.load(), 1);
152}
153
154TEST(WorkersSet, AFullQueueThrows)
155{
156 // The queue took more items than its size, which an assert only checked
157 calls = 0;
158 item_sum = 0;
159 Pool pool(&add_item, 2, 1);
160 pool.schedule_call(make_item(1)); // no count yet: the items wait
161 pool.schedule_call(make_item(2));
162 void * third = make_item(3);
163 EXPECT_THROW(pool.schedule_call(third), std::overflow_error);
164 std::free(third); // the pool did not take it
165 // The destructor frees the two items that are still queued
166}
167
168TEST(WorkersSet, AShutdownEndsTheWait)
169{
170 // The workers of a pool shut down leave the items that they did not
171 // take, and a wait for them waited forever
172 calls = 0;
173 item_sum = 0;
174 Pool pool(&add_item, 10, 1);
175 pool.prepare_num_workers(3); // three items, of which one comes
176 pool.schedule_call(make_item(1));
177 std::thread other([&pool]
178 {
179 std::this_thread::sleep_for(std::chrono::milliseconds(100));
180 pool.shutdown();
181 });
182 pool.wait_until_all_workers_finished_or_job_is_done();
183 other.join();
184 EXPECT_FALSE(pool.is_jobs_done());
185 EXPECT_LE(item_sum.load(), 1);
186}
187
188#if defined(_MSC_VER) && !defined(__clang__)
189# pragma warning(pop)
190#else
191# pragma GCC diagnostic pop
192#endif
Thread pool for parallel worker function execution.
#define TEST(name)
and
Check uniqueness with explicit hash + equality functors.
static int * k
General-purpose worker thread pool for parallel task execution.