Skip to content

Commit

Permalink
Async test tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zajo committed Dec 17, 2023
1 parent 98e9926 commit cd3ee4e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
40 changes: 17 additions & 23 deletions test/capture_exception_async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ std::vector<fut_info> launch_tasks( int task_count, F f )

int main()
{
int const task_count = 1;
int const task_count = 100;
int received_a, received_b;

auto task =
[]( int a, int b, int res ) -> leaf::result<int>
{
if( res >= 0 )
return res;
else
leaf::throw_exception(info<1>{a}, info<2>{b}, info<3>{});
};

auto error_handlers = std::make_tuple(
[&]( info<1> const & x1, info<2> const & x2, info<4> const & )
{
Expand All @@ -89,15 +99,7 @@ int main()
} );

{
std::vector<fut_info> fut = launch_tasks(
task_count,
[]( int a, int b, int res )
{
if( res >= 0 )
return res;
else
leaf::throw_exception(info<1>{a}, info<2>{b}, info<3>{});
} );
std::vector<fut_info> fut = launch_tasks(task_count, task);

for( auto & f : fut )
{
Expand All @@ -115,22 +117,14 @@ int main()
else
{
BOOST_TEST_EQ(r, -1);
BOOST_TEST_EQ(received_a, f.a);
BOOST_TEST_EQ(received_b, f.b);
BOOST_TEST_EQ(f.a, received_a);
BOOST_TEST_EQ(f.b, received_b);
}
}
}

{
std::vector<fut_info> fut = launch_tasks(
task_count,
[]( int a, int b, int res )
{
if( res >= 0 )
return res;
else
leaf::throw_exception(info<1>{a}, info<2>{b}, info<3>{});
} );
std::vector<fut_info> fut = launch_tasks(task_count, task);

for( auto & f : fut )
{
Expand All @@ -157,8 +151,8 @@ int main()
else
{
BOOST_TEST_EQ(r, -1);
BOOST_TEST_EQ(received_a, f.a);
BOOST_TEST_EQ(received_b, f.b);
BOOST_TEST_EQ(f.a, received_a);
BOOST_TEST_EQ(f.b, received_b);
}
}
}
Expand Down
36 changes: 15 additions & 21 deletions test/capture_result_async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ int main()
{
int const task_count = 100;
int received_a, received_b;

auto task =
[]( int a, int b, int res ) -> leaf::result<int>
{
if( res >= 0 )
return res;
else
return leaf::new_error( info<1>{a}, info<2>{b}, info<3>{} );
};

auto error_handlers = std::make_tuple(
[&received_a, &received_b]( info<1> const & x1, info<2> const & x2, info<4> const & )
[&]( info<1> const & x1, info<2> const & x2, info<4> const & )
{
received_a = x1.value;
received_b = x2.value;
Expand All @@ -88,19 +98,12 @@ int main()
} );

{
std::vector<fut_info> fut = launch_tasks(
task_count,
[]( int a, int b, int res ) -> leaf::result<int>
{
if( res >= 0 )
return res;
else
return leaf::new_error( info<1>{a}, info<2>{b}, info<3>{} );
} );
std::vector<fut_info> fut = launch_tasks(task_count, task);

for( auto & f : fut )
{
f.fut.wait();
received_a = received_b = 0;
int r = leaf::try_handle_all(
[&]
{
Expand All @@ -122,19 +125,12 @@ int main()
}

{
std::vector<fut_info> fut = launch_tasks(
task_count,
[]( int a, int b, int res ) -> leaf::result<int>
{
if( res >= 0 )
return res;
else
return leaf::new_error( info<1>{a}, info<2>{b}, info<3>{} );
} );
std::vector<fut_info> fut = launch_tasks(task_count, task);

for( auto & f : fut )
{
f.fut.wait();
received_a = received_b = 0;
int r = leaf::try_handle_all(
[&]
{
Expand All @@ -143,8 +139,6 @@ int main()
return leaf::try_handle_some(
[&]
{
// Not calling future_get, a on_error in this scope won't work correctly.
// This is to verify that the on_error in the outer scope (above) works.
return f.fut.get();
},
[]( leaf::error_info const & err )
Expand Down

0 comments on commit cd3ee4e

Please sign in to comment.