]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/leaf/test/capture_exception_result_async_test.cpp
1 // Copyright (c) 2018-2020 Emil Dotchevski and Reverge Studios, Inc.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/leaf/detail/config.hpp>
7 #if defined(BOOST_LEAF_NO_EXCEPTIONS) || defined(BOOST_LEAF_NO_THREADS)
13 std::cout
<< "Unit test not applicable." << std::endl
;
19 #include <boost/leaf/capture.hpp>
20 #include <boost/leaf/result.hpp>
21 #include <boost/leaf/handle_errors.hpp>
23 #include "lightweight_test.hpp"
29 namespace leaf
= boost::leaf
;
31 template <int> struct info
{ int value
; };
38 std::future
<leaf::result
<int>> fut
;
41 template <class H
, class F
>
42 std::vector
<fut_info
> launch_tasks( int task_count
, F f
)
44 BOOST_LEAF_ASSERT(task_count
>0);
45 std::vector
<fut_info
> fut
;
46 std::generate_n( std::back_inserter(fut
), task_count
,
51 int const res
= (rand()%10) - 5;
52 return fut_info
{ a
, b
, res
, std::async( std::launch::async
,
55 return leaf::capture(leaf::make_shared_context
<H
>(), f
, a
, b
, res
);
63 int received_a
, received_b
;
64 auto error_handlers
= std::make_tuple(
65 [&]( info
<1> const & x1
, info
<2> const & x2
, info
<4> const & )
67 received_a
= x1
.value
;
68 received_b
= x2
.value
;
77 std::vector
<fut_info
> fut
= launch_tasks
<decltype(error_handlers
)>(
79 []( int a
, int b
, int res
) -> leaf::result
<int>
84 return leaf::new_error( info
<1>{a
}, info
<2>{b
}, info
<3>{} );
90 received_a
= received_b
= 0;
91 int r
= leaf::try_handle_all(
94 auto load
= leaf::on_error( info
<4>{} );
96 // Calling future_get is required in order to make the on_error (above) work.
97 return leaf::future_get(f
.fut
);
101 BOOST_TEST_EQ(r
, f
.result
);
104 BOOST_TEST_EQ(r
, -1);
105 BOOST_TEST_EQ(received_a
, f
.a
);
106 BOOST_TEST_EQ(received_b
, f
.b
);
112 std::vector
<fut_info
> fut
= launch_tasks
<decltype(error_handlers
)>(
114 []( int a
, int b
, int res
) -> leaf::result
<int>
119 return leaf::new_error( info
<1>{a
}, info
<2>{b
}, info
<3>{} );
122 for( auto & f
: fut
)
125 received_a
= received_b
= 0;
126 int r
= leaf::try_handle_all(
129 auto load
= leaf::on_error( info
<4>{} );
131 return leaf::try_handle_some(
134 // Not calling future_get, a on_error in this scope won't work correctly.
135 // This is to verify that the on_error in the outer scope (above) works.
138 []( leaf::error_info
const & err
)
145 BOOST_TEST_EQ(r
, f
.result
);
148 BOOST_TEST_EQ(r
, -1);
149 BOOST_TEST_EQ(received_a
, f
.a
);
150 BOOST_TEST_EQ(received_b
, f
.b
);
155 return boost::report_errors();