@@ -100,57 +100,13 @@ void wait_all(
100100 ThreadPool& pool, bool use_wait, std::vector<ThreadPool::Task>& results) {
101101 if (use_wait) {
102102 for (auto & r : results) {
103- REQUIRE (pool.wait (r). ok ( ));
103+ REQUIRE_NOTHROW (pool.wait (r));
104104 }
105105 } else {
106- REQUIRE (pool.wait_all (results). ok ( ));
106+ REQUIRE_NOTHROW (pool.wait_all (results));
107107 }
108108}
109109
110- /* *
111- * Use the wait or wait_all function to wait on all status.
112- *
113- * @return First failed status code or success.
114- */
115- Status wait_all_status (
116- ThreadPool& pool, bool use_wait, std::vector<ThreadPool::Task>& results) {
117- if (use_wait) {
118- Status ret;
119- for (auto & r : results) {
120- auto st = pool.wait (r);
121- if (ret.ok () && !st.ok ()) {
122- ret = st;
123- }
124- }
125-
126- return ret;
127- } else {
128- return pool.wait_all (results);
129- }
130- }
131-
132- /* *
133- * Use the wait or wait_all function to wait on all status.
134- *
135- * @return Number of successes.
136- */
137- uint64_t wait_all_num_status (
138- ThreadPool& pool, bool use_wait, std::vector<ThreadPool::Task>& results) {
139- int num_ok = 0 ;
140- if (use_wait) {
141- for (auto & r : results) {
142- num_ok += pool.wait (r).ok () ? 1 : 0 ;
143- }
144- } else {
145- std::vector<Status> statuses = pool.wait_all_status (results);
146- for (const auto & st : statuses) {
147- num_ok += st.ok () ? 1 : 0 ;
148- }
149- }
150-
151- return num_ok;
152- }
153-
154110TEST_CASE (" ThreadPool: Test empty" , " [threadpool]" ) {
155111 for (int i = 0 ; i < 10 ; i++) {
156112 ThreadPool pool{4 };
@@ -165,10 +121,7 @@ TEST_CASE("ThreadPool: Test single thread", "[threadpool]") {
165121 ThreadPool pool{1 };
166122
167123 for (int i = 0 ; i < 100 ; i++) {
168- ThreadPool::Task task = pool.execute ([&result]() {
169- result++;
170- return Status::Ok ();
171- });
124+ ThreadPool::Task task = pool.execute ([&result]() { result++; });
172125
173126 REQUIRE (task.valid ());
174127
@@ -184,30 +137,12 @@ TEST_CASE("ThreadPool: Test multiple threads", "[threadpool]") {
184137 std::vector<ThreadPool::Task> results;
185138 ThreadPool pool{4 };
186139 for (int i = 0 ; i < 100 ; i++) {
187- results.push_back (pool.execute ([&result]() {
188- result++;
189- return Status::Ok ();
190- }));
140+ results.push_back (pool.execute ([&result]() { result++; }));
191141 }
192142 wait_all (pool, use_wait, results);
193143 REQUIRE (result == 100 );
194144}
195145
196- TEST_CASE (" ThreadPool: Test wait status" , " [threadpool]" ) {
197- bool use_wait = GENERATE (true , false );
198- std::atomic<int > result (0 );
199- std::vector<ThreadPool::Task> results;
200- ThreadPool pool{4 };
201- for (int i = 0 ; i < 100 ; i++) {
202- results.push_back (pool.execute ([&result, i]() {
203- result++;
204- return i == 50 ? Status_Error (" Generic error" ) : Status::Ok ();
205- }));
206- }
207- REQUIRE (wait_all_num_status (pool, use_wait, results) == 99 );
208- REQUIRE (result == 100 );
209- }
210-
211146struct AtomicHolder {
212147 AtomicHolder (int val)
213148 : val_(val) {
@@ -223,7 +158,6 @@ TEST_CASE("ThreadPool: Test no wait", "[threadpool]") {
223158 ThreadPool::Task task = pool.execute ([result = ptr]() {
224159 result->val_ ++;
225160 std::this_thread::sleep_for (std::chrono::milliseconds (random_ms (1000 )));
226- return Status::Ok ();
227161 });
228162 REQUIRE (task.valid ());
229163 }
@@ -234,7 +168,6 @@ TEST_CASE("ThreadPool: Test no wait", "[threadpool]") {
234168
235169TEST_CASE (
236170 " ThreadPool: Test pending task cancellation" , " [threadpool][cancel]" ) {
237- bool use_wait = GENERATE (true , false );
238171 SECTION (" - No cancellation callback" ) {
239172 ThreadPool pool{2 };
240173
@@ -247,17 +180,12 @@ TEST_CASE(
247180 tasks.push_back (cancelable_tasks.execute (&pool, [&result]() {
248181 std::this_thread::sleep_for (std::chrono::seconds (2 ));
249182 result++;
250- return Status::Ok ();
251183 }));
252184 }
253185
254186 // Because the thread pool has 2 threads, the first two will probably be
255187 // executing at this point, but some will still be queued.
256- cancelable_tasks.cancel_all_tasks ();
257-
258- // The result is the number of threads that returned Ok (were not
259- // cancelled).
260- REQUIRE (result == wait_all_num_status (pool, use_wait, tasks));
188+ REQUIRE_THROWS (cancelable_tasks.cancel_all_tasks ());
261189 }
262190
263191 SECTION (" - With cancellation callback" ) {
@@ -272,18 +200,13 @@ TEST_CASE(
272200 [&result]() {
273201 std::this_thread::sleep_for (std::chrono::seconds (2 ));
274202 result++;
275- return Status::Ok ();
276203 },
277204 [&num_cancelled]() { num_cancelled++; }));
278205 }
279206
280207 // Because the thread pool has 2 threads, the first two will probably be
281208 // executing at this point, but some will still be queued.
282- cancelable_tasks.cancel_all_tasks ();
283-
284- // The result is the number of threads that returned Ok (were not
285- // cancelled).
286- REQUIRE (result == wait_all_num_status (pool, use_wait, tasks));
209+ REQUIRE_THROWS (cancelable_tasks.cancel_all_tasks ());
287210 REQUIRE (num_cancelled == ((int64_t )tasks.size () - result));
288211 }
289212}
@@ -311,12 +234,10 @@ TEST_CASE("ThreadPool: Test recursion, simplest case", "[threadpool]") {
311234 auto b = pool.execute ([&result]() {
312235 std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
313236 ++result;
314- return Status::Ok ();
315237 });
316238 REQUIRE (b.valid ());
317239 tasks.emplace_back (std::move (b));
318240 wait_all (pool, use_wait, tasks);
319- return Status::Ok ();
320241 });
321242 REQUIRE (a.valid ());
322243 tasks.emplace_back (std::move (a));
@@ -353,14 +274,12 @@ TEST_CASE("ThreadPool: Test recursion", "[threadpool]") {
353274 auto inner_task = pool.execute ([&]() {
354275 std::this_thread::sleep_for (std::chrono::milliseconds (random_ms ()));
355276 ++result;
356- return Status::Ok ();
357277 });
358278
359279 inner_tasks.emplace_back (std::move (inner_task));
360280 }
361281
362282 wait_all (pool, use_wait, inner_tasks);
363- return Status::Ok ();
364283 });
365284
366285 REQUIRE (task.valid ());
@@ -383,11 +302,8 @@ TEST_CASE("ThreadPool: Test recursion", "[threadpool]") {
383302 if (--result == 0 ) {
384303 cv.notify_all ();
385304 }
386- return Status::Ok ();
387305 });
388306 }
389-
390- return Status::Ok ();
391307 });
392308
393309 REQUIRE (task.valid ());
@@ -445,21 +361,18 @@ TEST_CASE("ThreadPool: Test recursion, two pools", "[threadpool]") {
445361 std::this_thread::sleep_for (
446362 std::chrono::milliseconds (random_ms ()));
447363 ++result;
448- return Status::Ok ();
449364 });
450365
451366 tasks_c.emplace_back (std::move (task_c));
452367 }
453368
454369 wait_all (pool_a, use_wait, tasks_c);
455- return Status::Ok ();
456370 });
457371
458372 tasks_b.emplace_back (std::move (task_b));
459373 }
460374
461375 wait_all (pool_b, use_wait, tasks_b);
462- return Status::Ok ();
463376 });
464377
465378 REQUIRE (task_a.valid ());
@@ -487,21 +400,18 @@ TEST_CASE("ThreadPool: Test recursion, two pools", "[threadpool]") {
487400 std::unique_lock<std::mutex> ul (cv_mutex);
488401 cv.notify_all ();
489402 }
490- return Status::Ok ();
491403 });
492404
493405 tasks_c.emplace_back (std::move (task_c));
494406 }
495407
496408 wait_all (pool_a, use_wait, tasks_c);
497- return Status::Ok ();
498409 });
499410
500411 tasks_b.emplace_back (std::move (task_b));
501412 }
502413
503414 wait_all (pool_b, use_wait, tasks_b);
504- return Status::Ok ();
505415 });
506416
507417 REQUIRE (task_a.valid ());
@@ -533,14 +443,10 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
533443 if (tmp == 13 ) {
534444 throw (std::string (" Unripe banana" ));
535445 }
536- return Status::Ok ();
537446 }));
538447 }
539448
540- REQUIRE (
541- wait_all_status (pool, use_wait, results).to_string () ==
542- unripe_banana_status.to_string ());
543- REQUIRE (result == 207 );
449+ REQUIRE_THROWS_WITH (wait_all (pool, use_wait, results), " Unripe banana" );
544450 }
545451
546452 SECTION (" One tile error exception" ) {
@@ -550,16 +456,14 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
550456 results.push_back (pool.execute ([&result, &unbaked_potato_status]() {
551457 auto tmp = result++;
552458 if (tmp == 31 ) {
553- throw (unbaked_potato_status);
459+ throw StatusException (unbaked_potato_status);
554460 }
555- return Status::Ok ();
556461 }));
557462 }
558463
559- REQUIRE (
560- wait_all_status (pool, use_wait, results).to_string () ==
561- unbaked_potato_status.to_string ());
562- REQUIRE (result == 207 );
464+ REQUIRE_THROWS_WITH (
465+ wait_all (pool, use_wait, results),
466+ Catch::Matchers::EndsWith (" Unbaked potato" ));
563467 }
564468
565469 SECTION (" Two exceptions" ) {
@@ -574,16 +478,13 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
574478 if (tmp == 31 ) {
575479 throw (Status_TileError (" Unbaked potato" ));
576480 }
577-
578- return Status::Ok ();
579481 }));
580482 }
581483
582- auto pool_status = wait_all_status (pool, use_wait, results);
583- REQUIRE (
584- ((pool_status.to_string () == unripe_banana_status.to_string ()) ||
585- (pool_status.to_string () == unbaked_potato_status.to_string ())));
586- REQUIRE (result == 207 );
484+ REQUIRE_THROWS_WITH (
485+ wait_all (pool, use_wait, results),
486+ Catch::Matchers::Equals (" Unripe banana" ) ||
487+ Catch::Matchers::Equals (" Unbaked potato" ));
587488 }
588489
589490 SECTION (" Two exceptions reverse order" ) {
@@ -598,16 +499,13 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
598499 if (tmp == 13 ) {
599500 throw (Status_TileError (" Unbaked potato" ));
600501 }
601-
602- return Status::Ok ();
603502 }));
604503 }
605504
606- auto pool_status = wait_all_status (pool, use_wait, results);
607- REQUIRE (
608- ((pool_status.to_string () == unripe_banana_status.to_string ()) ||
609- (pool_status.to_string () == unbaked_potato_status.to_string ())));
610- REQUIRE (result == 207 );
505+ REQUIRE_THROWS_WITH (
506+ wait_all (pool, use_wait, results),
507+ Catch::Matchers::Equals (" Unripe banana" ) ||
508+ Catch::Matchers::Equals (" Unbaked potato" ));
611509 }
612510
613511 SECTION (" Two exceptions strict order" ) {
@@ -622,15 +520,13 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
622520 if (i == 31 ) {
623521 throw (Status_TileError (" Unbaked potato" ));
624522 }
625-
626- return Status::Ok ();
627523 }));
628524 }
629525
630- REQUIRE (
631- wait_all_status (pool, use_wait, results). to_string () ==
632- unripe_banana_status. to_string ());
633- REQUIRE (result == 207 );
526+ REQUIRE_THROWS_WITH (
527+ wait_all (pool, use_wait, results),
528+ Catch::Matchers::Equals ( " Unripe banana " ) ||
529+ Catch::Matchers::Equals ( " Unbaked potato " ) );
634530 }
635531
636532 SECTION (" Two exceptions strict reverse order" ) {
@@ -645,14 +541,12 @@ TEST_CASE("ThreadPool: Test Exceptions", "[threadpool]") {
645541 if (i == 13 ) {
646542 throw (Status_TileError (" Unbaked potato" ));
647543 }
648-
649- return Status::Ok ();
650544 }));
651545 }
652546
653- REQUIRE (
654- wait_all_status (pool, use_wait, results). to_string () ==
655- unbaked_potato_status. to_string ());
656- REQUIRE (result == 207 );
547+ REQUIRE_THROWS_WITH (
548+ wait_all (pool, use_wait, results),
549+ Catch::Matchers::Equals ( " Unripe banana " ) ||
550+ Catch::Matchers::Equals ( " Unbaked potato " ) );
657551 }
658552}
0 commit comments