Debian Codegraph @

tests/src/parallel/test_FireAndForget.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/FireAndForget.h>
2
3 #include <chrono>
4 #include <memory>
5 #include <string>
6 #include <thread>
7 #include <vector>
8
9 #include <gtest/gtest.h>
10
11 TEST(Parallel_FireAndForget, strings)
12 {
13 static const size_t numThreads = 3;
14 static const size_t numElements = 10000;
15 PacBio::Parallel::FireAndForget faf{numThreads};
16
17 std::atomic_int waiting{0};
18 auto Submit = [&waiting](std::string& input) {
19 input = "done-" + input;
20 --waiting;
21 };
22
23 std::vector<std::string> vec;
24
25 for (size_t i = 0; i < numElements; ++i) {
26 vec.emplace_back(std::to_string(i));
27 }
28
29 for (auto& v : vec) {
30 ++waiting;
31 EXPECT_NO_THROW(faf.ProduceWith(Submit, std::ref(v)));
32 }
33
34 EXPECT_NO_THROW(faf.Finalize());
35 EXPECT_EQ(waiting, 0);
36
37 for (auto& v : vec) {
38 EXPECT_EQ(v.substr(0, 4), "done");
39 }
40
41 EXPECT_EQ(vec.size(), numElements);
42 }
43
44 TEST(Parallel_FireAndForget, exceptionFinalize)
45 {
46 static const size_t numThreads = 3;
47 PacBio::Parallel::FireAndForget faf{numThreads, 1};
48
49 std::atomic_int counter{0};
50 auto SubmitSleep = [&counter]() {
51 std::this_thread::sleep_for(std::chrono::milliseconds(10));
52 ++counter;
53 };
54 auto SubmitExc = [&counter]() {
55 throw std::runtime_error("faf abort");
56 ++counter;
57 };
58
59 EXPECT_NO_THROW(faf.ProduceWith(SubmitSleep));
60 EXPECT_NO_THROW(faf.ProduceWith(SubmitSleep));
61 EXPECT_NO_THROW(faf.ProduceWith(SubmitExc));
62
63 EXPECT_ANY_THROW(faf.Finalize());
64
65 EXPECT_EQ(counter, 2);
66 }
67
68 TEST(Parallel_FireAndForget, exceptionProduceWith)
69 {
70 static const size_t numThreads = 3;
71 PacBio::Parallel::FireAndForget faf{numThreads, 1};
72
73 std::atomic_int counter{0};
74 auto SubmitSleep = [&counter]() {
75 std::this_thread::sleep_for(std::chrono::milliseconds(100));
76 ++counter;
77 };
78 auto Submit = [&counter]() { ++counter; };
79 auto SubmitExc = [&counter]() {
80 throw std::runtime_error{"faf abort"};
81 ++counter;
82 };
83
84 EXPECT_NO_THROW(faf.ProduceWith(SubmitSleep));
85 EXPECT_NO_THROW(faf.ProduceWith(SubmitSleep));
86 EXPECT_NO_THROW(faf.ProduceWith(SubmitExc));
87 std::this_thread::sleep_for(std::chrono::milliseconds(10));
88 EXPECT_ANY_THROW(faf.ProduceWith(Submit););
89 EXPECT_ANY_THROW(faf.Finalize());
90
91 EXPECT_EQ(counter, 2);
92 }
93
94 TEST(Parallel_FireAndForget, dispatch)
95 {
96 static const size_t numThreads = 3;
97 static const size_t numElements = 1000;
98 PacBio::Parallel::FireAndForget faf{numThreads};
99
100 std::vector<std::string> vec;
101 for (size_t i = 0; i < numElements; ++i) {
102 vec.emplace_back(std::to_string(i));
103 }
104
105 const auto Submit = [&vec](const int32_t i) {
106 std::string& input = vec[i];
107 input = "done-" + input;
108 };
109
110 PacBio::Parallel::Dispatch(&faf, numElements, Submit);
111
112 for (auto& v : vec) {
113 EXPECT_EQ(v.substr(0, 4), "done");
114 }
115
116 EXPECT_EQ(vec.size(), numElements);
117
118 faf.Finalize();
119 }
120
121 TEST(Parallel_FireAndForget, dispatchSingleException)
122 {
123 PacBio::Parallel::FireAndForget faf{1};
124
125 auto SubmitExc = [](const int32_t) { throw std::runtime_error("faf abort"); };
126
127 EXPECT_ANY_THROW(PacBio::Parallel::Dispatch(&faf, 1, SubmitExc));
128
129 faf.Finalize();
130 }