]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/coroutines_test.cc
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2019 ScyllaDB Ltd.
22 #include <seastar/core/coroutine.hh>
23 #include <seastar/core/future-util.hh>
24 #include <seastar/testing/test_case.hh>
26 using namespace seastar
;
30 future
<int> old_fashioned_continuations() {
31 return later().then([] {
36 future
<int> simple_coroutine() {
41 future
<int> ready_coroutine() {
45 future
<int, double> tuple_coroutine() {
46 co_return
std::tuple(1, 2.);
49 future
<int> failing_coroutine() {
56 SEASTAR_TEST_CASE(test_simple_coroutines
) {
57 BOOST_REQUIRE_EQUAL(co_await
old_fashioned_continuations(), 42);
58 BOOST_REQUIRE_EQUAL(co_await
simple_coroutine(), 53);
59 BOOST_REQUIRE_EQUAL(ready_coroutine().get0(), 64);
60 BOOST_REQUIRE(co_await
tuple_coroutine() == std::tuple(1, 2.));
61 BOOST_REQUIRE_EXCEPTION((void)co_await
failing_coroutine(), int, [] (auto v
) { return v
== 42; });
64 SEASTAR_TEST_CASE(test_abandond_coroutine
) {
65 compat::optional
<future
<int>> f
;
67 auto p1
= promise
<>();
68 auto p2
= promise
<>();
69 auto p3
= promise
<>();
70 f
= p1
.get_future().then([&] () -> future
<int> {
72 BOOST_CHECK_THROW(co_await p3
.get_future(), broken_promise
);
76 co_await p2
.get_future();
78 BOOST_CHECK_EQUAL(co_await
std::move(*f
), 1);
81 SEASTAR_TEST_CASE(test_scheduling_group
) {
82 auto other_sg
= co_await
create_scheduling_group("the other group", 10.f
);
84 auto p1
= promise
<>();
85 auto p2
= promise
<>();
87 auto p1b
= promise
<>();
88 auto p2b
= promise
<>();
89 auto f1
= p1b
.get_future();
90 auto f2
= p2b
.get_future();
92 BOOST_REQUIRE(current_scheduling_group() == default_scheduling_group());
93 auto f_ret
= with_scheduling_group(other_sg
,
94 [other_sg
] (future
<> f1
, future
<> f2
, promise
<> p1
, promise
<> p2
) -> future
<int> {
95 BOOST_REQUIRE(current_scheduling_group() == other_sg
);
96 BOOST_REQUIRE(other_sg
== other_sg
);
98 co_await
std::move(f1
);
99 BOOST_REQUIRE(current_scheduling_group() == other_sg
);
101 co_await
std::move(f2
);
102 BOOST_REQUIRE(current_scheduling_group() == other_sg
);
104 }, p1
.get_future(), p2
.get_future(), std::move(p1b
), std::move(p2b
));
106 co_await
std::move(f1
);
107 BOOST_REQUIRE(current_scheduling_group() == default_scheduling_group());
109 co_await
std::move(f2
);
110 BOOST_REQUIRE(current_scheduling_group() == default_scheduling_group());
112 BOOST_REQUIRE_EQUAL(co_await
std::move(f_ret
), 42);
113 BOOST_REQUIRE(current_scheduling_group() == default_scheduling_group());