]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/timer_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / tests / unit / timer_test.cc
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) 2014 Cloudius Systems, Ltd.
20 */
21
22 #include <seastar/core/app-template.hh>
23 #include <seastar/core/reactor.hh>
24 #include <seastar/core/print.hh>
25 #include <chrono>
26 #include <iostream>
27
28 using namespace seastar;
29 using namespace std::chrono_literals;
30
31 #define BUG() do { \
32 std::cerr << "ERROR @ " << __FILE__ << ":" << __LINE__ << std::endl; \
33 throw std::runtime_error("test failed"); \
34 } while (0)
35
36 #define OK() do { \
37 std::cerr << "OK @ " << __FILE__ << ":" << __LINE__ << std::endl; \
38 } while (0)
39
40 template <typename Clock>
41 struct timer_test {
42 timer<Clock> t1;
43 timer<Clock> t2;
44 timer<Clock> t3;
45 timer<Clock> t4;
46 timer<Clock> t5;
47 promise<> pr1;
48 promise<> pr2;
49
50 future<> run() {
51 t1.set_callback([this] {
52 OK();
53 fmt::print(" 500ms timer expired\n");
54 if (!t4.cancel()) {
55 BUG();
56 }
57 if (!t5.cancel()) {
58 BUG();
59 }
60 t5.arm(1100ms);
61 });
62 t2.set_callback([] { OK(); fmt::print(" 900ms timer expired\n"); });
63 t3.set_callback([] { OK(); fmt::print("1000ms timer expired\n"); });
64 t4.set_callback([] { OK(); fmt::print(" BAD cancelled timer expired\n"); });
65 t5.set_callback([this] { OK(); fmt::print("1600ms rearmed timer expired\n"); pr1.set_value(); });
66
67 t1.arm(500ms);
68 t2.arm(900ms);
69 t3.arm(1000ms);
70 t4.arm(700ms);
71 t5.arm(800ms);
72
73 return pr1.get_future().then([this] { return test_timer_cancelling(); });
74 }
75
76 future<> test_timer_cancelling() {
77 timer<Clock>& t1 = *new timer<Clock>();
78 t1.set_callback([] { BUG(); });
79 t1.arm(100ms);
80 t1.cancel();
81
82 t1.arm(100ms);
83 t1.cancel();
84
85 t1.set_callback([this] { OK(); pr2.set_value(); });
86 t1.arm(100ms);
87 return pr2.get_future().then([&t1] { delete &t1; });
88 }
89 };
90
91 int main(int ac, char** av) {
92 app_template app;
93 timer_test<steady_clock_type> t1;
94 timer_test<lowres_clock> t2;
95 return app.run_deprecated(ac, av, [&t1, &t2] {
96 fmt::print("=== Start High res clock test\n");
97 t1.run().then([&t2] {
98 fmt::print("=== Start Low res clock test\n");
99 return t2.run();
100 }).then([] {
101 fmt::print("Done\n");
102 engine().exit(0);
103 });
104 });
105 }