]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_ceph_timer.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2020 Red Hat
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
19 #include <gtest/gtest.h>
21 #include "common/ceph_timer.h"
23 using namespace std::literals
;
29 static constexpr auto MAX_FUTURES
= 5;
30 ceph::timer
<TC
> timer
;
31 std::vector
<std::future
<void>> futures
;
32 for (auto i
= 0; i
< MAX_FUTURES
; ++i
) {
33 auto t
= TC::now() + 2s
;
35 futures
.push_back(p
.get_future());
36 timer
.add_event(t
, [p
= std::move(p
)]() mutable {
40 for (auto& f
: futures
)
47 ceph::timer
<TC
> timer
;
49 std::future
<typename
TC::time_point
> first
;
50 std::future
<typename
TC::time_point
> second
;
54 std::promise
<typename
TC::time_point
> p
;
55 second
= p
.get_future();
56 timer
.add_event(4s
, [p
= std::move(p
)]() mutable {
57 p
.set_value(TC::now());
61 std::promise
<typename
TC::time_point
> p
;
62 first
= p
.get_future();
63 timer
.add_event(2s
, [p
= std::move(p
)]() mutable {
64 p
.set_value(TC::now());
68 EXPECT_LT(first
.get(), second
.get());
75 Destructo(std::promise
<void>&& p
) : p(std::move(p
)) {}
76 Destructo(const Destructo
&) = delete;
77 Destructo
& operator =(const Destructo
&) = delete;
78 Destructo(Destructo
&& rhs
) {
83 Destructo
& operator =(Destructo
& rhs
) {
95 void operator ()() const {
100 template<typename TC
>
103 ceph::timer
<TC
> timer
;
104 static constexpr auto MAX_FUTURES
= 5;
105 std::vector
<std::future
<void>> futures
;
106 for (auto i
= 0; i
< MAX_FUTURES
; ++i
) {
107 std::promise
<void> p
;
108 futures
.push_back(p
.get_future());
109 timer
.add_event(100s
+ i
*1s
, Destructo(std::move(p
)));
111 timer
.cancel_all_events();
112 for (auto& f
: futures
)
116 template<typename TC
>
119 ceph::timer
<TC
> timer
;
121 std::promise
<void> p
;
122 auto f
= p
.get_future();
123 auto e
= timer
.add_event(100s
, Destructo(std::move(p
)));
124 EXPECT_TRUE(timer
.cancel_event(e
));
127 std::promise
<void> p
;
128 auto f
= p
.get_future();
129 auto e
= timer
.add_event(1s
, [p
= std::move(p
)]() mutable {
133 EXPECT_FALSE(timer
.cancel_event(e
));
138 TEST(RunSome
, Steady
)
140 run_some
<std::chrono::steady_clock
>();
144 run_some
<std::chrono::system_clock
>();
147 TEST(RunOrderly
, Steady
)
149 run_orderly
<std::chrono::steady_clock
>();
151 TEST(RunOrderly
, Wall
)
153 run_orderly
<std::chrono::system_clock
>();
156 TEST(CancelAll
, Steady
)
158 cancel_all
<std::chrono::steady_clock
>();
160 TEST(CancelAll
, Wall
)
162 cancel_all
<std::chrono::system_clock
>();