]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/tests/unit/abort_source_test.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / tests / unit / abort_source_test.cc
CommitLineData
11fdf7f2
TL
1/*
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.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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
16 * under the License.
17 */
18/*
19 * Copyright (C) 2017 ScyllaDB
20 */
21
22#include <seastar/testing/test_case.hh>
23
24#include <seastar/core/gate.hh>
25#include <seastar/core/sleep.hh>
9f95a23c 26#include <seastar/core/do_with.hh>
11fdf7f2
TL
27
28using namespace seastar;
29using namespace std::chrono_literals;
30
31SEASTAR_TEST_CASE(test_abort_source_notifies_subscriber) {
32 bool signalled = false;
33 auto as = abort_source();
f67539c2 34 auto st_opt = as.subscribe([&signalled] () noexcept {
11fdf7f2
TL
35 signalled = true;
36 });
37 BOOST_REQUIRE_EQUAL(true, bool(st_opt));
38 as.request_abort();
39 BOOST_REQUIRE_EQUAL(true, signalled);
20effc67 40 BOOST_REQUIRE_EQUAL(false, bool(st_opt));
11fdf7f2
TL
41 return make_ready_future<>();
42}
43
44SEASTAR_TEST_CASE(test_abort_source_subscription_unregister) {
45 bool signalled = false;
46 auto as = abort_source();
f67539c2 47 auto st_opt = as.subscribe([&signalled] () noexcept {
11fdf7f2
TL
48 signalled = true;
49 });
50 BOOST_REQUIRE_EQUAL(true, bool(st_opt));
51 st_opt = { };
52 as.request_abort();
53 BOOST_REQUIRE_EQUAL(false, signalled);
54 return make_ready_future<>();
55}
56
57SEASTAR_TEST_CASE(test_abort_source_rejects_subscription) {
58 auto as = abort_source();
59 as.request_abort();
f67539c2 60 auto st_opt = as.subscribe([] () noexcept { });
11fdf7f2
TL
61 BOOST_REQUIRE_EQUAL(false, bool(st_opt));
62 return make_ready_future<>();
63}
64
65SEASTAR_TEST_CASE(test_sleep_abortable) {
66 auto as = std::make_unique<abort_source>();
67 auto f = sleep_abortable(100s, *as).then_wrapped([] (auto&& f) {
68 try {
69 f.get();
70 BOOST_FAIL("should have failed");
71 } catch (const sleep_aborted& e) {
72 // expected
73 } catch (...) {
74 BOOST_FAIL("unexpected exception");
75 }
76 });
77 as->request_abort();
78 return f.finally([as = std::move(as)] { });
79}
9f95a23c
TL
80
81// Verify that negative sleep does not sleep forever. It should not sleep
82// at all.
83SEASTAR_TEST_CASE(test_negative_sleep_abortable) {
84 return do_with(abort_source(), [] (abort_source& as) {
85 return sleep_abortable(-10s, as);
86 });
87}