Debian Codegraph @

tests/src/parallel/test_FireAndForget.cpp

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