]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | #include <chrono> |
9f95a23c | 2 | #include <iostream> |
11fdf7f2 TL |
3 | #include <numeric> |
4 | #include <seastar/core/app-template.hh> | |
5 | #include "crimson/thread/ThreadPool.h" | |
6 | ||
7 | using namespace std::chrono_literals; | |
9f95a23c | 8 | using ThreadPool = crimson::thread::ThreadPool; |
11fdf7f2 TL |
9 | |
10 | seastar::future<> test_accumulate(ThreadPool& tp) { | |
11 | static constexpr auto N = 5; | |
12 | static constexpr auto M = 1; | |
13 | auto slow_plus = [&tp](int i) { | |
14 | return tp.submit([=] { | |
15 | std::this_thread::sleep_for(10ns); | |
16 | return i + M; | |
17 | }); | |
18 | }; | |
19 | return seastar::map_reduce( | |
20 | boost::irange(0, N), slow_plus, 0, std::plus{}).then([] (int sum) { | |
21 | auto r = boost::irange(0 + M, N + M); | |
22 | if (sum != std::accumulate(r.begin(), r.end(), 0)) { | |
23 | throw std::runtime_error("test_accumulate failed"); | |
24 | } | |
25 | }); | |
26 | } | |
27 | ||
9f95a23c TL |
28 | seastar::future<> test_void_return(ThreadPool& tp) { |
29 | return tp.submit([=] { | |
30 | std::this_thread::sleep_for(10ns); | |
31 | }); | |
32 | } | |
33 | ||
11fdf7f2 TL |
34 | int main(int argc, char** argv) |
35 | { | |
36 | ThreadPool tp{2, 128, 0}; | |
37 | seastar::app_template app; | |
38 | return app.run(argc, argv, [&tp] { | |
39 | return tp.start().then([&tp] { | |
40 | return test_accumulate(tp); | |
9f95a23c TL |
41 | }).then([&tp] { |
42 | return test_void_return(tp); | |
11fdf7f2 TL |
43 | }).handle_exception([](auto e) { |
44 | std::cerr << "Error: " << e << std::endl; | |
45 | seastar::engine().exit(1); | |
46 | }).finally([&tp] { | |
47 | return tp.stop(); | |
48 | }); | |
49 | }); | |
50 | } | |
51 | ||
52 | /* | |
53 | * Local Variables: | |
54 | * compile-command: "make -j4 \ | |
55 | * -C ../../../build \ | |
56 | * unittest_seastar_thread_pool" | |
57 | * End: | |
58 | */ |