]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/crimson/test_errorator.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / crimson / test_errorator.cc
CommitLineData
f67539c2
TL
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
2// vim: ts=8 sw=2 smarttab
3
20effc67
TL
4#include <boost/iterator/counting_iterator.hpp>
5#include <numeric>
6
f67539c2
TL
7#include "test/crimson/gtest_seastar.h"
8
9#include "crimson/common/errorator.h"
20effc67 10#include "crimson/common/errorator-loop.h"
f67539c2 11#include "crimson/common/log.h"
20effc67 12#include "seastar/core/sleep.hh"
f67539c2
TL
13
14struct errorator_test_t : public seastar_test_suite_t {
15 using ertr = crimson::errorator<crimson::ct_error::invarg>;
16 ertr::future<> test_do_until() {
20effc67 17 return crimson::repeat([i=0]() mutable {
f67539c2
TL
18 if (i < 5) {
19 ++i;
20effc67
TL
20 return ertr::make_ready_future<seastar::stop_iteration>(
21 seastar::stop_iteration::no);
f67539c2 22 } else {
20effc67
TL
23 return ertr::make_ready_future<seastar::stop_iteration>(
24 seastar::stop_iteration::yes);
f67539c2
TL
25 }
26 });
27 }
20effc67
TL
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) {
35 *sum += i;
36 }).safe_then([sum=std::move(sum)] {
37 int expected = std::accumulate(boost::make_counting_iterator(0),
38 boost::make_counting_iterator(SIZE),
39 0);
40 ASSERT_EQ(*sum, expected);
41 });
42 }
f67539c2
TL
43 struct noncopyable_t {
44 constexpr noncopyable_t() = default;
45 ~noncopyable_t() = default;
46 noncopyable_t(noncopyable_t&&) = default;
47 private:
48 noncopyable_t(const noncopyable_t&) = delete;
49 noncopyable_t& operator=(const noncopyable_t&) = delete;
50 };
51 ertr::future<> test_non_copy_then() {
52 return create_noncopyable().safe_then([](auto t) {
53 return ertr::now();
54 });
55 }
20effc67
TL
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([] {
62 return 42;
63 }).safe_then([](int life) {
64 return ertr::make_ready_future<int>(life);
65 });
66 }
f67539c2
TL
67private:
68 ertr::future<noncopyable_t> create_noncopyable() {
69 return ertr::make_ready_future<noncopyable_t>();
70 }
71};
72
73TEST_F(errorator_test_t, basic)
74{
75 run_async([this] {
76 test_do_until().unsafe_get0();
77 });
78}
79
20effc67
TL
80TEST_F(errorator_test_t, parallel_for_each)
81{
82 run_async([this] {
83 test_parallel_for_each().unsafe_get0();
84 });
85}
86
f67539c2
TL
87TEST_F(errorator_test_t, non_copy_then)
88{
89 run_async([this] {
90 test_non_copy_then().unsafe_get0();
91 });
92}
20effc67
TL
93
94TEST_F(errorator_test_t, test_futurization)
95{
96 run_async([this] {
97 test_futurization().unsafe_get0();
98 });
99}