Debian Codegraph @

tests/src/parallel/test_WorkQueue.cpp

raw ◀ ▶
1 Debian patch touch this file
1 #include <pbcopper/parallel/WorkQueue.h>
2
3 #include <atomic>
4 #include <chrono>
5 #include <string>
6 #include <thread>
7 #include <vector>
8
9 #include <gtest/gtest.h>
10
11 void WorkerThread(PacBio::Parallel::WorkQueue<std::string>& queue, std::vector<std::string>* output)
12 {
13 auto LambdaWorker = [&](std::string&& ps) { output->emplace_back(std::move(ps)); };
14
15 while (queue.ConsumeWith(LambdaWorker)) {
16 }
17 }
18
19 TEST(Parallel_WorkQueue, strings)
20 {
21 static const size_t numThreads = 3;
22 static const size_t numElements = 10000;
23 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads};
24
25 std::vector<std::string> output;
26 output.reserve(numElements);
27 std::future<void> workerThread =
28 std::async(std::launch::async, WorkerThread, std::ref(workQueue), &output);
29
30 auto Submit = [](std::string& input) {
31 input += "-done";
32 return input;
33 };
34
35 std::vector<std::string> expected;
36 expected.reserve(numElements);
37 for (size_t i = 0; i < numElements; ++i) {
38 std::string tmp = std::to_string(i);
39 expected.emplace_back(tmp + "-done");
40 workQueue.ProduceWith(Submit, std::move(tmp));
41 }
42
43 EXPECT_NO_THROW(workQueue.Finalize());
44 EXPECT_NO_THROW(workerThread.wait());
45
46 EXPECT_EQ(expected.size(), numElements);
47 EXPECT_EQ(output.size(), numElements);
48 EXPECT_EQ(expected, output);
49 }
50
51 void WorkerThreadException(PacBio::Parallel::WorkQueue<std::string>& queue,
52 std::vector<std::string>* output)
53 {
54 auto LambdaWorker = [&](std::string&& ps) { output->emplace_back(std::move(ps)); };
55
56 try {
57 while (queue.ConsumeWith(LambdaWorker)) {
58 }
59 } catch (...) {
60 EXPECT_TRUE(false);
61 }
62 }
63
•64 /*
65 TEST(Parallel_WorkQueue, exceptionProduceWith)
66 {
67 static const size_t numThreads = 3;
68 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
69 std::vector<std::string> output;
70 std::future<void> workerThread =
71 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
72
73 auto Submit = [](std::string& input) {
74 input += "-done";
75 return input;
76 };
77
78 auto SubmitExc = [](std::string& input) {
79 input += "-done";
80 throw std::runtime_error{"faf abort"};
81 return input;
82 };
83
84 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
85 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
86 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"c"}));
87 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"0"}));
88 std::this_thread::sleep_for(std::chrono::milliseconds(10));
89 EXPECT_ANY_THROW(workQueue.ProduceWith(Submit, std::string{"1"}));
90
91 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
92 EXPECT_NO_THROW(workerThread.wait());
93 EXPECT_ANY_THROW(workQueue.Finalize());
94 }
•95 */
96
97 TEST(Parallel_WorkQueue, exceptionFinalize)
98 {
99 static const size_t numThreads = 3;
100 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
101 std::vector<std::string> output;
102 std::future<void> workerThread =
103 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
104
105 auto Submit = [](std::string& input) {
106 input += "-done";
107 return input;
108 };
109
110 auto SubmitExc = [](std::string& input) {
111 input += "-done";
112 throw std::runtime_error{"faf abort"};
113 return input;
114 };
115
116 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
117 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
118 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"c"}));
119 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"0"}));
120
121 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
122 EXPECT_NO_THROW(workerThread.wait());
123 EXPECT_ANY_THROW(workQueue.Finalize());
124 }
125
126 void WorkerThreadThrowException(PacBio::Parallel::WorkQueue<std::string>& queue,
127 std::vector<std::string>* output)
128 {
129 auto LambdaWorker = [&](std::string&& ps) {
130 output->emplace_back(std::move(ps));
131 throw std::runtime_error{"consumer abort"};
132 };
133
134 while (queue.ConsumeWith(LambdaWorker)) {
135 }
136 }
137
138 TEST(Parallel_WorkQueue, exceptionConsumer)
139 {
140 static const size_t numThreads = 3;
141 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
142 std::vector<std::string> output;
143 std::future<void> workerThread =
144 std::async(std::launch::async, WorkerThreadThrowException, std::ref(workQueue), &output);
145
146 auto Submit = [](std::string& input) {
147 input += "-done";
148 return input;
149 };
150
151 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
152 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
153
154 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
155 EXPECT_NO_THROW(workerThread.wait());
156 std::string exceptionMsg;
157 try {
158 workQueue.Finalize();
159 } catch (const std::runtime_error& e) {
160 exceptionMsg = e.what();
161 }
162 const std::string expectedError{"consumer abort"};
163 EXPECT_EQ(expectedError, exceptionMsg);
164 }
165
166 TEST(Parallel_WorkQueue, exceptionProduceWithCannotEscapeDestructor)
167 {
168 static const size_t numThreads = 3;
169 bool caughtException = false;
170 try {
171 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
172 std::vector<std::string> output;
173 std::future<void> workerThread =
174 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
175
176 auto SubmitExc = [](std::string& in) {
177 throw std::runtime_error{"encountered error"};
178 return in;
179 };
180
181 //
182 // TAG-4730: WorkQueue was (often) failing to throw exceptions from its
183 // workers, swallowing them at program exit. This test ensures
184 // that exceptions are _always_ propagated: whether during
185 // WorkQueue's normal execution/finalize, or at the very latest,
186 // from its destructor.
187 //
188 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"2"}));
189 workQueue.Finalize();
190 workerThread.wait();
191
192 } catch (const std::exception& e) {
193 caughtException = true;
194 }
195
196 EXPECT_TRUE(caughtException);
197 }