2
0
mirror of https://github.com/boostorg/leaf.git synced 2026-01-30 20:02:15 +00:00
Files
leaf/test/capture_exception_async_test.cpp
2019-01-13 09:13:54 -08:00

86 lines
1.7 KiB
C++

// Copyright (c) 2018 Emil Dotchevski
// Copyright (c) 2018 Second Spectrum, Inc.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/leaf/capture_exception.hpp>
#include <boost/leaf/try.hpp>
#include <boost/leaf/throw.hpp>
#include "boost/core/lightweight_test.hpp"
#include <vector>
#include <future>
#include <iterator>
#include <algorithm>
namespace leaf = boost::leaf;
template <int> struct info { int value; };
struct fut_info
{
int a;
int b;
int result;
std::future<int> fut;
};
template <class... E, class F>
std::vector<fut_info> launch_tasks( int task_count, F f )
{
assert(task_count>0);
std::vector<fut_info> fut;
std::generate_n( std::inserter(fut,fut.end()), task_count,
[=]
{
int const a = rand();
int const b = rand();
int const res = (rand()%10) - 5;
return fut_info { a, b, res, std::async( std::launch::async,
[=]
{
return f(a,b,res);
} ) };
} );
return fut;
}
int main()
{
std::vector<fut_info> fut = launch_tasks<info<1>, info<2>>( 42,
leaf::capture_exception<info<1>,info<2>,info<3>>(
[ ]( int a, int b, int res )
{
if( res>=0 )
return res;
else
throw leaf::exception( std::exception(), info<1>{a}, info<2>{b}, info<3>{} );
} ) );
for( auto & f : fut )
{
f.fut.wait();
int r = leaf::try_(
[&]
{
return f.fut.get();
},
[&]( info<1> const & x1, info<2> const & x2 )
{
BOOST_TEST(x1.value==f.a);
BOOST_TEST(x2.value==f.b);
return -1;
},
[ ]
{
return -2;
} );
if( f.result>=0 )
BOOST_TEST(r==f.result);
else
BOOST_TEST(r==-1);
}
return boost::report_errors();
}