]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/crimson/test_errorator.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
2 // vim: ts=8 sw=2 smarttab
4 #include <boost/iterator/counting_iterator.hpp>
7 #include "test/crimson/gtest_seastar.h"
9 #include "crimson/common/errorator.h"
10 #include "crimson/common/errorator-loop.h"
11 #include "crimson/common/log.h"
12 #include "seastar/core/sleep.hh"
14 struct errorator_test_t
: public seastar_test_suite_t
{
15 using ertr
= crimson::errorator
<crimson::ct_error::invarg
>;
16 ertr::future
<> test_do_until() {
17 return crimson::repeat([i
=0]() mutable {
20 return ertr::make_ready_future
<seastar::stop_iteration
>(
21 seastar::stop_iteration::no
);
23 return ertr::make_ready_future
<seastar::stop_iteration
>(
24 seastar::stop_iteration::yes
);
28 static constexpr int SIZE
= 42;
29 ertr::future
<> test_parallel_for_each() {
30 auto sum
= std::make_unique
<int>(0);
31 return ertr::parallel_for_each(
32 boost::make_counting_iterator(0),
33 boost::make_counting_iterator(SIZE
),
34 [sum
=sum
.get()](int i
) {
36 }).safe_then([sum
=std::move(sum
)] {
37 int expected
= std::accumulate(boost::make_counting_iterator(0),
38 boost::make_counting_iterator(SIZE
),
40 ASSERT_EQ(*sum
, expected
);
43 struct noncopyable_t
{
44 constexpr noncopyable_t() = default;
45 ~noncopyable_t() = default;
46 noncopyable_t(noncopyable_t
&&) = default;
48 noncopyable_t(const noncopyable_t
&) = delete;
49 noncopyable_t
& operator=(const noncopyable_t
&) = delete;
51 ertr::future
<> test_non_copy_then() {
52 return create_noncopyable().safe_then([](auto t
) {
56 ertr::future
<int> test_futurization() {
57 // we don't want to be enforced to always do `make_ready_future(...)`.
58 // as in seastar::future, the futurization should take care about
59 // turning non-future types (e.g. int) into futurized ones (e.g.
60 // ertr::future<int>).
61 return ertr::now().safe_then([] {
63 }).safe_then([](int life
) {
64 return ertr::make_ready_future
<int>(life
);
68 ertr::future
<noncopyable_t
> create_noncopyable() {
69 return ertr::make_ready_future
<noncopyable_t
>();
73 TEST_F(errorator_test_t
, basic
)
76 test_do_until().unsafe_get0();
80 TEST_F(errorator_test_t
, parallel_for_each
)
83 test_parallel_for_each().unsafe_get0();
87 TEST_F(errorator_test_t
, non_copy_then
)
90 test_non_copy_then().unsafe_get0();
94 TEST_F(errorator_test_t
, test_futurization
)
97 test_futurization().unsafe_get0();