Debian Codegraph @

tests/src/parallel/test_WorkQueue.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 TEST(Parallel_WorkQueue, exceptionProduceWith)
65 {
66 static const size_t numThreads = 3;
67 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
68 std::vector<std::string> output;
69 std::future<void> workerThread =
70 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
71
72 auto Submit = [](std::string& input) {
73 input += "-done";
74 return input;
75 };
76
77 auto SubmitExc = [](std::string& input) {
78 input += "-done";
79 throw std::runtime_error{"faf abort"};
80 return input;
81 };
82
83 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
84 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
85 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"c"}));
86 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"0"}));
87 std::this_thread::sleep_for(std::chrono::milliseconds(10));
88 EXPECT_ANY_THROW(workQueue.ProduceWith(Submit, std::string{"1"}));
89
90 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
91 EXPECT_NO_THROW(workerThread.wait());
92 EXPECT_ANY_THROW(workQueue.Finalize());
93 }
94
95 TEST(Parallel_WorkQueue, exceptionFinalize)
96 {
97 static const size_t numThreads = 3;
98 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
99 std::vector<std::string> output;
100 std::future<void> workerThread =
101 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
102
103 auto Submit = [](std::string& input) {
104 input += "-done";
105 return input;
106 };
107
108 auto SubmitExc = [](std::string& input) {
109 input += "-done";
110 throw std::runtime_error{"faf abort"};
111 return input;
112 };
113
114 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
115 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
116 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"c"}));
117 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"0"}));
118
119 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
120 EXPECT_NO_THROW(workerThread.wait());
121 EXPECT_ANY_THROW(workQueue.Finalize());
122 }
123
124 void WorkerThreadThrowException(PacBio::Parallel::WorkQueue<std::string>& queue,
125 std::vector<std::string>* output)
126 {
127 auto LambdaWorker = [&](std::string&& ps) {
128 output->emplace_back(std::move(ps));
129 throw std::runtime_error{"consumer abort"};
130 };
131
132 while (queue.ConsumeWith(LambdaWorker)) {
133 }
134 }
135
136 TEST(Parallel_WorkQueue, exceptionConsumer)
137 {
138 static const size_t numThreads = 3;
139 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
140 std::vector<std::string> output;
141 std::future<void> workerThread =
142 std::async(std::launch::async, WorkerThreadThrowException, std::ref(workQueue), &output);
143
144 auto Submit = [](std::string& input) {
145 input += "-done";
146 return input;
147 };
148
149 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"a"}));
150 EXPECT_NO_THROW(workQueue.ProduceWith(Submit, std::string{"b"}));
151
152 EXPECT_NO_THROW(workQueue.FinalizeWorkers());
153 EXPECT_NO_THROW(workerThread.wait());
154 std::string exceptionMsg;
155 try {
156 workQueue.Finalize();
157 } catch (const std::runtime_error& e) {
158 exceptionMsg = e.what();
159 }
160 const std::string expectedError{"consumer abort"};
161 EXPECT_EQ(expectedError, exceptionMsg);
162 }
163
164 TEST(Parallel_WorkQueue, exceptionProduceWithCannotEscapeDestructor)
165 {
166 static const size_t numThreads = 3;
167 bool caughtException = false;
168 try {
169 PacBio::Parallel::WorkQueue<std::string> workQueue{numThreads, 1};
170 std::vector<std::string> output;
171 std::future<void> workerThread =
172 std::async(std::launch::async, WorkerThreadException, std::ref(workQueue), &output);
173
174 auto SubmitExc = [](std::string& in) {
175 throw std::runtime_error{"encountered error"};
176 return in;
177 };
178
179 //
180 // TAG-4730: WorkQueue was (often) failing to throw exceptions from its
181 // workers, swallowing them at program exit. This test ensures
182 // that exceptions are _always_ propagated: whether during
183 // WorkQueue's normal execution/finalize, or at the very latest,
184 // from its destructor.
185 //
186 EXPECT_NO_THROW(workQueue.ProduceWith(SubmitExc, std::string{"2"}));
187 workQueue.Finalize();
188 workerThread.wait();
189
190 } catch (const std::exception& e) {
191 caughtException = true;
192 }
193
194 EXPECT_TRUE(caughtException);
195 }